saleor-dashboard/src/products/views/ProductVariantCreate.tsx
Dominik Żegleń 5b85d6c086
Use graphql-codegen (#1874)
* Use generated hooks in apps

* Remove unused files

* Use proper types in apps

* Use generated hooks in attributes

* Use generated hooks in auth module

* Use generated hooks in categories

* Use generated hooks in channels

* Use generated types in collections

* Remove legacy types from background tasks

* Use generated hooks in customers

* Use generated hooks in discounts

* Use generated hook in file upload

* Use generated types in gift cards

* Use generated types in home

* Use generated hooks in navigation

* Use generated hooks in orders

* Use generated hooks in pages

* Use generated hooks in page types

* Use generated hooks in permission groups

* Use generated hooks in plugins

* Use generated hooks in products

* Use fragment to mark product variants

* Improve code a bit

* Use generated hooks in page types

* Use generated types in searches

* Use generated hooks in shipping

* Use generated hooks in site settings

* Use generated hooks in staff members

* Use generated hooks in taxes

* Place all gql generated files in one directory

* Use generated hooks in translations

* Use global types from new generated module

* Use generated hooks in warehouses

* Use generated hooks in webhooks

* Use generated fragment types

* Unclutter types

* Remove hoc components

* Split hooks and types

* Fetch introspection file

* Delete obsolete schema file

* Fix rebase artifacts

* Fix autoreplace

* Fix auth provider tests

* Fix urls

* Remove leftover types

* Fix rebase artifacts
2022-03-09 09:56:55 +01:00

263 lines
8.5 KiB
TypeScript

import { getAttributesAfterFileAttributesUpdate } from "@saleor/attributes/utils/data";
import {
handleUploadMultipleFiles,
prepareAttributesInput
} from "@saleor/attributes/utils/handlers";
import { AttributeInput } from "@saleor/components/Attributes";
import NotFoundPage from "@saleor/components/NotFoundPage";
import { WindowTitle } from "@saleor/components/WindowTitle";
import { DEFAULT_INITIAL_SEARCH_DATA } from "@saleor/config";
import {
useFileUploadMutation,
useProductVariantCreateDataQuery,
useProductVariantReorderMutation,
useUpdateMetadataMutation,
useUpdatePrivateMetadataMutation,
useVariantCreateMutation,
useWarehouseListQuery
} from "@saleor/graphql";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import useShop from "@saleor/hooks/useShop";
import usePageSearch from "@saleor/searches/usePageSearch";
import useProductSearch from "@saleor/searches/useProductSearch";
import useAttributeValueSearchHandler from "@saleor/utils/handlers/attributeValueSearchHandler";
import createMetadataCreateHandler from "@saleor/utils/handlers/metadataCreateHandler";
import { mapEdgesToItems } from "@saleor/utils/maps";
import { warehouseAddPath } from "@saleor/warehouses/urls";
import React from "react";
import { useIntl } from "react-intl";
import { getMutationErrors, weight } from "../../misc";
import ProductVariantCreatePage from "../components/ProductVariantCreatePage";
import { ProductVariantCreateData } from "../components/ProductVariantCreatePage/form";
import {
productListUrl,
productUrl,
productVariantAddUrl,
ProductVariantAddUrlQueryParams,
productVariantEditUrl
} from "../urls";
import { variantCreateMessages as messages } from "./messages";
import { createVariantReorderHandler } from "./ProductUpdate/handlers";
interface ProductVariantCreateProps {
productId: string;
params: ProductVariantAddUrlQueryParams;
}
export const ProductVariant: React.FC<ProductVariantCreateProps> = ({
productId,
params
}) => {
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
const intl = useIntl();
const warehouses = useWarehouseListQuery({
displayLoader: true,
variables: {
first: 50
}
});
const { data, loading: productLoading } = useProductVariantCreateDataQuery({
displayLoader: true,
variables: {
id: productId,
firstValues: 10
}
});
const [uploadFile, uploadFileOpts] = useFileUploadMutation({});
const product = data?.product;
const [variantCreate, variantCreateResult] = useVariantCreateMutation({
onCompleted: data => {
const variantId = data.productVariantCreate.productVariant.id;
notify({
status: "success",
text: intl.formatMessage(messages.variantCreatedSuccess)
});
navigate(productVariantEditUrl(productId, variantId), {
resetScroll: true
});
}
});
const [updateMetadata] = useUpdateMetadataMutation({});
const [updatePrivateMetadata] = useUpdatePrivateMetadataMutation({});
if (product === null) {
return <NotFoundPage onBack={() => navigate(productListUrl())} />;
}
const [
reorderProductVariants,
reorderProductVariantsOpts
] = useProductVariantReorderMutation({});
const handleVariantReorder = createVariantReorderHandler(product, variables =>
reorderProductVariants({ variables })
);
const handleBack = () => navigate(productUrl(productId));
const handleCreate = async (formData: ProductVariantCreateData) => {
const uploadFilesResult = await handleUploadMultipleFiles(
formData.attributesWithNewFileValue,
variables => uploadFile({ variables })
);
const updatedFileAttributes = getAttributesAfterFileAttributesUpdate(
formData.attributesWithNewFileValue,
uploadFilesResult
);
const result = await variantCreate({
variables: {
input: {
attributes: prepareAttributesInput({
attributes: formData.attributes.filter(
attribute => attribute.value?.length && attribute.value[0] !== ""
),
updatedFileAttributes
}),
product: productId,
sku: formData.sku,
stocks: formData.stocks.map(stock => ({
quantity: parseInt(stock.value, 0) || 0,
warehouse: stock.id
})),
trackInventory: true,
weight: weight(formData.weight),
quantityLimitPerCustomer:
Number(formData.quantityLimitPerCustomer) || null,
preorder: formData.isPreorder
? {
globalThreshold: formData.globalThreshold
? parseInt(formData.globalThreshold, 10)
: null,
endDate: formData.preorderEndDateTime || null
}
: undefined
},
firstValues: 10
}
});
const id = result.data?.productVariantCreate?.productVariant?.id || null;
return { id, errors: getMutationErrors(result) };
};
const handleSubmit = createMetadataCreateHandler(
handleCreate,
updateMetadata,
updatePrivateMetadata
);
const handleVariantClick = (id: string) =>
navigate(productVariantEditUrl(productId, id));
const handleAssignAttributeReferenceClick = (attribute: AttributeInput) =>
navigate(
productVariantAddUrl(productId, {
action: "assign-attribute-value",
id: attribute.id
})
);
const {
loadMore: loadMorePages,
search: searchPages,
result: searchPagesOpts
} = usePageSearch({
variables: DEFAULT_INITIAL_SEARCH_DATA
});
const {
loadMore: loadMoreProducts,
search: searchProducts,
result: searchProductsOpts
} = useProductSearch({
variables: DEFAULT_INITIAL_SEARCH_DATA
});
const {
loadMore: loadMoreAttributeValues,
search: searchAttributeValues,
result: searchAttributeValuesOpts,
reset: searchAttributeReset
} = useAttributeValueSearchHandler(DEFAULT_INITIAL_SEARCH_DATA);
const fetchMoreReferencePages = {
hasMore: searchPagesOpts.data?.search?.pageInfo?.hasNextPage,
loading: searchPagesOpts.loading,
onFetchMore: loadMorePages
};
const fetchMoreReferenceProducts = {
hasMore: searchProductsOpts.data?.search?.pageInfo?.hasNextPage,
loading: searchProductsOpts.loading,
onFetchMore: loadMoreProducts
};
const fetchMoreAttributeValues = {
hasMore: !!searchAttributeValuesOpts.data?.attribute?.choices?.pageInfo
?.hasNextPage,
loading: !!searchAttributeValuesOpts.loading,
onFetchMore: loadMoreAttributeValues
};
const attributeValues =
mapEdgesToItems(searchAttributeValuesOpts?.data?.attribute.choices) || [];
const disableForm =
productLoading ||
uploadFileOpts.loading ||
variantCreateResult.loading ||
reorderProductVariantsOpts.loading;
return (
<>
<WindowTitle
title={intl.formatMessage({
defaultMessage: "Create variant",
description: "window title"
})}
/>
<ProductVariantCreatePage
disabled={disableForm}
errors={variantCreateResult.data?.productVariantCreate.errors || []}
header={intl.formatMessage({
defaultMessage: "Create Variant",
description: "header"
})}
product={data?.product}
attributeValues={attributeValues}
onBack={handleBack}
onSubmit={handleSubmit}
onVariantClick={handleVariantClick}
onWarehouseConfigure={() => navigate(warehouseAddPath)}
onVariantReorder={handleVariantReorder}
saveButtonBarState={variantCreateResult.status}
warehouses={mapEdgesToItems(warehouses?.data?.warehouses) || []}
weightUnit={shop?.defaultWeightUnit}
assignReferencesAttributeId={
params.action === "assign-attribute-value" && params.id
}
onAssignReferencesClick={handleAssignAttributeReferenceClick}
referencePages={mapEdgesToItems(searchPagesOpts?.data?.search) || []}
referenceProducts={
mapEdgesToItems(searchProductsOpts?.data?.search) || []
}
fetchReferencePages={searchPages}
fetchMoreReferencePages={fetchMoreReferencePages}
fetchReferenceProducts={searchProducts}
fetchMoreReferenceProducts={fetchMoreReferenceProducts}
fetchAttributeValues={searchAttributeValues}
fetchMoreAttributeValues={fetchMoreAttributeValues}
onCloseDialog={() => navigate(productVariantAddUrl(productId))}
onAttributeSelectBlur={searchAttributeReset}
/>
</>
);
};
export default ProductVariant;