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

261 lines
8.6 KiB
TypeScript
Raw Normal View History

import { attributeListUrl } from "@dashboard/attributes/urls";
import { ATTRIBUTE_TYPES_WITH_DEDICATED_VALUES } from "@dashboard/attributes/utils/data";
import { Backlink } from "@dashboard/components/Backlink";
import CardSpacer from "@dashboard/components/CardSpacer";
import Container from "@dashboard/components/Container";
import Form from "@dashboard/components/Form";
import Grid from "@dashboard/components/Grid";
import Metadata from "@dashboard/components/Metadata/Metadata";
import { MetadataFormData } from "@dashboard/components/Metadata/types";
import PageHeader from "@dashboard/components/PageHeader";
import Savebar from "@dashboard/components/Savebar";
import { ListSettingsUpdate } from "@dashboard/components/TablePagination";
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 {
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
AttributeDetailsFragment,
AttributeDetailsQuery,
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,
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
AttributeInputTypeEnum,
AttributeTypeEnum,
MeasurementUnitsEnum,
} from "@dashboard/graphql";
import { SubmitPromise } from "@dashboard/hooks/useForm";
import useNavigator from "@dashboard/hooks/useNavigator";
import { sectionNames } from "@dashboard/intl";
import { ListSettings, ReorderAction } from "@dashboard/types";
import { mapEdgesToItems, mapMetadataItemToInput } from "@dashboard/utils/maps";
import useMetadataChangeTrigger from "@dashboard/utils/metadata/useMetadataChangeTrigger";
import { ConfirmButtonTransitionState } from "@saleor/macaw-ui";
import React from "react";
import { useIntl } from "react-intl";
import slugify from "slugify";
2019-08-09 10:17:04 +00:00
import AttributeDetails from "../AttributeDetails";
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 AttributeOrganization from "../AttributeOrganization";
2019-08-09 10:17:04 +00:00
import AttributeProperties from "../AttributeProperties";
import AttributeValues from "../AttributeValues";
export interface AttributePageProps {
attribute?: AttributeDetailsFragment | null | undefined;
2019-08-09 10:17:04 +00:00
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
saveButtonBarState: ConfirmButtonTransitionState;
values?:
| NonNullable<AttributeDetailsQuery["attribute"]>["choices"]
| undefined;
2019-08-09 10:17:04 +00:00
onDelete: () => void;
Exit dirty form (#1816) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Fixes after review * Add default value for useform prop opts so the app doesn't crash * Add missing category prop to getting initial data for category details form * Add exit dialog to everywhere WIP (#1600) * Add Exit form prompt component and change some minor styles in other components to match * Add Exit form prompt provider * Adjust generic form and useform hook to allow using exit form prompt provider * Add exit form prompt provider to index * wip * Fix types * Fix styling * Fix types * Revert warehouse details refactor * Add handling of edge cases to exit prompt * Refactor, add comments, fix some types * Refactor after exit form dialog name change * fix types * Add CommonUseFormResultWithHandlers type for later use and refactor handleFormSubmit util * Refactor login form not to use custom form since it doesn't need to * Add exit form dialog to order refund page * Add exit form dialog to order return page * Add exit form dialog to order order settings page * Add exit form dialog to product variant page * Add exit form dialog to product create page * Add exit form dialog to product update page * Add exit form dialog to product variant create page * Fix confirm leave prop passing in generic Form * Add util function to handle for submit to extract errors * Add confirmLeave prop to generic forms * Move handleChange for custom forms to useForm * Add exit dialog to more forms * Add extract mutation errors util function * Add extracting errors to submit functions that use metadata create handler * Fix typo * Add missing category prop to getting initial data for category details form * Fix types * wip * wip * wip * wip * Fix types & refactor * Fix types & refactor * Fix typescript * Fix unmatching tag * Fixes * Add handling of multiple forms at once to exit dirty form provider * Change all usages of ExitFormDialogContext to designated hook * wip * wip * wip * Fix types wip * Fix types * Remove console logs * Add isSubmitting prop to exit form dialog in order to avoid enabling exit dialog while submit is still in progresS * Replace handleSubmit global util with a hook to use exit form dialog props inside * Move useHandleSubmit to general hooks dir, update imports * Small fixes * Update snapshots * Fix types * Small fixes due to extensive rebase * Update package lock * Fixes after rebase * Remove exit form from customer address dialog * Fix types and update messages * Fix types * Change imports names * Refactor * Remove unnecessary console.log * Update types, snapshots. etc after rebase
2022-02-01 09:58:06 +00:00
onSubmit: (data: AttributePageFormData) => SubmitPromise;
2019-08-09 10:17:04 +00:00
onValueAdd: () => void;
onValueDelete: (id: string) => void;
onValueReorder: ReorderAction;
onValueUpdate: (id: string) => void;
settings?: ListSettings;
2021-06-15 15:15:14 +00:00
onUpdateListSettings?: ListSettingsUpdate;
pageInfo: {
hasNextPage: boolean;
hasPreviousPage: boolean;
};
onNextPage: () => void;
onPreviousPage: () => void;
children: (data: AttributePageFormData) => React.ReactNode;
2019-08-09 10:17:04 +00:00
}
export interface AttributePageFormData extends MetadataFormData {
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
type: AttributeTypeEnum;
2019-08-12 13:08:16 +00:00
availableInGrid: boolean;
2019-08-09 10:17:04 +00:00
filterableInDashboard: boolean;
inputType: AttributeInputTypeEnum;
entityType: AttributeEntityTypeEnum | null;
2019-08-09 10:17:04 +00:00
filterableInStorefront: boolean;
name: string;
slug: string;
storefrontSearchPosition: string;
valueRequired: boolean;
unit: MeasurementUnitsEnum | null | undefined;
2019-08-09 10:17:04 +00:00
visibleInStorefront: boolean;
}
const AttributePage: React.FC<AttributePageProps> = ({
attribute,
disabled,
errors: apiErrors,
2019-08-09 10:17:04 +00:00
saveButtonBarState,
values,
onDelete,
onSubmit,
onValueAdd,
onValueDelete,
onValueReorder,
onValueUpdate,
settings,
onUpdateListSettings,
pageInfo,
onNextPage,
onPreviousPage,
children,
2019-08-09 10:17:04 +00:00
}) => {
const intl = useIntl();
const navigate = useNavigator();
const {
isMetadataModified,
isPrivateMetadataModified,
makeChangeHandler: makeMetadataChangeHandler,
} = useMetadataChangeTrigger();
const initialForm: AttributePageFormData = !attribute
? {
availableInGrid: true,
entityType: null,
filterableInDashboard: true,
filterableInStorefront: true,
inputType: AttributeInputTypeEnum.DROPDOWN,
metadata: [],
name: "",
privateMetadata: [],
slug: "",
storefrontSearchPosition: "",
type: AttributeTypeEnum.PRODUCT_TYPE,
valueRequired: true,
visibleInStorefront: true,
unit: undefined,
}
: {
availableInGrid: attribute.availableInGrid,
entityType: attribute.entityType,
filterableInDashboard: attribute.filterableInDashboard,
filterableInStorefront: attribute.filterableInStorefront,
inputType: attribute?.inputType ?? AttributeInputTypeEnum.DROPDOWN,
metadata: attribute.metadata.map(mapMetadataItemToInput),
name: attribute?.name ?? "",
privateMetadata: attribute.privateMetadata.map(mapMetadataItemToInput),
slug: attribute?.slug ?? "",
storefrontSearchPosition: attribute.storefrontSearchPosition.toString(),
type: attribute?.type ?? AttributeTypeEnum.PRODUCT_TYPE,
valueRequired: !!attribute.valueRequired,
visibleInStorefront: attribute.visibleInStorefront,
unit: attribute?.unit ?? null,
};
2019-08-09 10:17:04 +00:00
const handleSubmit = (data: AttributePageFormData) => {
const metadata = !attribute || isMetadataModified ? data.metadata : [];
const type = attribute === null ? data.type : undefined;
2020-09-02 10:12:32 +00:00
const privateMetadata =
!attribute || isPrivateMetadataModified ? data.privateMetadata : [];
2020-10-22 11:33:29 +00:00
return onSubmit({
2019-08-09 10:17:04 +00:00
...data,
metadata,
privateMetadata,
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
slug: data.slug || slugify(data.name).toLowerCase(),
type,
2019-08-09 10:17:04 +00:00
});
};
2019-08-09 10:17:04 +00:00
return (
<Form
confirmLeave
initial={initialForm}
onSubmit={handleSubmit}
disabled={disabled}
>
{({
change,
set,
data,
isSaveDisabled,
submit,
errors,
setError,
clearErrors,
}) => {
const changeMetadata = makeMetadataChangeHandler(change);
return (
<>
<Container>
<Backlink href={attributeListUrl()}>
{intl.formatMessage(sectionNames.attributes)}
</Backlink>
<PageHeader
title={
!attribute
? intl.formatMessage({
id: "8cUEPV",
defaultMessage: "Create New Attribute",
description: "page title",
})
: attribute.name
}
/>
<Grid>
<div>
<AttributeDetails
canChangeType={attribute === null}
data={data}
disabled={disabled}
apiErrors={apiErrors}
onChange={change}
set={set}
errors={errors}
setError={setError}
clearErrors={clearErrors}
/>
{ATTRIBUTE_TYPES_WITH_DEDICATED_VALUES.includes(
data.inputType,
) && (
<>
<CardSpacer />
<AttributeValues
inputType={data.inputType}
disabled={disabled}
values={mapEdgesToItems(values) ?? []}
onValueAdd={onValueAdd}
onValueDelete={onValueDelete}
onValueReorder={onValueReorder}
onValueUpdate={onValueUpdate}
settings={settings}
onUpdateListSettings={onUpdateListSettings}
pageInfo={pageInfo}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
/>
</>
)}
<CardSpacer />
<Metadata data={data} onChange={changeMetadata} />
</div>
<div>
<AttributeOrganization
canChangeType={attribute === null}
data={data}
disabled={disabled}
onChange={change}
/>
<CardSpacer />
<AttributeProperties
data={data}
errors={apiErrors}
disabled={disabled}
onChange={change}
/>
</div>
</Grid>
<Savebar
disabled={!!isSaveDisabled}
state={saveButtonBarState}
onCancel={() => navigate(attributeListUrl())}
onSubmit={submit}
onDelete={attribute === null ? undefined : onDelete}
/>
</Container>
{children(data)}
</>
);
}}
2019-08-09 10:17:04 +00:00
</Form>
);
};
AttributePage.displayName = "AttributePage";
export default AttributePage;