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

111 lines
3.4 KiB
TypeScript
Raw Normal View History

import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
TextField
} from "@material-ui/core";
import { getAttributeValueErrorMessage } from "@saleor/attributes/errors";
2019-08-09 10:17:04 +00:00
import ConfirmButton, {
ConfirmButtonTransitionState
} from "@saleor/components/ConfirmButton";
import Form from "@saleor/components/Form";
Page types (#807) * Create attribute class selector * Use ProductAttributeType to check if product is simple or with variants * Allow attribute class selection only during its creation * Update attribute type selection translations * Show only product attributes in columns picker on product list view * Cleanups in Attribute Organization component * Create Page Types list page * Create content management section in settings * Implement page types list view * Remove unused imports from page type list * Updatte page type list style * Remove legacy code from page type list component * Update PageTypeListPage component * Create Page Types details page * Fix page type attribute reordering * Implement PageType create view * Implement PageType update view * gUpdate page type details components * Fix page type update component * Update page type components stories * Update page type errors handling * Update page type details view * Create Page Types details page * Implement PageType create view * Update product attribute assignment mutations * Add page types attribute assignment mutations * Add page types attribute assignment handling * Temporarily fix page create mutation * Update page type error messages * Remove legacy storybook page type stories * Update attribute assignment dialogs stories * Update page type details error handling * Update props for page type components * Create attribute class selector * Implement page types list view * Add page type selector on page create and details views * Add attributes list to page details views * Update page types list * Use attribute errors for attributes muatations * Save attribute values on page create and update * Update messages for page view * Update page attributes fragment * Use AttributeError in AttributeBulkDelete * Update page type and its attribute selection * Handle page types deleting * Update page types deleting messages * Handle page types attribute reorder * Fix PageOrganizeContent component types * Update graphqql types * Fix page fixture * Update messages * Update test snapshots * Pass pageTypes to PageForm * Update changelog with page type addition note * Update package-lock * Update test snapshots * Fix malformed generated type * Update messages after rebase
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";
import { buttonMessages } from "@saleor/intl";
2019-08-09 10:17:04 +00:00
import { maybe } from "@saleor/misc";
2020-03-24 14:05:26 +00:00
import { getFormErrors } from "@saleor/utils/errors";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { AttributeDetails_attribute_values_edges_node } from "../../types/AttributeDetails";
2019-08-09 10:17:04 +00:00
export interface AttributeValueEditDialogFormData {
name: string;
}
export interface AttributeValueEditDialogProps {
attributeValue: AttributeDetails_attribute_values_edges_node | null;
2019-08-09 10:17:04 +00:00
confirmButtonState: ConfirmButtonTransitionState;
disabled: boolean;
Page types (#807) * Create attribute class selector * Use ProductAttributeType to check if product is simple or with variants * Allow attribute class selection only during its creation * Update attribute type selection translations * Show only product attributes in columns picker on product list view * Cleanups in Attribute Organization component * Create Page Types list page * Create content management section in settings * Implement page types list view * Remove unused imports from page type list * Updatte page type list style * Remove legacy code from page type list component * Update PageTypeListPage component * Create Page Types details page * Fix page type attribute reordering * Implement PageType create view * Implement PageType update view * gUpdate page type details components * Fix page type update component * Update page type components stories * Update page type errors handling * Update page type details view * Create Page Types details page * Implement PageType create view * Update product attribute assignment mutations * Add page types attribute assignment mutations * Add page types attribute assignment handling * Temporarily fix page create mutation * Update page type error messages * Remove legacy storybook page type stories * Update attribute assignment dialogs stories * Update page type details error handling * Update props for page type components * Create attribute class selector * Implement page types list view * Add page type selector on page create and details views * Add attributes list to page details views * Update page types list * Use attribute errors for attributes muatations * Save attribute values on page create and update * Update messages for page view * Update page attributes fragment * Use AttributeError in AttributeBulkDelete * Update page type and its attribute selection * Handle page types deleting * Update page types deleting messages * Handle page types attribute reorder * Fix PageOrganizeContent component types * Update graphqql types * Fix page fixture * Update messages * Update test snapshots * Pass pageTypes to PageForm * Update changelog with page type addition note * Update package-lock * Update test snapshots * Fix malformed generated type * Update messages after rebase
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;
}
const AttributeValueEditDialog: React.FC<AttributeValueEditDialogProps> = ({
2019-08-09 10:17:04 +00:00
attributeValue,
confirmButtonState,
disabled,
errors: apiErrors,
onClose,
onSubmit,
open
}) => {
const intl = useIntl();
2019-08-09 10:17:04 +00:00
const initialForm: AttributeValueEditDialogFormData = {
name: maybe(() => attributeValue.name, "")
};
const errors = useModalDialogErrors(apiErrors, open);
2020-03-24 14:05:26 +00:00
const formErrors = getFormErrors(["name"], errors);
2019-08-09 10:17:04 +00:00
return (
<Dialog onClose={onClose} open={open} fullWidth maxWidth="sm">
<DialogTitle>
{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}>
{({ change, data, submit }) => (
2019-08-09 10:17:04 +00:00
<>
<DialogContent>
<TextField
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}
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}
/>
</DialogContent>
<DialogActions>
<Button onClick={onClose}>
2019-10-21 14:46:12 +00:00
<FormattedMessage {...buttonMessages.back} />
2019-08-09 10:17:04 +00:00
</Button>
<ConfirmButton
transitionState={confirmButtonState}
color="primary"
variant="contained"
onClick={submit}
>
<FormattedMessage {...buttonMessages.save} />
2019-08-09 10:17:04 +00:00
</ConfirmButton>
</DialogActions>
</>
)}
</Form>
</Dialog>
);
};
AttributeValueEditDialog.displayName = "AttributeValueEditDialog";
export default AttributeValueEditDialog;