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

241 lines
7.7 KiB
TypeScript
Raw Normal View History

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";
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";
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
2020-08-31 14:58:07 +00:00
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
import {
useMetadataUpdate,
usePrivateMetadataUpdate
} from "@saleor/utils/metadata/updateMetadata";
import { useWarehouseList } from "@saleor/warehouses/queries";
import { warehouseListPath } from "@saleor/warehouses/urls";
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, {
ProductCreatePageSubmitData,
ProductCreatePageSubmitNextAction
2019-08-09 11:14:35 +00:00
} from "../components/ProductCreatePage";
import {
useProductCreateMutation,
useProductSetAvailabilityForPurchase
} from "../mutations";
import { ProductAddUrlQueryParams, productListUrl, productUrl } from "../urls";
interface ProductCreateViewProps {
params: ProductAddUrlQueryParams;
}
2019-06-19 14:40:52 +00:00
export const ProductCreateView: React.FC<ProductCreateViewProps> = ({}) => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
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
});
const warehouses = useWarehouseList({
displayLoader: true,
2020-03-25 13:06:14 +00:00
variables: {
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());
const [
setProductAvailability,
productAvailabilityOpts
] = useProductSetAvailabilityForPurchase({
onCompleted: data => {
const errors = data?.productSetAvailabilityForPurchase?.errors;
if (errors?.length === 0 && !submitNextAction) {
navigate(productUrl(data.productSetAvailabilityForPurchase.product.id));
}
}
});
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-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({
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,
slug: formData.slug,
2020-09-23 08:29:13 +00:00
stocks: formData.stocks.map(stock => ({
quantity: parseInt(stock.value, 0),
warehouse: stock.id
})),
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-31 14:58:07 +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
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 (
<>
<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(
edge => edge.node
)}
disabled={productCreateOpts.loading || productAvailabilityOpts.loading}
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
)}
onBack={handleBack}
2020-09-22 10:32:17 +00:00
onSubmit={async (data, nextAction) => {
const errors = await handleSubmit(data, nextAction);
if (errors?.length === 0) {
2020-09-22 10:32:17 +00:00
handleSubmitNextAction(nextAction);
} else {
setSubmitNextAction(null);
}
}}
2020-09-22 10:32:17 +00:00
onSubmitSkip={handleSubmitNextAction}
saveButtonBarState={productCreateOpts.status}
fetchMoreCategories={{
2020-08-25 09:42:14 +00:00
hasMore: searchCategoryOpts.data?.search.pageInfo.hasNextPage,
loading: searchCategoryOpts.loading,
onFetchMore: loadMoreCategories
}}
fetchMoreCollections={{
2020-08-25 09:42:14 +00:00
hasMore: searchCollectionOpts.data?.search.pageInfo.hasNextPage,
loading: searchCollectionOpts.loading,
onFetchMore: loadMoreCollections
}}
fetchMoreProductTypes={{
2020-08-25 09:42:14 +00:00
hasMore: searchProductTypesOpts.data?.search.pageInfo.hasNextPage,
loading: searchProductTypesOpts.loading,
onFetchMore: loadMoreProductTypes
}}
warehouses={
warehouses.data?.warehouses.edges.map(edge => edge.node) || []
}
taxTypes={taxTypes.data?.taxTypes || []}
weightUnit={shop?.defaultWeightUnit}
/>
</>
2019-06-19 14:40:52 +00:00
);
};
2020-03-25 13:06:14 +00:00
export default ProductCreateView;