2019-06-19 14:40:52 +00:00
|
|
|
import DialogContentText from "@material-ui/core/DialogContentText";
|
|
|
|
import ActionDialog from "@saleor/components/ActionDialog";
|
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2020-03-17 10:57:02 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import { getStringOrPlaceholder, maybe } from "../../misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { PageInput } from "../../types/globalTypes";
|
|
|
|
import PageDetailsPage, { FormData } from "../components/PageDetailsPage";
|
|
|
|
import { TypedPageRemove, TypedPageUpdate } from "../mutations";
|
|
|
|
import { TypedPageDetailsQuery } from "../queries";
|
|
|
|
import { PageRemove } from "../types/PageRemove";
|
|
|
|
import { pageListUrl, pageUrl, PageUrlQueryParams } from "../urls";
|
|
|
|
|
|
|
|
export interface PageDetailsProps {
|
|
|
|
id: string;
|
|
|
|
params: PageUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
2019-12-02 10:49:14 +00:00
|
|
|
const createPageInput = (data: FormData): PageInput => ({
|
|
|
|
contentJson: JSON.stringify(data.content),
|
|
|
|
isPublished: data.isPublished,
|
2020-10-19 10:28:43 +00:00
|
|
|
publicationDate: data.publicationDate,
|
2019-12-02 10:49:14 +00:00
|
|
|
seo: {
|
|
|
|
description: data.seoDescription,
|
|
|
|
title: data.seoTitle
|
|
|
|
},
|
|
|
|
slug: data.slug === "" ? null : data.slug,
|
|
|
|
title: data.title
|
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const PageDetails: React.FC<PageDetailsProps> = ({ id, params }) => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-26 17:48:13 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handlePageRemove = (data: PageRemove) => {
|
|
|
|
if (data.pageDelete.errors.length === 0) {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2020-03-17 10:57:02 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
|
|
|
navigate(pageListUrl());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<TypedPageRemove variables={{ id }} onCompleted={handlePageRemove}>
|
|
|
|
{(pageRemove, pageRemoveOpts) => (
|
|
|
|
<TypedPageUpdate>
|
|
|
|
{(pageUpdate, pageUpdateOpts) => (
|
|
|
|
<TypedPageDetailsQuery variables={{ id }}>
|
2019-12-06 17:11:46 +00:00
|
|
|
{pageDetails => (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={maybe(() => pageDetails.data.page.title)}
|
|
|
|
/>
|
|
|
|
<PageDetailsPage
|
|
|
|
disabled={pageDetails.loading}
|
2020-03-17 10:57:02 +00:00
|
|
|
errors={pageUpdateOpts.data?.pageUpdate.errors || []}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonBarState={pageUpdateOpts.status}
|
2020-03-17 10:57:02 +00:00
|
|
|
page={pageDetails.data?.page}
|
2019-12-06 17:11:46 +00:00
|
|
|
onBack={() => navigate(pageListUrl())}
|
|
|
|
onRemove={() =>
|
|
|
|
navigate(
|
|
|
|
pageUrl(id, {
|
|
|
|
action: "remove"
|
2019-06-19 14:40:52 +00:00
|
|
|
})
|
2019-12-06 17:11:46 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
onSubmit={formData =>
|
|
|
|
pageUpdate({
|
|
|
|
variables: {
|
|
|
|
id,
|
|
|
|
input: createPageInput(formData)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<ActionDialog
|
|
|
|
open={params.action === "remove"}
|
2019-12-06 17:17:44 +00:00
|
|
|
confirmButtonState={pageRemoveOpts.status}
|
2019-12-06 17:11:46 +00:00
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Delete Page",
|
|
|
|
description: "dialog header"
|
|
|
|
})}
|
|
|
|
onClose={() => navigate(pageUrl(id))}
|
|
|
|
onConfirm={pageRemove}
|
|
|
|
variant="delete"
|
|
|
|
>
|
|
|
|
<DialogContentText>
|
|
|
|
<FormattedMessage
|
|
|
|
defaultMessage="Are you sure you want to delete {title}?"
|
|
|
|
description="delete page"
|
|
|
|
values={{
|
|
|
|
title: (
|
|
|
|
<strong>
|
2020-03-17 10:57:02 +00:00
|
|
|
{getStringOrPlaceholder(
|
|
|
|
pageDetails.data?.page?.title
|
|
|
|
)}
|
2019-12-06 17:11:46 +00:00
|
|
|
</strong>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContentText>
|
|
|
|
</ActionDialog>
|
|
|
|
</>
|
|
|
|
)}
|
2019-06-19 14:40:52 +00:00
|
|
|
</TypedPageDetailsQuery>
|
|
|
|
)}
|
|
|
|
</TypedPageUpdate>
|
|
|
|
)}
|
|
|
|
</TypedPageRemove>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
PageDetails.displayName = "PageDetails";
|
|
|
|
export default PageDetails;
|