saleor-dashboard/src/attributes/components/AttributeDetails/AttributeDetails.tsx

228 lines
6.9 KiB
TypeScript
Raw Normal View History

import { NumericUnits } from "@dashboard/attributes/components/AttributeDetails/NumericUnits";
import CardTitle from "@dashboard/components/CardTitle";
import ControlledCheckbox from "@dashboard/components/ControlledCheckbox";
import FormSpacer from "@dashboard/components/FormSpacer";
import SingleSelectField from "@dashboard/components/SingleSelectField";
import {
AttributeEntityTypeEnum,
Use graphql-codegen (#1874) * Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
2022-03-09 08:56:55 +00:00
AttributeErrorFragment,
AttributeInputTypeEnum,
} from "@dashboard/graphql";
import { UseFormResult } from "@dashboard/hooks/useForm";
import { commonMessages } from "@dashboard/intl";
import { getFormErrors } from "@dashboard/utils/errors";
import getAttributeErrorMessage from "@dashboard/utils/errors/attribute";
import { Card, CardContent, TextField } from "@material-ui/core";
Use graphql-codegen (#1874) * Use generated hooks in apps * Remove unused files * Use proper types in apps * Use generated hooks in attributes * Use generated hooks in auth module * Use generated hooks in categories * Use generated hooks in channels * Use generated types in collections * Remove legacy types from background tasks * Use generated hooks in customers * Use generated hooks in discounts * Use generated hook in file upload * Use generated types in gift cards * Use generated types in home * Use generated hooks in navigation * Use generated hooks in orders * Use generated hooks in pages * Use generated hooks in page types * Use generated hooks in permission groups * Use generated hooks in plugins * Use generated hooks in products * Use fragment to mark product variants * Improve code a bit * Use generated hooks in page types * Use generated types in searches * Use generated hooks in shipping * Use generated hooks in site settings * Use generated hooks in staff members * Use generated hooks in taxes * Place all gql generated files in one directory * Use generated hooks in translations * Use global types from new generated module * Use generated hooks in warehouses * Use generated hooks in webhooks * Use generated fragment types * Unclutter types * Remove hoc components * Split hooks and types * Fetch introspection file * Delete obsolete schema file * Fix rebase artifacts * Fix autoreplace * Fix auth provider tests * Fix urls * Remove leftover types * Fix rebase artifacts
2022-03-09 08:56:55 +00:00
import { makeStyles } from "@saleor/macaw-ui";
import React from "react";
import { defineMessages, useIntl } from "react-intl";
import slugify from "slugify";
2020-03-24 14:05:26 +00:00
import { getAttributeSlugErrorMessage } from "../../errors";
import { AttributePageFormData } from "../AttributePage";
import { inputTypeMessages, messages } from "./messages";
const entityTypeMessages = defineMessages({
page: {
id: "Iafyt5",
defaultMessage: "Pages",
description: "page attribute entity type",
},
product: {
id: "5TUpjG",
defaultMessage: "Products",
description: "product attribute entity type",
},
productVariant: {
id: "wsDF7X",
defaultMessage: "Product variants",
description: "product variant attribute entity type",
},
});
const useStyles = makeStyles(
theme => ({
inputTypeSection: {
Use MacawUI (#1229) * Replace withStyleswith useStyles (#1100) * Replace withStyleswith useStyles * Update messages * Use rem as a spacing unit (#1101) * Use rems as spacing units * Fix visual bugs * Update stories * Use macaw-ui as theme provider (#1108) * Use macaw ui as a theme provider * Add react-dom to aliases * Fix jest module resolution * Update useTheme hook usage * Fix test wrapper * Use macaw from git repo * Fix CI * Update stories * Fix aliasing * Extract savebar to macaw ui (#1146) * wip * Use savebar from macaw * Use confirm button from macaw * Improve file structure * Use sidebar context from macaw * Update macaw * Update macaw version * Remove savebar from storybook * Update stories * Use alerts and notifications from macaw (#1166) * Use alerts from macaw * Add notifications from macaw * Update stories * Pin macaw version * Encapsulate limit reached in one component * Remove unused imports * Use backlinks from macaw (#1183) * Use backlink from macaw * Update macaw version * Use macaw sidebar (#1148) * Use sidebar from macaw * Use shipped logo * Use lowercase * Update stories * Use user chip from macaw (#1191) * Use user chip from macaw * Use dedicated components for menu items * Simplify code * Bump version and fix types (#1210) * Rename onBack to onClick * Rename UserChip to UserChipMenu * Rename IMenuItem to SidebarMenuItem * Update macaw version * Fix tables after changes in macaw (#1220) * Update macaw version * Update changelog * Update stories * Fix after rebase * Update to macaw 0.2.0 * Lint files * Update macaw to 0.2.2
2021-07-21 08:59:52 +00:00
columnGap: theme.spacing(2),
display: "flex",
[theme.breakpoints.down("md")]: {
flexFlow: "wrap",
rowGap: theme.spacing(3),
},
},
}),
{ name: "AttributeDetails" },
);
export interface AttributeDetailsProps
extends Pick<
UseFormResult<AttributePageFormData>,
"set" | "setError" | "data" | "clearErrors" | "errors"
> {
2019-08-09 10:17:04 +00:00
canChangeType: boolean;
disabled: boolean;
apiErrors: AttributeErrorFragment[];
2019-08-09 10:17:04 +00:00
onChange: (event: React.ChangeEvent<any>) => void;
}
const AttributeDetails: React.FC<AttributeDetailsProps> = props => {
const {
canChangeType,
errors,
clearErrors,
setError,
data,
disabled,
apiErrors,
onChange,
set,
} = props;
const classes = useStyles(props);
const intl = useIntl();
const inputTypeChoices = [
{
label: intl.formatMessage(inputTypeMessages.dropdown),
value: AttributeInputTypeEnum.DROPDOWN,
},
{
label: intl.formatMessage(inputTypeMessages.multiselect),
value: AttributeInputTypeEnum.MULTISELECT,
File attributes (#884) * Update changelog with file attributes * Add file type attribute * Update attribute properties form * Update translation messages with file upload * Create generic attributes component (#832) * Create generic Attributes component * Add story for Attributes component * Remove deprecated attribute value type field from queries * Update test snapshots of attributes component * Add file upload field to atributes (#888) * Add story for Attributes component * Update test snapshots of attributes component * Create file upload field in attributes * Update upload file input data-test * Update storybook test snapshots of attributes * Add dedicated input props to file field * Run Cypress using custom API * Add missing error handling in file upload field Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com> * Add file attribute upload to page attributes (#894) * Support upload file attribute for pages * Update after review * Add file attribute upload to variant attributes (#892) * Support upload file attribute for variants * Update after review * Refactor attribute values errors merging * Update after review * Add file attribute upload to product attributes (#826) * Support upload file attribute for products * Update after review * Refactor attribute values errors merging * Refactor product attribute value delete handling * Fix deleting file in file upload field * Fix delete attribute values errors handling * Add link to file upload field (#898) * Update file attributes updates (#899) * Update file attributes updates * Refactor file uploads handling * Move attributes utils to attributes directory * Fix product channel listing updates * Clear file field value if file is not passed as prop * Delete attribute values before update (#908) * Delete file attributes after file update * Triggr CI * Show skeleton in file upload field during loading Co-authored-by: Krzysztof Wolski <krzysztof.k.wolski@gmail.com>
2020-12-16 10:53:28 +00:00
},
{
label: intl.formatMessage(inputTypeMessages.file),
value: AttributeInputTypeEnum.FILE,
},
{
label: intl.formatMessage(inputTypeMessages.references),
value: AttributeInputTypeEnum.REFERENCE,
},
{
2022-07-19 14:16:43 +00:00
label: intl.formatMessage(inputTypeMessages.plainText),
value: AttributeInputTypeEnum.PLAIN_TEXT,
},
{
label: intl.formatMessage(inputTypeMessages.richText),
value: AttributeInputTypeEnum.RICH_TEXT,
},
{
label: intl.formatMessage(inputTypeMessages.numeric),
value: AttributeInputTypeEnum.NUMERIC,
},
{
label: intl.formatMessage(inputTypeMessages.boolean),
value: AttributeInputTypeEnum.BOOLEAN,
},
{
label: intl.formatMessage(inputTypeMessages.date),
value: AttributeInputTypeEnum.DATE,
},
{
label: intl.formatMessage(inputTypeMessages.dateTime),
value: AttributeInputTypeEnum.DATE_TIME,
},
{
label: intl.formatMessage(inputTypeMessages.swatch),
value: AttributeInputTypeEnum.SWATCH,
},
];
const entityTypeChoices = [
{
label: intl.formatMessage(entityTypeMessages.page),
value: AttributeEntityTypeEnum.PAGE,
},
{
label: intl.formatMessage(entityTypeMessages.product),
value: AttributeEntityTypeEnum.PRODUCT,
},
{
label: intl.formatMessage(entityTypeMessages.productVariant),
value: AttributeEntityTypeEnum.PRODUCT_VARIANT,
},
];
const formApiErrors = getFormErrors(
["name", "slug", "inputType", "entityType", "unit"],
apiErrors,
);
2020-03-24 14:05:26 +00:00
return (
<Card>
<CardTitle
2019-08-20 09:17:06 +00:00
title={intl.formatMessage(commonMessages.generalInformations)}
2019-08-09 10:17:04 +00:00
/>
<CardContent>
<TextField
disabled={disabled}
error={!!formApiErrors.name}
label={intl.formatMessage(messages.attributeLabel)}
name={"name" as keyof AttributePageFormData}
fullWidth
helperText={getAttributeErrorMessage(formApiErrors.name, intl)}
value={data.name}
onChange={onChange}
/>
<FormSpacer />
<TextField
disabled={disabled}
error={!!formApiErrors.slug}
label={intl.formatMessage(messages.attributeSlug)}
name={"slug" as keyof AttributePageFormData}
placeholder={slugify(data.name).toLowerCase()}
fullWidth
helperText={
getAttributeSlugErrorMessage(formApiErrors.slug, intl) ||
intl.formatMessage(messages.attributeSlugHelperText)
}
value={data.slug}
onChange={onChange}
/>
<FormSpacer />
<div className={classes.inputTypeSection}>
<SingleSelectField
choices={inputTypeChoices}
disabled={disabled || !canChangeType}
error={!!formApiErrors.inputType}
hint={getAttributeErrorMessage(formApiErrors.inputType, intl)}
label={intl.formatMessage(messages.inputType)}
name="inputType"
onChange={onChange}
value={data.inputType}
/>
{data.inputType === AttributeInputTypeEnum.REFERENCE && (
<SingleSelectField
choices={entityTypeChoices}
disabled={disabled || !canChangeType}
error={!!formApiErrors.entityType}
hint={getAttributeErrorMessage(formApiErrors.entityType, intl)}
label={intl.formatMessage(messages.entityType)}
name="entityType"
onChange={onChange}
value={data.entityType}
/>
)}
</div>
<FormSpacer />
2019-09-09 09:28:06 +00:00
<ControlledCheckbox
2019-09-11 14:24:24 +00:00
name={"valueRequired" as keyof AttributePageFormData}
label={intl.formatMessage(messages.valueRequired)}
2019-09-09 09:28:06 +00:00
checked={data.valueRequired}
onChange={onChange}
2019-09-09 09:28:06 +00:00
disabled={disabled}
/>
{data.inputType === AttributeInputTypeEnum.NUMERIC && (
<NumericUnits
data={data}
errors={errors}
disabled={disabled}
clearErrors={clearErrors}
setError={setError}
set={set}
/>
)}
</CardContent>
</Card>
);
};
2019-08-09 10:17:04 +00:00
AttributeDetails.displayName = "AttributeDetails";
export default AttributeDetails;