2019-06-19 14:40:52 +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";
|
2020-10-20 11:20:55 +00:00
|
|
|
import Metadata, { MetadataFormData } from "@saleor/components/Metadata";
|
2019-06-19 14:40:52 +00:00
|
|
|
import PageHeader from "@saleor/components/PageHeader";
|
|
|
|
import SaveButtonBar from "@saleor/components/SaveButtonBar";
|
|
|
|
import SeoForm from "@saleor/components/SeoForm";
|
|
|
|
import VisibilityCard from "@saleor/components/VisibilityCard";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { PageErrorFragment } from "@saleor/fragments/types/PageErrorFragment";
|
2019-09-16 01:36:04 +00:00
|
|
|
import useDateLocalize from "@saleor/hooks/useDateLocalize";
|
2020-11-02 13:26:02 +00:00
|
|
|
import { SubmitPromise } from "@saleor/hooks/useForm";
|
2019-08-26 17:48:13 +00:00
|
|
|
import { sectionNames } from "@saleor/intl";
|
2020-10-20 11:20:55 +00:00
|
|
|
import { mapMetadataItemToInput } from "@saleor/utils/maps";
|
|
|
|
import useMetadataChangeTrigger from "@saleor/utils/metadata/useMetadataChangeTrigger";
|
2020-05-14 09:30:32 +00:00
|
|
|
import {
|
|
|
|
ContentState,
|
|
|
|
convertFromRaw,
|
|
|
|
convertToRaw,
|
|
|
|
RawDraftContentState
|
|
|
|
} from "draft-js";
|
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import { maybe } from "../../../misc";
|
|
|
|
import { PageDetails_page } from "../../types/PageDetails";
|
|
|
|
import PageInfo from "../PageInfo";
|
|
|
|
|
2020-10-22 11:33:29 +00:00
|
|
|
export interface PageDetailsPageFormData extends MetadataFormData {
|
2019-06-19 14:40:52 +00:00
|
|
|
content: RawDraftContentState;
|
|
|
|
isPublished: boolean;
|
|
|
|
publicationDate: string;
|
|
|
|
seoDescription: string;
|
|
|
|
seoTitle: string;
|
|
|
|
slug: string;
|
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface PageDetailsPageProps {
|
|
|
|
disabled: boolean;
|
2020-03-17 10:57:02 +00:00
|
|
|
errors: PageErrorFragment[];
|
2019-06-19 14:40:52 +00:00
|
|
|
page: PageDetails_page;
|
2020-09-25 14:21:10 +00:00
|
|
|
allowEmptySlug?: boolean;
|
2019-06-19 14:40:52 +00:00
|
|
|
saveButtonBarState: ConfirmButtonTransitionState;
|
|
|
|
onBack: () => void;
|
|
|
|
onRemove: () => void;
|
2020-11-02 13:26:02 +00:00
|
|
|
onSubmit: (data: PageDetailsPageFormData) => SubmitPromise;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
const PageDetailsPage: React.FC<PageDetailsPageProps> = ({
|
2019-06-19 14:40:52 +00:00
|
|
|
disabled,
|
|
|
|
errors,
|
|
|
|
page,
|
|
|
|
saveButtonBarState,
|
|
|
|
onBack,
|
|
|
|
onRemove,
|
|
|
|
onSubmit
|
|
|
|
}) => {
|
2019-08-26 17:48:13 +00:00
|
|
|
const intl = useIntl();
|
2019-09-16 01:36:04 +00:00
|
|
|
const localizeDate = useDateLocalize();
|
2020-10-20 11:20:55 +00:00
|
|
|
const {
|
|
|
|
isMetadataModified,
|
|
|
|
isPrivateMetadataModified,
|
|
|
|
makeChangeHandler: makeMetadataChangeHandler
|
|
|
|
} = useMetadataChangeTrigger();
|
|
|
|
|
2020-09-25 14:21:10 +00:00
|
|
|
const pageExists = page !== null;
|
2019-08-26 17:48:13 +00:00
|
|
|
|
2020-10-22 11:33:29 +00:00
|
|
|
const initialForm: PageDetailsPageFormData = {
|
2019-06-19 14:40:52 +00:00
|
|
|
content: maybe(
|
|
|
|
() => JSON.parse(page.contentJson),
|
|
|
|
convertToRaw(ContentState.createFromText(""))
|
|
|
|
),
|
2020-10-20 11:20:55 +00:00
|
|
|
isPublished: page?.isPublished,
|
|
|
|
metadata: pageExists ? page?.metadata?.map(mapMetadataItemToInput) : [],
|
|
|
|
privateMetadata: pageExists
|
|
|
|
? page?.privateMetadata?.map(mapMetadataItemToInput)
|
|
|
|
: [],
|
|
|
|
publicationDate: page?.publicationDate || "",
|
|
|
|
seoDescription: page?.seoDescription || "",
|
|
|
|
seoTitle: page?.seoTitle || "",
|
|
|
|
slug: page?.slug || "",
|
|
|
|
title: page?.title || ""
|
2019-06-19 14:40:52 +00:00
|
|
|
};
|
2020-10-19 10:28:43 +00:00
|
|
|
|
2020-10-28 13:22:42 +00:00
|
|
|
const handleSubmit = (data: PageDetailsPageFormData) => {
|
2020-10-20 11:20:55 +00:00
|
|
|
const metadata = isMetadataModified ? data.metadata : undefined;
|
|
|
|
const privateMetadata = isPrivateMetadataModified
|
|
|
|
? data.privateMetadata
|
|
|
|
: undefined;
|
2020-10-19 10:28:43 +00:00
|
|
|
|
2020-10-28 13:22:42 +00:00
|
|
|
return onSubmit({
|
2020-10-20 11:20:55 +00:00
|
|
|
...data,
|
|
|
|
isPublished: data.isPublished || !!data.publicationDate,
|
|
|
|
metadata,
|
|
|
|
privateMetadata
|
|
|
|
});
|
|
|
|
};
|
2020-10-19 10:28:43 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2020-10-19 10:28:43 +00:00
|
|
|
<Form initial={initialForm} onSubmit={handleSubmit}>
|
2020-10-20 11:20:55 +00:00
|
|
|
{({ change, data, hasChanged, submit }) => {
|
|
|
|
const changeMetadata = makeMetadataChangeHandler(change);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Container>
|
|
|
|
<AppHeader onBack={onBack}>
|
|
|
|
{intl.formatMessage(sectionNames.pages)}
|
|
|
|
</AppHeader>
|
|
|
|
<PageHeader
|
|
|
|
title={
|
|
|
|
!pageExists
|
|
|
|
? intl.formatMessage({
|
|
|
|
defaultMessage: "Create Page",
|
|
|
|
description: "page header"
|
|
|
|
})
|
|
|
|
: maybe(() => page.title)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<Grid>
|
|
|
|
<div>
|
|
|
|
<PageInfo
|
|
|
|
data={data}
|
|
|
|
disabled={disabled}
|
|
|
|
errors={errors}
|
|
|
|
page={page}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<SeoForm
|
|
|
|
errors={errors}
|
|
|
|
allowEmptySlug={!pageExists}
|
|
|
|
description={data.seoDescription}
|
|
|
|
disabled={disabled}
|
|
|
|
descriptionPlaceholder={maybe(
|
|
|
|
() =>
|
|
|
|
convertFromRaw(data.content)
|
|
|
|
.getPlainText()
|
|
|
|
.slice(0, 300),
|
|
|
|
""
|
|
|
|
)}
|
|
|
|
onChange={change}
|
|
|
|
slug={data.slug}
|
|
|
|
slugPlaceholder={data.title}
|
|
|
|
title={data.seoTitle}
|
|
|
|
titlePlaceholder={data.title}
|
|
|
|
helperText={intl.formatMessage({
|
|
|
|
defaultMessage:
|
|
|
|
"Add search engine title and description to make this page easier to find"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<CardSpacer />
|
|
|
|
<Metadata data={data} onChange={changeMetadata} />
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<CardSpacer />
|
|
|
|
<VisibilityCard
|
|
|
|
data={data}
|
|
|
|
errors={errors}
|
|
|
|
disabled={disabled}
|
|
|
|
messages={{
|
|
|
|
hiddenLabel: intl.formatMessage({
|
|
|
|
defaultMessage: "Hidden",
|
|
|
|
description: "page label"
|
|
|
|
}),
|
|
|
|
hiddenSecondLabel: intl.formatMessage(
|
|
|
|
{
|
|
|
|
defaultMessage: "will be visible from {date}",
|
|
|
|
description: "page"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
date: localizeDate(data.publicationDate, "L")
|
|
|
|
}
|
|
|
|
),
|
|
|
|
visibleLabel: intl.formatMessage({
|
|
|
|
defaultMessage: "Visible",
|
|
|
|
description: "page label"
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
onChange={change}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Grid>
|
|
|
|
<SaveButtonBar
|
|
|
|
disabled={disabled || !hasChanged}
|
|
|
|
state={saveButtonBarState}
|
|
|
|
onCancel={onBack}
|
|
|
|
onDelete={page === null ? undefined : onRemove}
|
|
|
|
onSave={submit}
|
|
|
|
/>
|
|
|
|
</Container>
|
|
|
|
);
|
|
|
|
}}
|
2019-06-19 14:40:52 +00:00
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
PageDetailsPage.displayName = "PageDetailsPage";
|
|
|
|
export default PageDetailsPage;
|