2020-05-14 09:30:32 +00:00
|
|
|
import NotFoundPage from "@saleor/components/NotFoundPage";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import useShop from "@saleor/hooks/useShop";
|
2020-03-06 14:25:23 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2020-09-01 14:27:35 +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-07-13 16:51:05 +00:00
|
|
|
import { decimal, weight } from "../../misc";
|
2019-08-09 11:14:35 +00:00
|
|
|
import ProductVariantCreatePage, {
|
2020-09-21 16:29:50 +00:00
|
|
|
ProductVariantCreatePageSubmitData,
|
|
|
|
ProductVariantCreatePageSubmitNextAction
|
2019-08-09 11:14:35 +00:00
|
|
|
} from "../components/ProductVariantCreatePage";
|
2020-09-17 14:37:33 +00:00
|
|
|
import {
|
|
|
|
useProductVariantReorderMutation,
|
|
|
|
useVariantCreateMutation
|
|
|
|
} from "../mutations";
|
2020-08-24 10:18:58 +00:00
|
|
|
import { useProductVariantCreateQuery } from "../queries";
|
2020-09-16 17:13:08 +00:00
|
|
|
import {
|
|
|
|
productListUrl,
|
|
|
|
productUrl,
|
|
|
|
ProductVariantAddUrlQueryParams,
|
|
|
|
productVariantEditUrl
|
|
|
|
} from "../urls";
|
2020-09-17 14:37:33 +00:00
|
|
|
import { createVariantReorderHandler } from "./ProductUpdate/handlers";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-03-27 11:06:11 +00:00
|
|
|
interface ProductVariantCreateProps {
|
2019-06-19 14:40:52 +00:00
|
|
|
productId: string;
|
2020-09-16 17:13:08 +00:00
|
|
|
params: ProductVariantAddUrlQueryParams;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 11:06:11 +00:00
|
|
|
export const ProductVariant: React.FC<ProductVariantCreateProps> = ({
|
2020-09-21 16:29:50 +00:00
|
|
|
productId
|
2020-03-27 11:06:11 +00:00
|
|
|
}) => {
|
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();
|
2020-05-04 15:29:06 +00:00
|
|
|
const warehouses = useWarehouseList({
|
|
|
|
displayLoader: true,
|
2020-03-27 11:06:11 +00:00
|
|
|
variables: {
|
2020-05-04 15:29:06 +00:00
|
|
|
first: 50
|
2020-03-27 11:06:11 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-08-24 10:18:58 +00:00
|
|
|
const { data, loading: productLoading } = useProductVariantCreateQuery({
|
|
|
|
displayLoader: true,
|
|
|
|
variables: { id: productId }
|
|
|
|
});
|
2020-02-20 14:18:22 +00:00
|
|
|
|
2020-08-24 10:18:58 +00:00
|
|
|
const [variantCreate, variantCreateResult] = useVariantCreateMutation({
|
|
|
|
onCompleted: data => {
|
2020-09-21 16:29:50 +00:00
|
|
|
if (data.productVariantCreate.errors.length === 0 && !submitNextAction) {
|
2020-08-24 10:18:58 +00:00
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
|
|
|
});
|
|
|
|
navigate(
|
|
|
|
productVariantEditUrl(
|
|
|
|
productId,
|
|
|
|
data.productVariantCreate.productVariant.id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-01 14:27:35 +00:00
|
|
|
const [updateMetadata] = useMetadataUpdate({});
|
|
|
|
const [updatePrivateMetadata] = usePrivateMetadataUpdate({});
|
2020-02-20 14:18:22 +00:00
|
|
|
|
2020-08-24 10:18:58 +00:00
|
|
|
const product = data?.product;
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-08-24 10:18:58 +00:00
|
|
|
if (product === null) {
|
|
|
|
return <NotFoundPage onBack={() => navigate(productListUrl())} />;
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-09-17 14:37:33 +00:00
|
|
|
const [
|
|
|
|
reorderProductVariants,
|
|
|
|
reorderProductVariantsOpts
|
|
|
|
] = useProductVariantReorderMutation({});
|
|
|
|
|
|
|
|
const handleVariantReorder = createVariantReorderHandler(product, variables =>
|
|
|
|
reorderProductVariants({ variables })
|
|
|
|
);
|
|
|
|
|
2020-08-24 10:18:58 +00:00
|
|
|
const handleBack = () => navigate(productUrl(productId));
|
2020-09-22 10:32:17 +00:00
|
|
|
const handleCreate = async (
|
|
|
|
formData: ProductVariantCreatePageSubmitData,
|
|
|
|
nextAction?: ProductVariantCreatePageSubmitNextAction
|
|
|
|
) => {
|
|
|
|
setSubmitNextAction(nextAction);
|
|
|
|
|
2020-09-01 14:27:35 +00:00
|
|
|
const result = await variantCreate({
|
2020-08-24 10:18:58 +00:00
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
attributes: formData.attributes
|
|
|
|
.filter(attribute => attribute.value !== "")
|
|
|
|
.map(attribute => ({
|
|
|
|
id: attribute.id,
|
|
|
|
values: [attribute.value]
|
|
|
|
})),
|
|
|
|
costPrice: decimal(formData.costPrice),
|
|
|
|
price: decimal(formData.price),
|
|
|
|
product: productId,
|
|
|
|
sku: formData.sku,
|
|
|
|
stocks: formData.stocks.map(stock => ({
|
|
|
|
quantity: parseInt(stock.value, 0),
|
|
|
|
warehouse: stock.id
|
|
|
|
})),
|
|
|
|
trackInventory: true,
|
|
|
|
weight: weight(formData.weight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-01 14:27:35 +00:00
|
|
|
|
|
|
|
return result.data.productVariantCreate?.productVariant?.id || null;
|
|
|
|
};
|
|
|
|
const handleSubmit = createMetadataCreateHandler(
|
|
|
|
handleCreate,
|
|
|
|
updateMetadata,
|
|
|
|
updatePrivateMetadata
|
|
|
|
);
|
2020-08-24 10:18:58 +00:00
|
|
|
const handleVariantClick = (id: string) =>
|
|
|
|
navigate(productVariantEditUrl(productId, id));
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-09-21 16:29:50 +00:00
|
|
|
const [submitNextAction, setSubmitNextAction] = React.useState<
|
|
|
|
ProductVariantCreatePageSubmitNextAction
|
|
|
|
>(null);
|
|
|
|
const handleSubmitNextAction = (
|
|
|
|
nextAction?: ProductVariantCreatePageSubmitNextAction
|
|
|
|
) => {
|
|
|
|
const action = nextAction || submitNextAction;
|
|
|
|
if (action === "warehouse-configure") {
|
|
|
|
navigate(warehouseListPath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-17 14:37:33 +00:00
|
|
|
const disableForm =
|
|
|
|
productLoading ||
|
|
|
|
variantCreateResult.loading ||
|
|
|
|
reorderProductVariantsOpts.loading;
|
2020-08-24 10:18:58 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
|
|
|
defaultMessage: "Create variant",
|
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
<ProductVariantCreatePage
|
|
|
|
currencySymbol={shop?.defaultCurrency}
|
|
|
|
disabled={disableForm}
|
|
|
|
errors={variantCreateResult.data?.productVariantCreate.errors || []}
|
|
|
|
header={intl.formatMessage({
|
|
|
|
defaultMessage: "Create Variant",
|
|
|
|
description: "header"
|
|
|
|
})}
|
|
|
|
product={data?.product}
|
|
|
|
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:18:58 +00:00
|
|
|
onVariantClick={handleVariantClick}
|
2020-09-17 14:37:33 +00:00
|
|
|
onVariantReorder={handleVariantReorder}
|
2020-08-24 10:18:58 +00:00
|
|
|
saveButtonBarState={variantCreateResult.status}
|
|
|
|
warehouses={
|
|
|
|
warehouses.data?.warehouses.edges.map(edge => edge.node) || []
|
|
|
|
}
|
|
|
|
weightUnit={shop?.defaultWeightUnit}
|
2020-09-16 17:13:08 +00:00
|
|
|
/>
|
2020-08-24 10:18:58 +00:00
|
|
|
</>
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ProductVariant;
|