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

202 lines
6.4 KiB
TypeScript
Raw Normal View History

2019-08-09 10:17:04 +00:00
import AppHeader from "@saleor/components/AppHeader";
import CardSpacer from "@saleor/components/CardSpacer";
import { ConfirmButtonTransitionState } from "@saleor/components/ConfirmButton";
import Container from "@saleor/components/Container";
import Form from "@saleor/components/Form";
import Grid from "@saleor/components/Grid";
import Metadata from "@saleor/components/Metadata/Metadata";
import { MetadataFormData } from "@saleor/components/Metadata/types";
2019-08-09 10:17:04 +00:00
import PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
import {
AttributeDetailsFragment,
AttributeDetailsFragment_values
} from "@saleor/fragments/types/AttributeDetailsFragment";
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
import { sectionNames } 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 { ReorderAction } from "@saleor/types";
2019-08-09 10:17:04 +00:00
import { AttributeInputTypeEnum } from "@saleor/types/globalTypes";
import { mapMetadataItemToInput } from "@saleor/utils/maps";
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
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";
import AttributeProperties from "../AttributeProperties";
import AttributeValues from "../AttributeValues";
export interface AttributePageProps {
attribute: AttributeDetailsFragment | null;
disabled: boolean;
2020-03-24 14:05:26 +00:00
errors: ProductErrorFragment[];
2019-08-09 10:17:04 +00:00
saveButtonBarState: ConfirmButtonTransitionState;
values: AttributeDetailsFragment_values[];
onBack: () => void;
onDelete: () => void;
onSubmit: (data: AttributePageFormData) => void;
onValueAdd: () => void;
onValueDelete: (id: string) => void;
onValueReorder: ReorderAction;
onValueUpdate: (id: string) => void;
}
export interface AttributePageFormData extends MetadataFormData {
2019-08-12 13:08:16 +00:00
availableInGrid: boolean;
2019-08-09 10:17:04 +00:00
filterableInDashboard: boolean;
inputType: AttributeInputTypeEnum;
filterableInStorefront: boolean;
name: string;
slug: string;
storefrontSearchPosition: string;
valueRequired: boolean;
visibleInStorefront: boolean;
}
const AttributePage: React.FC<AttributePageProps> = ({
attribute,
disabled,
errors,
saveButtonBarState,
values,
onBack,
onDelete,
onSubmit,
onValueAdd,
onValueDelete,
onValueReorder,
onValueUpdate
}) => {
const intl = useIntl();
const {
isMetadataModified,
isPrivateMetadataModified,
makeChangeHandler: makeMetadataChangeHandler
} = useMetadataChangeTrigger();
2019-08-09 10:17:04 +00:00
const initialForm: AttributePageFormData =
attribute === null
? {
2019-08-12 13:08:16 +00:00
availableInGrid: true,
2019-08-09 10:17:04 +00:00
filterableInDashboard: true,
filterableInStorefront: true,
inputType: AttributeInputTypeEnum.DROPDOWN,
metadata: [],
2019-08-09 10:17:04 +00:00
name: "",
privateMetadata: [],
2019-08-09 10:17:04 +00:00
slug: "",
storefrontSearchPosition: "",
valueRequired: true,
visibleInStorefront: true
}
: {
2019-08-12 13:08:16 +00:00
availableInGrid: maybe(() => attribute.availableInGrid, true),
2019-08-09 10:17:04 +00:00
filterableInDashboard: maybe(
() => attribute.filterableInDashboard,
true
),
filterableInStorefront: maybe(
() => attribute.filterableInStorefront,
true
),
inputType: maybe(
() => attribute.inputType,
AttributeInputTypeEnum.DROPDOWN
),
metadata: attribute?.metadata?.map(mapMetadataItemToInput),
2019-08-09 10:17:04 +00:00
name: maybe(() => attribute.name, ""),
privateMetadata: attribute?.privateMetadata?.map(
mapMetadataItemToInput
),
2019-08-09 10:17:04 +00:00
slug: maybe(() => attribute.slug, ""),
storefrontSearchPosition: maybe(
() => attribute.storefrontSearchPosition.toString(),
""
),
valueRequired: maybe(() => attribute.valueRequired, true),
visibleInStorefront: maybe(() => attribute.visibleInStorefront, true)
};
const handleSubmit = (data: AttributePageFormData) => {
2020-09-02 10:12:32 +00:00
const metadata =
!attribute || isMetadataModified ? data.metadata : undefined;
const privateMetadata =
!attribute || isPrivateMetadataModified
? data.privateMetadata
: undefined;
2020-10-22 11:33:29 +00:00
return onSubmit({
2019-08-09 10:17:04 +00:00
...data,
metadata,
privateMetadata,
2019-08-09 10:17:04 +00:00
slug: data.slug || slugify(data.name).toLowerCase()
});
};
2019-08-09 10:17:04 +00:00
return (
2020-02-24 14:14:48 +00:00
<Form initial={initialForm} onSubmit={handleSubmit}>
{({ change, data, hasChanged, submit }) => {
const changeMetadata = makeMetadataChangeHandler(change);
return (
<Container>
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.attributes)}
</AppHeader>
<PageHeader
title={
attribute === null
? intl.formatMessage({
defaultMessage: "Create New Attribute",
description: "page title"
})
: maybe(() => attribute.name)
}
/>
<Grid>
<div>
<AttributeDetails
canChangeType={attribute === null}
data={data}
disabled={disabled}
errors={errors}
onChange={change}
/>
<CardSpacer />
<AttributeValues
disabled={disabled}
values={values}
onValueAdd={onValueAdd}
onValueDelete={onValueDelete}
onValueReorder={onValueReorder}
onValueUpdate={onValueUpdate}
/>
<CardSpacer />
<Metadata data={data} onChange={changeMetadata} />
</div>
<div>
<AttributeProperties
data={data}
errors={errors}
disabled={disabled}
onChange={change}
/>
</div>
</Grid>
<SaveButtonBar
disabled={disabled || !hasChanged}
state={saveButtonBarState}
onCancel={onBack}
onSave={submit}
onDelete={attribute === null ? undefined : onDelete}
/>
</Container>
);
}}
2019-08-09 10:17:04 +00:00
</Form>
);
};
AttributePage.displayName = "AttributePage";
export default AttributePage;