saleor-dashboard/src/collections/components/CollectionCreatePage/CollectionCreatePage.tsx

190 lines
6.1 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
import AppHeader from "@saleor/components/AppHeader";
import { CardSpacer } from "@saleor/components/CardSpacer";
import CardTitle from "@saleor/components/CardTitle";
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 SeoForm from "@saleor/components/SeoForm";
import VisibilityCard from "@saleor/components/VisibilityCard";
import { ProductErrorFragment } from "@saleor/fragments/types/ProductErrorFragment";
import useDateLocalize from "@saleor/hooks/useDateLocalize";
import { commonMessages, sectionNames } from "@saleor/intl";
import { ContentState, convertToRaw, RawDraftContentState } from "draft-js";
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import CollectionDetails from "../CollectionDetails/CollectionDetails";
import { CollectionImage } from "../CollectionImage/CollectionImage";
export interface CollectionCreatePageFormData {
backgroundImage: {
url: string;
value: string;
};
backgroundImageAlt: string;
description: RawDraftContentState;
name: string;
publicationDate: string;
isPublished: boolean;
seoDescription: string;
seoTitle: string;
}
export interface CollectionCreatePageProps {
disabled: boolean;
2020-03-05 14:59:55 +00:00
errors: ProductErrorFragment[];
2019-06-19 14:40:52 +00:00
saveButtonBarState: ConfirmButtonTransitionState;
onBack: () => void;
onSubmit: (data: CollectionCreatePageFormData) => void;
}
const initialForm: CollectionCreatePageFormData = {
backgroundImage: {
url: null,
value: null
},
backgroundImageAlt: "",
description: convertToRaw(ContentState.createFromText("")),
isPublished: false,
name: "",
publicationDate: "",
seoDescription: "",
seoTitle: ""
};
const CollectionCreatePage: React.FC<CollectionCreatePageProps> = ({
2019-06-19 14:40:52 +00:00
disabled,
errors,
saveButtonBarState,
onBack,
onSubmit
}: CollectionCreatePageProps) => {
const intl = useIntl();
const localizeDate = useDateLocalize();
return (
2020-02-24 14:14:48 +00:00
<Form initial={initialForm} onSubmit={onSubmit}>
{({ change, data, hasChanged, submit }) => (
<Container>
<AppHeader onBack={onBack}>
{intl.formatMessage(sectionNames.collections)}
</AppHeader>
<PageHeader
title={intl.formatMessage({
2019-10-09 15:33:55 +00:00
defaultMessage: "Add Collection",
2019-08-22 16:25:55 +00:00
description: "page header"
})}
/>
<Grid>
<div>
<CollectionDetails
data={data}
disabled={disabled}
2020-02-24 14:14:48 +00:00
errors={errors}
onChange={change}
/>
<CardSpacer />
<CollectionImage
image={
data.backgroundImage.url
? {
__typename: "Image",
alt: data.backgroundImageAlt,
url: data.backgroundImage.url
}
: null
}
onImageDelete={() =>
change({
target: {
name: "backgroundImage",
value: {
url: null,
value: null
}
2019-06-19 14:40:52 +00:00
}
} as any)
}
onImageUpload={file =>
change({
target: {
name: "backgroundImage",
value: {
url: URL.createObjectURL(file),
value: file
}
2019-06-19 14:40:52 +00:00
}
} as any)
2019-06-19 14:40:52 +00:00
}
onChange={change}
data={data}
/>
<CardSpacer />
<SeoForm
description={data.seoDescription}
disabled={disabled}
descriptionPlaceholder=""
helperText={intl.formatMessage({
defaultMessage:
2019-08-22 16:25:55 +00:00
"Add search engine title and description to make this collection easier to find"
})}
title={data.seoTitle}
titlePlaceholder={data.name}
onChange={change}
/>
</div>
2019-06-19 14:40:52 +00:00
<div>
<div>
<Card>
<CardTitle
title={intl.formatMessage(commonMessages.availability)}
2019-06-19 14:40:52 +00:00
/>
<CardContent>
<VisibilityCard
data={data}
2020-02-24 14:14:48 +00:00
errors={errors}
disabled={disabled}
hiddenMessage={intl.formatMessage(
{
defaultMessage: "will be visible from {date}",
description: "collection"
},
{
date: localizeDate(data.publicationDate)
}
)}
onChange={change}
visibleMessage={intl.formatMessage(
{
defaultMessage: "since {date}",
description: "collection"
},
{
date: localizeDate(data.publicationDate)
}
)}
/>
</CardContent>
</Card>
</div>
2019-06-19 14:40:52 +00:00
</div>
</Grid>
<SaveButtonBar
state={saveButtonBarState}
disabled={disabled || !hasChanged}
onCancel={onBack}
onSave={submit}
/>
</Container>
)}
</Form>
);
};
2019-06-19 14:40:52 +00:00
CollectionCreatePage.displayName = "CollectionCreatePage";
export default CollectionCreatePage;