2021-05-14 08:15:15 +00:00
|
|
|
import { Card, CardContent, TextField } from "@material-ui/core";
|
2021-04-29 08:58:03 +00:00
|
|
|
import { NumericUnits } from "@saleor/attributes/components/AttributeDetails/NumericUnits";
|
2019-08-09 10:17:04 +00:00
|
|
|
import CardTitle from "@saleor/components/CardTitle";
|
2019-09-09 09:28:06 +00:00
|
|
|
import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
|
2019-08-09 10:17:04 +00:00
|
|
|
import FormSpacer from "@saleor/components/FormSpacer";
|
|
|
|
import SingleSelectField from "@saleor/components/SingleSelectField";
|
2021-01-12 11:11:15 +00:00
|
|
|
import {
|
|
|
|
AttributeEntityTypeEnum,
|
2022-03-09 08:56:55 +00:00
|
|
|
AttributeErrorFragment,
|
2021-01-12 11:11:15 +00:00
|
|
|
AttributeInputTypeEnum
|
2022-03-09 08:56:55 +00:00
|
|
|
} from "@saleor/graphql";
|
|
|
|
import { UseFormResult } from "@saleor/hooks/useForm";
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
|
|
|
import { makeStyles } from "@saleor/macaw-ui";
|
2020-11-19 14:42:14 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
|
|
|
import getAttributeErrorMessage from "@saleor/utils/errors/attribute";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
2021-01-12 11:11:15 +00:00
|
|
|
import { defineMessages, useIntl } from "react-intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
import slugify from "slugify";
|
|
|
|
|
2020-03-24 14:05:26 +00:00
|
|
|
import { getAttributeSlugErrorMessage } from "../../errors";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { AttributePageFormData } from "../AttributePage";
|
2021-04-29 08:58:03 +00:00
|
|
|
import { inputTypeMessages, messages } from "./messages";
|
2021-01-12 11:11:15 +00:00
|
|
|
|
|
|
|
const entityTypeMessages = defineMessages({
|
|
|
|
page: {
|
|
|
|
defaultMessage: "Pages",
|
|
|
|
description: "page attribute entity type"
|
2021-01-20 16:37:36 +00:00
|
|
|
},
|
|
|
|
product: {
|
|
|
|
defaultMessage: "Products",
|
|
|
|
description: "product attribute entity type"
|
2021-01-12 11:11:15 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
inputTypeSection: {
|
2021-07-21 08:59:52 +00:00
|
|
|
columnGap: theme.spacing(2),
|
2021-01-12 11:11:15 +00:00
|
|
|
display: "flex",
|
|
|
|
[theme.breakpoints.down("md")]: {
|
|
|
|
flexFlow: "wrap",
|
2021-07-21 08:59:52 +00:00
|
|
|
rowGap: theme.spacing(3)
|
2021-01-12 11:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
{ name: "AttributeDetails" }
|
|
|
|
);
|
|
|
|
|
2021-04-29 08:58:03 +00:00
|
|
|
export interface AttributeDetailsProps
|
|
|
|
extends Pick<
|
|
|
|
UseFormResult<AttributePageFormData>,
|
|
|
|
"set" | "setError" | "data" | "clearErrors" | "errors"
|
|
|
|
> {
|
2019-08-09 10:17:04 +00:00
|
|
|
canChangeType: boolean;
|
|
|
|
disabled: boolean;
|
2021-04-29 08:58:03 +00:00
|
|
|
apiErrors: AttributeErrorFragment[];
|
2019-08-09 10:17:04 +00:00
|
|
|
onChange: (event: React.ChangeEvent<any>) => void;
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:11:15 +00:00
|
|
|
const AttributeDetails: React.FC<AttributeDetailsProps> = props => {
|
2021-04-29 08:58:03 +00:00
|
|
|
const {
|
|
|
|
canChangeType,
|
|
|
|
errors,
|
|
|
|
clearErrors,
|
|
|
|
setError,
|
|
|
|
data,
|
|
|
|
disabled,
|
|
|
|
apiErrors,
|
|
|
|
onChange,
|
|
|
|
set
|
|
|
|
} = props;
|
2021-01-12 11:11:15 +00:00
|
|
|
const classes = useStyles(props);
|
2019-08-16 11:47:30 +00:00
|
|
|
const intl = useIntl();
|
|
|
|
const inputTypeChoices = [
|
|
|
|
{
|
2021-01-12 11:11:15 +00:00
|
|
|
label: intl.formatMessage(inputTypeMessages.dropdown),
|
2019-08-16 11:47:30 +00:00
|
|
|
value: AttributeInputTypeEnum.DROPDOWN
|
|
|
|
},
|
|
|
|
{
|
2021-01-12 11:11:15 +00:00
|
|
|
label: intl.formatMessage(inputTypeMessages.multiselect),
|
2019-08-16 11:47:30 +00:00
|
|
|
value: AttributeInputTypeEnum.MULTISELECT
|
2020-12-16 10:53:28 +00:00
|
|
|
},
|
|
|
|
{
|
2021-01-12 11:11:15 +00:00
|
|
|
label: intl.formatMessage(inputTypeMessages.file),
|
2020-12-16 10:53:28 +00:00
|
|
|
value: AttributeInputTypeEnum.FILE
|
2021-01-12 11:11:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.references),
|
|
|
|
value: AttributeInputTypeEnum.REFERENCE
|
2021-04-09 07:51:49 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.text),
|
|
|
|
value: AttributeInputTypeEnum.RICH_TEXT
|
2021-04-29 08:58:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.numeric),
|
|
|
|
value: AttributeInputTypeEnum.NUMERIC
|
2021-06-19 23:13:16 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.boolean),
|
|
|
|
value: AttributeInputTypeEnum.BOOLEAN
|
2021-07-29 12:15:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.date),
|
|
|
|
value: AttributeInputTypeEnum.DATE
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.dateTime),
|
|
|
|
value: AttributeInputTypeEnum.DATE_TIME
|
2021-09-21 13:16:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(inputTypeMessages.swatch),
|
|
|
|
value: AttributeInputTypeEnum.SWATCH
|
2021-01-12 11:11:15 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
const entityTypeChoices = [
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(entityTypeMessages.page),
|
|
|
|
value: AttributeEntityTypeEnum.PAGE
|
2021-01-20 16:37:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
label: intl.formatMessage(entityTypeMessages.product),
|
|
|
|
value: AttributeEntityTypeEnum.PRODUCT
|
2019-08-16 11:47:30 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-04-29 08:58:03 +00:00
|
|
|
const formApiErrors = getFormErrors(
|
|
|
|
["name", "slug", "inputType", "entityType", "unit"],
|
|
|
|
apiErrors
|
2021-01-12 11:11:15 +00:00
|
|
|
);
|
2020-03-24 14:05:26 +00:00
|
|
|
|
2019-08-16 11:47:30 +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
|
|
|
/>
|
2019-08-16 11:47:30 +00:00
|
|
|
<CardContent>
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2021-04-29 08:58:03 +00:00
|
|
|
error={!!formApiErrors.name}
|
2021-01-12 11:11:15 +00:00
|
|
|
label={intl.formatMessage(messages.attributeLabel)}
|
2019-08-16 11:47:30 +00:00
|
|
|
name={"name" as keyof AttributePageFormData}
|
|
|
|
fullWidth
|
2021-04-29 08:58:03 +00:00
|
|
|
helperText={getAttributeErrorMessage(formApiErrors.name, intl)}
|
2019-08-16 11:47:30 +00:00
|
|
|
value={data.name}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
|
|
|
<TextField
|
|
|
|
disabled={disabled}
|
2021-04-29 08:58:03 +00:00
|
|
|
error={!!formApiErrors.slug}
|
2021-01-12 11:11:15 +00:00
|
|
|
label={intl.formatMessage(messages.attributeSlug)}
|
2019-08-16 11:47:30 +00:00
|
|
|
name={"slug" as keyof AttributePageFormData}
|
|
|
|
placeholder={slugify(data.name).toLowerCase()}
|
|
|
|
fullWidth
|
|
|
|
helperText={
|
2021-04-29 08:58:03 +00:00
|
|
|
getAttributeSlugErrorMessage(formApiErrors.slug, intl) ||
|
2021-01-12 11:11:15 +00:00
|
|
|
intl.formatMessage(messages.attributeSlugHelperText)
|
2019-08-16 11:47:30 +00:00
|
|
|
}
|
|
|
|
value={data.slug}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
<FormSpacer />
|
2021-01-12 11:11:15 +00:00
|
|
|
<div className={classes.inputTypeSection}>
|
|
|
|
<SingleSelectField
|
|
|
|
choices={inputTypeChoices}
|
|
|
|
disabled={disabled || !canChangeType}
|
2021-04-29 08:58:03 +00:00
|
|
|
error={!!formApiErrors.inputType}
|
|
|
|
hint={getAttributeErrorMessage(formApiErrors.inputType, intl)}
|
2021-01-12 11:11:15 +00:00
|
|
|
label={intl.formatMessage(messages.inputType)}
|
|
|
|
name="inputType"
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.inputType}
|
|
|
|
/>
|
|
|
|
{data.inputType === AttributeInputTypeEnum.REFERENCE && (
|
|
|
|
<SingleSelectField
|
|
|
|
choices={entityTypeChoices}
|
|
|
|
disabled={disabled || !canChangeType}
|
2021-04-29 08:58:03 +00:00
|
|
|
error={!!formApiErrors.entityType}
|
|
|
|
hint={getAttributeErrorMessage(formApiErrors.entityType, intl)}
|
2021-01-12 11:11:15 +00:00
|
|
|
label={intl.formatMessage(messages.entityType)}
|
|
|
|
name="entityType"
|
|
|
|
onChange={onChange}
|
|
|
|
value={data.entityType}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
2019-08-16 11:47:30 +00:00
|
|
|
<FormSpacer />
|
2019-09-09 09:28:06 +00:00
|
|
|
<ControlledCheckbox
|
2019-09-11 14:24:24 +00:00
|
|
|
name={"valueRequired" as keyof AttributePageFormData}
|
2021-01-12 11:11:15 +00:00
|
|
|
label={intl.formatMessage(messages.valueRequired)}
|
2019-09-09 09:28:06 +00:00
|
|
|
checked={data.valueRequired}
|
2019-08-16 11:47:30 +00:00
|
|
|
onChange={onChange}
|
2019-09-09 09:28:06 +00:00
|
|
|
disabled={disabled}
|
2019-08-16 11:47:30 +00:00
|
|
|
/>
|
2021-04-29 08:58:03 +00:00
|
|
|
{data.inputType === AttributeInputTypeEnum.NUMERIC && (
|
|
|
|
<NumericUnits
|
|
|
|
data={data}
|
|
|
|
errors={errors}
|
|
|
|
disabled={disabled}
|
|
|
|
clearErrors={clearErrors}
|
|
|
|
setError={setError}
|
|
|
|
set={set}
|
|
|
|
/>
|
|
|
|
)}
|
2019-08-16 11:47:30 +00:00
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
};
|
2019-08-09 10:17:04 +00:00
|
|
|
AttributeDetails.displayName = "AttributeDetails";
|
|
|
|
export default AttributeDetails;
|