2019-08-09 10:17:04 +00:00
|
|
|
|
import Card from "@material-ui/core/Card";
|
|
|
|
|
import CardContent from "@material-ui/core/CardContent";
|
2021-01-12 11:11:15 +00:00
|
|
|
|
import { makeStyles } from "@material-ui/core/styles";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
import TextField from "@material-ui/core/TextField";
|
|
|
|
|
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";
|
2020-11-19 14:42:14 +00:00
|
|
|
|
import { AttributeErrorFragment } from "@saleor/fragments/types/AttributeErrorFragment";
|
2019-08-16 11:47:30 +00:00
|
|
|
|
import { commonMessages } from "@saleor/intl";
|
2021-01-12 11:11:15 +00:00
|
|
|
|
import {
|
|
|
|
|
AttributeEntityTypeEnum,
|
|
|
|
|
AttributeInputTypeEnum
|
|
|
|
|
} from "@saleor/types/globalTypes";
|
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";
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
2021-01-12 11:11:15 +00:00
|
|
|
|
const messages = defineMessages({
|
|
|
|
|
attributeLabel: {
|
|
|
|
|
defaultMessage: "Default Label",
|
|
|
|
|
description: "attribute's label"
|
|
|
|
|
},
|
|
|
|
|
attributeSlug: {
|
|
|
|
|
defaultMessage: "Attribute Code",
|
|
|
|
|
description: "attribute's slug short code label"
|
|
|
|
|
},
|
|
|
|
|
attributeSlugHelperText: {
|
|
|
|
|
defaultMessage: "This is used internally. Make sure you don’t use spaces",
|
|
|
|
|
description: "attribute slug input field helper text"
|
|
|
|
|
},
|
|
|
|
|
entityType: {
|
|
|
|
|
defaultMessage: "Entity",
|
|
|
|
|
description: "attribute's editor component entity"
|
|
|
|
|
},
|
|
|
|
|
inputType: {
|
|
|
|
|
defaultMessage: "Catalog Input type for Store Owner",
|
|
|
|
|
description: "attribute's editor component"
|
|
|
|
|
},
|
|
|
|
|
valueRequired: {
|
|
|
|
|
defaultMessage: "Value Required",
|
|
|
|
|
description: "check to require attribute to have value"
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const inputTypeMessages = defineMessages({
|
|
|
|
|
dropdown: {
|
|
|
|
|
defaultMessage: "Dropdown",
|
|
|
|
|
description: "product attribute type"
|
|
|
|
|
},
|
|
|
|
|
file: {
|
|
|
|
|
defaultMessage: "File",
|
|
|
|
|
description: "file attribute type"
|
|
|
|
|
},
|
|
|
|
|
multiselect: {
|
|
|
|
|
defaultMessage: "Multiple Select",
|
|
|
|
|
description: "product attribute type"
|
|
|
|
|
},
|
|
|
|
|
references: {
|
|
|
|
|
defaultMessage: "References",
|
|
|
|
|
description: "references attribute type"
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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: {
|
|
|
|
|
columnGap: theme.spacing(2) + "px",
|
|
|
|
|
display: "flex",
|
|
|
|
|
[theme.breakpoints.down("md")]: {
|
|
|
|
|
flexFlow: "wrap",
|
|
|
|
|
rowGap: theme.spacing(3) + "px"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
{ name: "AttributeDetails" }
|
|
|
|
|
);
|
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
|
export interface AttributeDetailsProps {
|
|
|
|
|
canChangeType: boolean;
|
|
|
|
|
data: AttributePageFormData;
|
|
|
|
|
disabled: boolean;
|
2020-11-19 14:42:14 +00:00
|
|
|
|
errors: 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 => {
|
|
|
|
|
const { canChangeType, data, disabled, errors, onChange } = props;
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
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-01-12 11:11:15 +00:00
|
|
|
|
const formErrors = getFormErrors(
|
|
|
|
|
["name", "slug", "inputType", "entityType"],
|
|
|
|
|
errors
|
|
|
|
|
);
|
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}
|
2020-03-24 14:05:26 +00:00
|
|
|
|
error={!!formErrors.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
|
2020-11-19 14:42:14 +00:00
|
|
|
|
helperText={getAttributeErrorMessage(formErrors.name, intl)}
|
2019-08-16 11:47:30 +00:00
|
|
|
|
value={data.name}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
/>
|
|
|
|
|
<FormSpacer />
|
|
|
|
|
<TextField
|
|
|
|
|
disabled={disabled}
|
2020-03-24 14:05:26 +00:00
|
|
|
|
error={!!formErrors.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={
|
2020-03-24 14:05:26 +00:00
|
|
|
|
getAttributeSlugErrorMessage(formErrors.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}
|
|
|
|
|
error={!!formErrors.inputType}
|
|
|
|
|
hint={getAttributeErrorMessage(formErrors.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={!!formErrors.entityType}
|
|
|
|
|
hint={getAttributeErrorMessage(formErrors.entityType, intl)}
|
|
|
|
|
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
|
|
|
|
/>
|
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
};
|
2019-08-09 10:17:04 +00:00
|
|
|
|
AttributeDetails.displayName = "AttributeDetails";
|
|
|
|
|
export default AttributeDetails;
|