2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2020-03-05 14:59:55 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2020-11-04 10:48:49 +00:00
|
|
|
import getPublicationData from "@saleor/utils/data/getPublicationData";
|
2020-09-01 11:46:15 +00:00
|
|
|
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
|
|
|
|
import {
|
|
|
|
useMetadataUpdate,
|
|
|
|
usePrivateMetadataUpdate
|
|
|
|
} from "@saleor/utils/metadata/updateMetadata";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import { CollectionCreateInput } from "../../types/globalTypes";
|
2020-11-04 10:48:49 +00:00
|
|
|
import CollectionCreatePage from "../components/CollectionCreatePage/CollectionCreatePage";
|
|
|
|
import { CollectionCreateData } from "../components/CollectionCreatePage/form";
|
2020-08-28 12:45:11 +00:00
|
|
|
import { useCollectionCreateMutation } from "../mutations";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { collectionListUrl, collectionUrl } from "../urls";
|
|
|
|
|
2019-08-21 12:31:55 +00:00
|
|
|
export const CollectionCreate: React.FC = () => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-21 12:31:55 +00:00
|
|
|
const intl = useIntl();
|
2020-09-01 11:46:15 +00:00
|
|
|
const [updateMetadata] = useMetadataUpdate({});
|
|
|
|
const [updatePrivateMetadata] = usePrivateMetadataUpdate({});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-08-28 12:45:11 +00:00
|
|
|
const [createCollection, createCollectionOpts] = useCollectionCreateMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
if (data.collectionCreate.errors.length === 0) {
|
2019-06-19 14:40:52 +00:00
|
|
|
notify({
|
2020-08-28 12:45:11 +00:00
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-06-19 14:40:52 +00:00
|
|
|
});
|
2020-08-28 12:45:11 +00:00
|
|
|
navigate(collectionUrl(data.collectionCreate.collection.id));
|
|
|
|
} else {
|
|
|
|
const backgroundImageError = data.collectionCreate.errors.find(
|
|
|
|
error =>
|
|
|
|
error.field === ("backgroundImage" as keyof CollectionCreateInput)
|
|
|
|
);
|
|
|
|
if (backgroundImageError) {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: intl.formatMessage(commonMessages.somethingWentWrong)
|
|
|
|
});
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-28 12:45:11 +00:00
|
|
|
});
|
|
|
|
|
2020-11-04 10:48:49 +00:00
|
|
|
const handleCreate = async (formData: CollectionCreateData) => {
|
2020-09-01 11:46:15 +00:00
|
|
|
const result = await createCollection({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
backgroundImage: formData.backgroundImage.value,
|
|
|
|
backgroundImageAlt: formData.backgroundImageAlt,
|
|
|
|
descriptionJson: JSON.stringify(formData.description),
|
|
|
|
name: formData.name,
|
|
|
|
seo: {
|
|
|
|
description: formData.seoDescription,
|
|
|
|
title: formData.seoTitle
|
2020-10-22 11:03:02 +00:00
|
|
|
},
|
|
|
|
...getPublicationData(formData)
|
2020-09-01 11:46:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.data?.collectionCreate.collection?.id || null;
|
|
|
|
};
|
2020-10-22 11:03:02 +00:00
|
|
|
|
2020-09-01 11:46:15 +00:00
|
|
|
const handleSubmit = createMetadataCreateHandler(
|
|
|
|
handleCreate,
|
|
|
|
updateMetadata,
|
|
|
|
updatePrivateMetadata
|
|
|
|
);
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2020-08-28 12:45:11 +00:00
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create collection",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<CollectionCreatePage
|
|
|
|
errors={createCollectionOpts.data?.collectionCreate.errors || []}
|
|
|
|
onBack={() => navigate(collectionListUrl())}
|
|
|
|
disabled={createCollectionOpts.loading}
|
2020-09-01 11:46:15 +00:00
|
|
|
onSubmit={handleSubmit}
|
2020-08-28 12:45:11 +00:00
|
|
|
saveButtonBarState={createCollectionOpts.status}
|
|
|
|
/>
|
|
|
|
</>
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
export default CollectionCreate;
|