2021-05-14 08:15:15 +00:00
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogActions,
|
|
|
|
DialogContent,
|
|
|
|
DialogTitle,
|
|
|
|
TextField
|
|
|
|
} from "@material-ui/core";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { getAttributeValueErrorMessage } from "@saleor/attributes/errors";
|
2022-01-28 12:34:20 +00:00
|
|
|
import BackButton from "@saleor/components/BackButton";
|
|
|
|
import ConfirmButton from "@saleor/components/ConfirmButton";
|
2019-08-09 10:17:04 +00:00
|
|
|
import Form from "@saleor/components/Form";
|
2020-11-19 14:42:14 +00:00
|
|
|
import { AttributeErrorFragment } from "@saleor/fragments/types/AttributeErrorFragment";
|
2019-08-09 10:17:04 +00:00
|
|
|
import useModalDialogErrors from "@saleor/hooks/useModalDialogErrors";
|
2019-08-23 12:23:59 +00:00
|
|
|
import { buttonMessages } from "@saleor/intl";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui";
|
2021-09-21 13:16:21 +00:00
|
|
|
import { AttributeInputTypeEnum } from "@saleor/types/globalTypes";
|
2020-03-24 14:05:26 +00:00
|
|
|
import { getFormErrors } from "@saleor/utils/errors";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
2021-09-21 13:16:21 +00:00
|
|
|
import { AttributeValueEditDialogFormData } from "../../utils/data";
|
|
|
|
import AttributeSwatchField from "../AttributeSwatchField";
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
export interface AttributeValueEditDialogProps {
|
2021-05-28 09:59:25 +00:00
|
|
|
attributeValue: AttributeValueEditDialogFormData | null;
|
2019-08-09 10:17:04 +00:00
|
|
|
confirmButtonState: ConfirmButtonTransitionState;
|
|
|
|
disabled: boolean;
|
2020-11-19 14:42:14 +00:00
|
|
|
errors: AttributeErrorFragment[];
|
2019-08-09 10:17:04 +00:00
|
|
|
open: boolean;
|
|
|
|
onSubmit: (data: AttributeValueEditDialogFormData) => void;
|
|
|
|
onClose: () => void;
|
2021-09-21 13:16:21 +00:00
|
|
|
inputType?: AttributeInputTypeEnum;
|
2019-08-09 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
const AttributeValueEditDialog: React.FC<AttributeValueEditDialogProps> = ({
|
2019-08-09 10:17:04 +00:00
|
|
|
attributeValue,
|
|
|
|
confirmButtonState,
|
|
|
|
disabled,
|
|
|
|
errors: apiErrors,
|
|
|
|
onClose,
|
|
|
|
onSubmit,
|
2021-09-21 13:16:21 +00:00
|
|
|
open,
|
|
|
|
inputType
|
2019-08-09 10:17:04 +00:00
|
|
|
}) => {
|
2019-08-16 11:47:30 +00:00
|
|
|
const intl = useIntl();
|
2021-09-21 13:16:21 +00:00
|
|
|
const attributeValueFields = attributeValue?.fileUrl
|
|
|
|
? {
|
|
|
|
fileUrl: attributeValue?.fileUrl,
|
|
|
|
contentType: attributeValue?.contentType
|
|
|
|
}
|
|
|
|
: { value: attributeValue?.value ?? "" };
|
|
|
|
|
2019-08-09 10:17:04 +00:00
|
|
|
const initialForm: AttributeValueEditDialogFormData = {
|
2021-09-21 13:16:21 +00:00
|
|
|
name: attributeValue?.name ?? "",
|
|
|
|
...attributeValueFields
|
2019-08-09 10:17:04 +00:00
|
|
|
};
|
|
|
|
const errors = useModalDialogErrors(apiErrors, open);
|
2020-03-24 14:05:26 +00:00
|
|
|
const formErrors = getFormErrors(["name"], errors);
|
2021-09-21 13:16:21 +00:00
|
|
|
const isSwatch = inputType === AttributeInputTypeEnum.SWATCH;
|
2019-08-09 10:17:04 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
|
|
|
|
<DialogTitle>
|
2019-08-16 11:47:30 +00:00
|
|
|
{attributeValue === null ? (
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Add Value"
|
|
|
|
description="add attribute value"
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Edit Value"
|
|
|
|
description="edit attribute value"
|
|
|
|
/>
|
|
|
|
)}
|
2019-08-09 10:17:04 +00:00
|
|
|
</DialogTitle>
|
2020-02-24 14:14:48 +00:00
|
|
|
<Form initial={initialForm} onSubmit={onSubmit}>
|
2021-09-21 13:16:21 +00:00
|
|
|
{({ errors, set, change, clearErrors, setError, data, submit }) => (
|
2019-08-09 10:17:04 +00:00
|
|
|
<>
|
|
|
|
<DialogContent>
|
|
|
|
<TextField
|
2021-07-09 09:43:45 +00:00
|
|
|
data-test-id="valueName"
|
2019-08-09 10:17:04 +00:00
|
|
|
autoFocus
|
|
|
|
disabled={disabled}
|
2020-03-24 14:05:26 +00:00
|
|
|
error={!!formErrors.name}
|
2019-08-09 10:17:04 +00:00
|
|
|
fullWidth
|
2020-03-24 14:05:26 +00:00
|
|
|
helperText={getAttributeValueErrorMessage(
|
|
|
|
formErrors.name,
|
|
|
|
intl
|
|
|
|
)}
|
2019-08-09 10:17:04 +00:00
|
|
|
name={"name" as keyof AttributeValueEditDialogFormData}
|
2019-08-16 11:47:30 +00:00
|
|
|
label={intl.formatMessage({
|
|
|
|
defaultMessage: "Name",
|
2019-08-22 16:25:55 +00:00
|
|
|
description: "attribute name"
|
2019-08-09 10:17:04 +00:00
|
|
|
})}
|
|
|
|
value={data.name}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
2021-09-21 13:16:21 +00:00
|
|
|
{isSwatch && (
|
|
|
|
<AttributeSwatchField
|
|
|
|
data={data}
|
|
|
|
errors={errors}
|
|
|
|
clearErrors={clearErrors}
|
|
|
|
setError={setError}
|
|
|
|
set={set}
|
|
|
|
/>
|
|
|
|
)}
|
2019-08-09 10:17:04 +00:00
|
|
|
</DialogContent>
|
|
|
|
<DialogActions>
|
2022-01-28 12:34:20 +00:00
|
|
|
<BackButton onClick={onClose} />
|
2019-08-09 10:17:04 +00:00
|
|
|
<ConfirmButton
|
2021-07-09 09:43:45 +00:00
|
|
|
data-test="submit"
|
2019-08-09 10:17:04 +00:00
|
|
|
transitionState={confirmButtonState}
|
|
|
|
onClick={submit}
|
|
|
|
>
|
2019-08-23 12:23:59 +00:00
|
|
|
<FormattedMessage {...buttonMessages.save} />
|
2019-08-09 10:17:04 +00:00
|
|
|
</ConfirmButton>
|
|
|
|
</DialogActions>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Form>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
AttributeValueEditDialog.displayName = "AttributeValueEditDialog";
|
|
|
|
export default AttributeValueEditDialog;
|