saleor-dashboard/src/products/views/ProductCreate.tsx

200 lines
8.4 KiB
TypeScript
Raw Normal View History

2019-08-09 10:26:22 +00:00
import React from "react";
import { useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
2019-09-30 10:55:22 +00:00
import SearchProductTypes from "@saleor/containers/SearchProductTypes";
2019-06-19 14:40:52 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import useShop from "@saleor/hooks/useShop";
import { DEFAULT_INITIAL_SEARCH_DATA } from "../../config";
import SearchCategories from "../../containers/SearchCategories";
import SearchCollections from "../../containers/SearchCollections";
import { decimal, getMutationState, maybe } from "../../misc";
2019-08-09 11:14:35 +00:00
import ProductCreatePage, {
ProductCreatePageSubmitData
} from "../components/ProductCreatePage";
2019-06-19 14:40:52 +00:00
import { TypedProductCreateMutation } from "../mutations";
import { ProductCreate } from "../types/ProductCreate";
import { productListUrl, productUrl } from "../urls";
interface ProductUpdateProps {
id: string;
}
export const ProductUpdate: React.FC<ProductUpdateProps> = () => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const handleBack = () => navigate(productListUrl());
return (
<SearchCategories variables={DEFAULT_INITIAL_SEARCH_DATA}>
2019-10-15 13:11:38 +00:00
{({
loadMore: loadMoreCategories,
search: searchCategory,
result: searchCategoryOpts
}) => (
2019-06-19 14:40:52 +00:00
<SearchCollections variables={DEFAULT_INITIAL_SEARCH_DATA}>
2019-10-15 13:11:38 +00:00
{({
loadMore: loadMoreCollections,
search: searchCollection,
result: searchCollectionOpts
}) => (
2019-09-30 10:55:22 +00:00
<SearchProductTypes variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({
2019-10-15 13:11:38 +00:00
loadMore: loadMoreProductTypes,
2019-09-30 10:55:22 +00:00
search: searchProductTypes,
result: searchProductTypesOpts
}) => {
2019-06-19 14:40:52 +00:00
const handleSuccess = (data: ProductCreate) => {
if (data.productCreate.errors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Product created"
})
2019-06-19 14:40:52 +00:00
});
navigate(productUrl(data.productCreate.product.id));
2019-08-09 11:14:35 +00:00
} else {
const attributeError = data.productCreate.errors.find(
err => err.field === "attributes"
);
if (!!attributeError) {
notify({ text: attributeError.message });
}
2019-06-19 14:40:52 +00:00
}
};
return (
<TypedProductCreateMutation onCompleted={handleSuccess}>
{(
productCreate,
{
called: productCreateCalled,
data: productCreateData,
loading: productCreateDataLoading
}
) => {
2019-08-09 11:14:35 +00:00
const handleSubmit = (
formData: ProductCreatePageSubmitData
) => {
2019-06-19 14:40:52 +00:00
productCreate({
variables: {
2019-08-09 11:14:35 +00:00
attributes: formData.attributes.map(attribute => ({
id: attribute.id,
values: attribute.value
})),
2019-06-19 14:40:52 +00:00
basePrice: decimal(formData.basePrice),
2019-08-09 11:14:35 +00:00
category: formData.category,
2019-06-19 14:40:52 +00:00
chargeTaxes: formData.chargeTaxes,
2019-08-09 11:14:35 +00:00
collections: formData.collections,
2019-06-19 14:40:52 +00:00
descriptionJson: JSON.stringify(
formData.description
),
isPublished: formData.isPublished,
name: formData.name,
2019-08-09 11:14:35 +00:00
productType: formData.productType,
2019-06-19 14:40:52 +00:00
publicationDate:
formData.publicationDate !== ""
? formData.publicationDate
: null,
2019-08-09 11:14:35 +00:00
seo: {
description: formData.seoDescription,
title: formData.seoTitle
},
2019-06-19 14:40:52 +00:00
sku: formData.sku,
stockQuantity:
formData.stockQuantity !== null
? formData.stockQuantity
: 0
}
});
};
const formTransitionState = getMutationState(
productCreateCalled,
productCreateDataLoading,
maybe(() => productCreateData.productCreate.errors)
);
return (
<>
<WindowTitle
title={intl.formatMessage({
defaultMessage: "Create Product",
description: "window title"
})}
/>
2019-06-19 14:40:52 +00:00
<ProductCreatePage
currency={maybe(() => shop.defaultCurrency)}
categories={maybe(
2019-10-15 12:17:35 +00:00
() => searchCategoryOpts.data.search.edges,
2019-06-19 14:40:52 +00:00
[]
).map(edge => edge.node)}
collections={maybe(
2019-10-15 12:17:35 +00:00
() => searchCollectionOpts.data.search.edges,
2019-06-19 14:40:52 +00:00
[]
).map(edge => edge.node)}
2019-09-30 10:55:22 +00:00
disabled={productCreateDataLoading}
2019-06-19 14:40:52 +00:00
errors={maybe(
() => productCreateData.productCreate.errors,
[]
)}
fetchCategories={searchCategory}
fetchCollections={searchCollection}
2019-09-30 10:55:22 +00:00
fetchProductTypes={searchProductTypes}
header={intl.formatMessage({
defaultMessage: "New Product",
description: "page header"
})}
2019-06-19 14:40:52 +00:00
productTypes={maybe(() =>
2019-10-15 12:17:35 +00:00
searchProductTypesOpts.data.search.edges.map(
2019-09-30 10:55:22 +00:00
edge => edge.node
)
2019-06-19 14:40:52 +00:00
)}
onBack={handleBack}
onSubmit={handleSubmit}
saveButtonBarState={formTransitionState}
2019-10-15 13:11:38 +00:00
fetchMoreCategories={{
hasMore: maybe(
() =>
searchCategoryOpts.data.search.pageInfo
.hasNextPage
),
loading: searchCategoryOpts.loading,
onFetchMore: loadMoreCategories
}}
fetchMoreCollections={{
hasMore: maybe(
() =>
searchCollectionOpts.data.search.pageInfo
.hasNextPage
),
loading: searchCollectionOpts.loading,
onFetchMore: loadMoreCollections
}}
fetchMoreProductTypes={{
hasMore: maybe(
() =>
searchProductTypesOpts.data.search.pageInfo
.hasNextPage
),
loading: searchProductTypesOpts.loading,
onFetchMore: loadMoreProductTypes
}}
2019-06-19 14:40:52 +00:00
/>
</>
);
}}
</TypedProductCreateMutation>
);
}}
2019-09-30 10:55:22 +00:00
</SearchProductTypes>
2019-06-19 14:40:52 +00:00
)}
</SearchCollections>
)}
</SearchCategories>
);
};
export default ProductUpdate;