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";
|
2020-09-18 13:01:00 +00:00
|
|
|
import { getProductAvailabilityVariables } from "@saleor/products/utils/handlers";
|
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-09-25 15:02:27 +00:00
|
|
|
import { useTaxTypeList } from "@saleor/taxes/queries";
|
2020-08-31 14:58:07 +00:00
|
|
|
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
|
|
|
|
import {
|
|
|
|
useMetadataUpdate,
|
|
|
|
usePrivateMetadataUpdate
|
|
|
|
} from "@saleor/utils/metadata/updateMetadata";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { useWarehouseList } from "@saleor/warehouses/queries";
|
2020-09-16 17:13:08 +00:00
|
|
|
import { warehouseListPath } from "@saleor/warehouses/urls";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2020-08-25 09:42:14 +00:00
|
|
|
import { decimal, weight } from "../../misc";
|
2019-08-09 11:14:35 +00:00
|
|
|
import ProductCreatePage, {
|
2020-09-21 16:29:50 +00:00
|
|
|
ProductCreatePageSubmitData,
|
|
|
|
ProductCreatePageSubmitNextAction
|
2019-08-09 11:14:35 +00:00
|
|
|
} from "../components/ProductCreatePage";
|
2020-09-18 13:01:00 +00:00
|
|
|
import {
|
|
|
|
useProductCreateMutation,
|
|
|
|
useProductSetAvailabilityForPurchase
|
|
|
|
} from "../mutations";
|
2020-09-22 10:50:28 +00:00
|
|
|
import { productListUrl, productUrl } from "../urls";
|
2020-09-16 17:13:08 +00:00
|
|
|
|
2020-09-22 10:50:28 +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
|
|
|
}
|
|
|
|
});
|
2020-08-31 14:58:07 +00:00
|
|
|
const [updateMetadata] = useMetadataUpdate({});
|
|
|
|
const [updatePrivateMetadata] = usePrivateMetadataUpdate({});
|
2020-09-25 15:02:27 +00:00
|
|
|
const taxTypes = useTaxTypeList({});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
const handleBack = () => navigate(productListUrl());
|
|
|
|
|
2020-09-18 13:01:00 +00:00
|
|
|
const [
|
|
|
|
setProductAvailability,
|
|
|
|
productAvailabilityOpts
|
|
|
|
] = useProductSetAvailabilityForPurchase({
|
|
|
|
onCompleted: data => {
|
|
|
|
const errors = data?.productSetAvailabilityForPurchase?.errors;
|
2020-09-21 16:29:50 +00:00
|
|
|
if (errors?.length === 0 && !submitNextAction) {
|
2020-09-18 13:01:00 +00:00
|
|
|
navigate(productUrl(data.productSetAvailabilityForPurchase.product.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-24 10:29:54 +00:00
|
|
|
const [productCreate, productCreateOpts] = useProductCreateMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
if (data.productCreate.errors.length === 0) {
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Product created"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
2019-11-19 17:00:14 +00:00
|
|
|
}
|
2020-08-24 10:29:54 +00:00
|
|
|
});
|
|
|
|
|
2020-09-22 10:32:17 +00:00
|
|
|
const handleCreate = async (
|
|
|
|
formData: ProductCreatePageSubmitData,
|
|
|
|
nextAction?: ProductCreatePageSubmitNextAction
|
|
|
|
) => {
|
|
|
|
setSubmitNextAction(nextAction);
|
|
|
|
|
2020-08-31 14:58:07 +00:00
|
|
|
const result = await productCreate({
|
2020-08-24 10:29:54 +00:00
|
|
|
variables: {
|
2020-09-23 08:29:13 +00:00
|
|
|
input: {
|
|
|
|
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-09-25 15:41:58 +00:00
|
|
|
slug: formData.slug,
|
2020-09-23 08:29:13 +00:00
|
|
|
stocks: formData.stocks.map(stock => ({
|
|
|
|
quantity: parseInt(stock.value, 0),
|
|
|
|
warehouse: stock.id
|
|
|
|
})),
|
2020-09-24 10:55:02 +00:00
|
|
|
taxCode: formData.changeTaxCode ? formData.taxCode : undefined,
|
2020-09-23 08:29:13 +00:00
|
|
|
trackInventory: formData.trackInventory,
|
|
|
|
visibleInListings: formData.visibleInListings,
|
|
|
|
weight: weight(formData.weight)
|
|
|
|
}
|
2020-08-24 10:29:54 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-31 14:58:07 +00:00
|
|
|
|
2020-09-18 13:01:00 +00:00
|
|
|
const productId = result.data.productCreate?.product?.id;
|
|
|
|
|
|
|
|
if (productId) {
|
|
|
|
const { isAvailableForPurchase, availableForPurchase } = formData;
|
|
|
|
|
|
|
|
const variables = getProductAvailabilityVariables({
|
|
|
|
availableForPurchase,
|
|
|
|
isAvailableForPurchase,
|
|
|
|
productId
|
|
|
|
});
|
|
|
|
|
|
|
|
setProductAvailability({
|
|
|
|
variables
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return productId || null;
|
2019-11-19 17:00:14 +00:00
|
|
|
};
|
2020-08-31 14:58:07 +00:00
|
|
|
const handleSubmit = createMetadataCreateHandler(
|
|
|
|
handleCreate,
|
|
|
|
updateMetadata,
|
|
|
|
updatePrivateMetadata
|
|
|
|
);
|
2019-11-19 17:00:14 +00:00
|
|
|
|
2020-09-21 16:29:50 +00:00
|
|
|
const [submitNextAction, setSubmitNextAction] = React.useState<
|
|
|
|
ProductCreatePageSubmitNextAction
|
|
|
|
>(null);
|
|
|
|
const handleSubmitNextAction = (
|
|
|
|
nextAction?: ProductCreatePageSubmitNextAction
|
|
|
|
) => {
|
|
|
|
const action = nextAction || submitNextAction;
|
|
|
|
if (action === "warehouse-configure") {
|
|
|
|
navigate(warehouseListPath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2020-08-24 10:29:54 +00:00
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Product",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<ProductCreatePage
|
2020-08-25 09:42:14 +00:00
|
|
|
currency={shop?.defaultCurrency}
|
|
|
|
categories={(searchCategoryOpts.data?.search.edges || []).map(
|
|
|
|
edge => edge.node
|
|
|
|
)}
|
|
|
|
collections={(searchCollectionOpts.data?.search.edges || []).map(
|
2020-08-24 10:29:54 +00:00
|
|
|
edge => edge.node
|
|
|
|
)}
|
2020-09-18 13:01:00 +00:00
|
|
|
disabled={productCreateOpts.loading || productAvailabilityOpts.loading}
|
2020-08-24 10:29:54 +00:00
|
|
|
errors={productCreateOpts.data?.productCreate.errors || []}
|
|
|
|
fetchCategories={searchCategory}
|
|
|
|
fetchCollections={searchCollection}
|
|
|
|
fetchProductTypes={searchProductTypes}
|
|
|
|
header={intl.formatMessage({
|
|
|
|
defaultMessage: "New Product",
|
|
|
|
description: "page header"
|
|
|
|
})}
|
2020-08-25 09:42:14 +00:00
|
|
|
productTypes={searchProductTypesOpts.data?.search.edges.map(
|
|
|
|
edge => edge.node
|
2020-08-24 10:29:54 +00:00
|
|
|
)}
|
|
|
|
onBack={handleBack}
|
2020-09-22 10:32:17 +00:00
|
|
|
onSubmit={async (data, nextAction) => {
|
|
|
|
const errors = await handleSubmit(data, nextAction);
|
2020-09-21 16:29:50 +00:00
|
|
|
if (errors?.length === 0) {
|
2020-09-22 10:32:17 +00:00
|
|
|
handleSubmitNextAction(nextAction);
|
2020-09-21 16:29:50 +00:00
|
|
|
} else {
|
|
|
|
setSubmitNextAction(null);
|
|
|
|
}
|
|
|
|
}}
|
2020-09-22 10:32:17 +00:00
|
|
|
onSubmitSkip={handleSubmitNextAction}
|
2020-08-24 10:29:54 +00:00
|
|
|
saveButtonBarState={productCreateOpts.status}
|
|
|
|
fetchMoreCategories={{
|
2020-08-25 09:42:14 +00:00
|
|
|
hasMore: searchCategoryOpts.data?.search.pageInfo.hasNextPage,
|
2020-08-24 10:29:54 +00:00
|
|
|
loading: searchCategoryOpts.loading,
|
|
|
|
onFetchMore: loadMoreCategories
|
|
|
|
}}
|
|
|
|
fetchMoreCollections={{
|
2020-08-25 09:42:14 +00:00
|
|
|
hasMore: searchCollectionOpts.data?.search.pageInfo.hasNextPage,
|
2020-08-24 10:29:54 +00:00
|
|
|
loading: searchCollectionOpts.loading,
|
|
|
|
onFetchMore: loadMoreCollections
|
|
|
|
}}
|
|
|
|
fetchMoreProductTypes={{
|
2020-08-25 09:42:14 +00:00
|
|
|
hasMore: searchProductTypesOpts.data?.search.pageInfo.hasNextPage,
|
2020-08-24 10:29:54 +00:00
|
|
|
loading: searchProductTypesOpts.loading,
|
|
|
|
onFetchMore: loadMoreProductTypes
|
|
|
|
}}
|
|
|
|
warehouses={
|
|
|
|
warehouses.data?.warehouses.edges.map(edge => edge.node) || []
|
|
|
|
}
|
2020-09-24 10:55:02 +00:00
|
|
|
taxTypes={taxTypes.data?.taxTypes || []}
|
2020-08-24 10:29:54 +00:00
|
|
|
weightUnit={shop?.defaultWeightUnit}
|
|
|
|
/>
|
|
|
|
</>
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
};
|
2020-03-25 13:06:14 +00:00
|
|
|
export default ProductCreateView;
|