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

161 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-08-09 10:17:04 +00:00
import React from "react";
import slugify from "slugify";
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 PageHeader from "@saleor/components/PageHeader";
import SaveButtonBar from "@saleor/components/SaveButtonBar";
import i18n from "@saleor/i18n";
import { maybe } from "@saleor/misc";
import { ReorderAction, UserError } from "@saleor/types";
import { AttributeInputTypeEnum } from "@saleor/types/globalTypes";
import {
AttributeDetailsFragment,
AttributeDetailsFragment_values
} from "../../types/AttributeDetailsFragment";
import AttributeDetails from "../AttributeDetails";
import AttributeProperties from "../AttributeProperties";
import AttributeValues from "../AttributeValues";
export interface AttributePageProps {
attribute: AttributeDetailsFragment | null;
disabled: boolean;
errors: UserError[];
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 {
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 initialForm: AttributePageFormData =
attribute === null
? {
filterableInDashboard: true,
filterableInStorefront: true,
inputType: AttributeInputTypeEnum.DROPDOWN,
name: "",
slug: "",
storefrontSearchPosition: "",
valueRequired: true,
visibleInStorefront: true
}
: {
filterableInDashboard: maybe(
() => attribute.filterableInDashboard,
true
),
filterableInStorefront: maybe(
() => attribute.filterableInStorefront,
true
),
inputType: maybe(
() => attribute.inputType,
AttributeInputTypeEnum.DROPDOWN
),
name: maybe(() => attribute.name, ""),
slug: maybe(() => attribute.slug, ""),
storefrontSearchPosition: maybe(
() => attribute.storefrontSearchPosition.toString(),
""
),
valueRequired: maybe(() => attribute.valueRequired, true),
visibleInStorefront: maybe(() => attribute.visibleInStorefront, true)
};
const handleSubmit = (data: AttributePageFormData) =>
onSubmit({
...data,
slug: data.slug || slugify(data.name).toLowerCase()
});
return (
<Form errors={errors} initial={initialForm} onSubmit={handleSubmit}>
{({ change, errors: formErrors, data, submit }) => (
<Container>
<AppHeader onBack={onBack}>{i18n.t("Attributes")}</AppHeader>
<PageHeader
title={
attribute === null
? i18n.t("Create New Attribute", {
context: "page title"
})
: maybe(() => attribute.name)
}
/>
<Grid>
<div>
<AttributeDetails
canChangeType={attribute === null}
data={data}
disabled={disabled}
errors={formErrors}
onChange={change}
/>
<CardSpacer />
<AttributeValues
disabled={disabled}
values={values}
onValueAdd={onValueAdd}
onValueDelete={onValueDelete}
onValueReorder={onValueReorder}
onValueUpdate={onValueUpdate}
/>
</div>
<div>
<AttributeProperties
data={data}
errors={formErrors}
disabled={disabled}
onChange={change}
/>
</div>
</Grid>
<SaveButtonBar
disabled={disabled}
state={saveButtonBarState}
onCancel={onBack}
onSave={submit}
onDelete={attribute === null ? undefined : onDelete}
/>
</Container>
)}
</Form>
);
};
AttributePage.displayName = "AttributePage";
export default AttributePage;