2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
2019-11-21 14:59:06 +00:00
|
|
|
import { DEFAULT_INITIAL_SEARCH_DATA } from "@saleor/config";
|
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";
|
2019-11-19 15:47:12 +00:00
|
|
|
import useCategorySearch from "@saleor/searches/useCategorySearch";
|
2019-11-19 16:04:53 +00:00
|
|
|
import useCollectionSearch from "@saleor/searches/useCollectionSearch";
|
2019-11-19 17:00:14 +00:00
|
|
|
import useProductTypeSearch from "@saleor/searches/useProductTypeSearch";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { useWarehouseList } from "@saleor/warehouses/queries";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2019-12-06 17:11:46 +00:00
|
|
|
import { decimal, 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";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { productListUrl, productUrl } from "../urls";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-05-04 15:29:06 +00:00
|
|
|
export const ProductCreateView: React.FC = () => {
|
2019-06-19 14:40:52 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const shop = useShop();
|
2019-08-26 17:53:22 +00:00
|
|
|
const intl = useIntl();
|
2019-11-19 15:47:12 +00:00
|
|
|
const {
|
|
|
|
loadMore: loadMoreCategories,
|
|
|
|
search: searchCategory,
|
|
|
|
result: searchCategoryOpts
|
|
|
|
} = useCategorySearch({
|
|
|
|
variables: DEFAULT_INITIAL_SEARCH_DATA
|
|
|
|
});
|
2019-11-19 16:04:53 +00:00
|
|
|
const {
|
|
|
|
loadMore: loadMoreCollections,
|
|
|
|
search: searchCollection,
|
|
|
|
result: searchCollectionOpts
|
|
|
|
} = useCollectionSearch({
|
|
|
|
variables: DEFAULT_INITIAL_SEARCH_DATA
|
|
|
|
});
|
2019-11-19 17:00:14 +00:00
|
|
|
const {
|
|
|
|
loadMore: loadMoreProductTypes,
|
|
|
|
search: searchProductTypes,
|
|
|
|
result: searchProductTypesOpts
|
|
|
|
} = useProductTypeSearch({
|
|
|
|
variables: DEFAULT_INITIAL_SEARCH_DATA
|
|
|
|
});
|
2020-05-04 15:29:06 +00:00
|
|
|
const warehouses = useWarehouseList({
|
|
|
|
displayLoader: true,
|
2020-03-25 13:06:14 +00:00
|
|
|
variables: {
|
2020-05-04 15:29:06 +00:00
|
|
|
first: 50
|
2020-03-25 13:06:14 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handleBack = () => navigate(productListUrl());
|
|
|
|
|
2019-11-19 17:00:14 +00:00
|
|
|
const handleSuccess = (data: ProductCreate) => {
|
|
|
|
if (data.productCreate.errors.length === 0) {
|
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2019-11-19 17:00:14 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Product created"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
navigate(productUrl(data.productCreate.product.id));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2019-11-19 17:00:14 +00:00
|
|
|
<TypedProductCreateMutation onCompleted={handleSuccess}>
|
2019-12-06 17:11:46 +00:00
|
|
|
{(productCreate, productCreateOpts) => {
|
2019-11-19 17:00:14 +00:00
|
|
|
const handleSubmit = (formData: ProductCreatePageSubmitData) => {
|
|
|
|
productCreate({
|
|
|
|
variables: {
|
|
|
|
attributes: formData.attributes.map(attribute => ({
|
|
|
|
id: attribute.id,
|
|
|
|
values: attribute.value
|
|
|
|
})),
|
|
|
|
basePrice: decimal(formData.basePrice),
|
|
|
|
category: formData.category,
|
|
|
|
chargeTaxes: formData.chargeTaxes,
|
|
|
|
collections: formData.collections,
|
|
|
|
descriptionJson: JSON.stringify(formData.description),
|
|
|
|
isPublished: formData.isPublished,
|
|
|
|
name: formData.name,
|
|
|
|
productType: formData.productType,
|
|
|
|
publicationDate:
|
|
|
|
formData.publicationDate !== ""
|
|
|
|
? formData.publicationDate
|
|
|
|
: null,
|
|
|
|
seo: {
|
|
|
|
description: formData.seoDescription,
|
|
|
|
title: formData.seoTitle
|
|
|
|
},
|
|
|
|
sku: formData.sku,
|
2020-03-25 13:06:14 +00:00
|
|
|
stocks: formData.stocks.map(stock => ({
|
|
|
|
quantity: parseInt(stock.value, 0),
|
|
|
|
warehouse: stock.id
|
2020-03-27 11:06:11 +00:00
|
|
|
})),
|
|
|
|
trackInventory: formData.trackInventory
|
2019-11-19 16:04:53 +00:00
|
|
|
}
|
2019-11-19 17:00:14 +00:00
|
|
|
});
|
2019-11-19 16:04:53 +00:00
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-11-19 16:04:53 +00:00
|
|
|
return (
|
2019-11-19 17:00:14 +00:00
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Product",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<ProductCreatePage
|
|
|
|
currency={maybe(() => shop.defaultCurrency)}
|
|
|
|
categories={maybe(
|
|
|
|
() => searchCategoryOpts.data.search.edges,
|
|
|
|
[]
|
|
|
|
).map(edge => edge.node)}
|
|
|
|
collections={maybe(
|
|
|
|
() => searchCollectionOpts.data.search.edges,
|
|
|
|
[]
|
|
|
|
).map(edge => edge.node)}
|
2019-12-06 17:11:46 +00:00
|
|
|
disabled={productCreateOpts.loading}
|
2020-03-06 14:25:23 +00:00
|
|
|
errors={productCreateOpts.data?.productCreate.errors || []}
|
2019-11-19 17:00:14 +00:00
|
|
|
fetchCategories={searchCategory}
|
|
|
|
fetchCollections={searchCollection}
|
|
|
|
fetchProductTypes={searchProductTypes}
|
|
|
|
header={intl.formatMessage({
|
|
|
|
defaultMessage: "New Product",
|
|
|
|
description: "page header"
|
|
|
|
})}
|
|
|
|
productTypes={maybe(() =>
|
|
|
|
searchProductTypesOpts.data.search.edges.map(edge => edge.node)
|
|
|
|
)}
|
|
|
|
onBack={handleBack}
|
|
|
|
onSubmit={handleSubmit}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonBarState={productCreateOpts.status}
|
2019-11-19 17:00:14 +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
|
|
|
|
}}
|
2020-05-04 15:29:06 +00:00
|
|
|
warehouses={
|
|
|
|
warehouses.data?.warehouses.edges.map(edge => edge.node) || []
|
|
|
|
}
|
2019-11-19 17:00:14 +00:00
|
|
|
/>
|
|
|
|
</>
|
2019-11-19 16:04:53 +00:00
|
|
|
);
|
|
|
|
}}
|
2019-11-19 17:00:14 +00:00
|
|
|
</TypedProductCreateMutation>
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
};
|
2020-03-25 13:06:14 +00:00
|
|
|
export default ProductCreateView;
|