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-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";
|
|
|
|
|
2020-03-27 11:06:11 +00:00
|
|
|
import { decimal } from "../../misc";
|
2019-08-09 11:14:35 +00:00
|
|
|
import ProductVariantCreatePage, {
|
|
|
|
ProductVariantCreatePageSubmitData
|
|
|
|
} from "../components/ProductVariantCreatePage";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { TypedVariantCreateMutation } from "../mutations";
|
|
|
|
import { TypedProductVariantCreateQuery } from "../queries";
|
|
|
|
import { VariantCreate } from "../types/VariantCreate";
|
2020-05-14 09:30:32 +00:00
|
|
|
import { productListUrl, productUrl, productVariantEditUrl } from "../urls";
|
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-03-27 11:06:11 +00:00
|
|
|
export const ProductVariant: React.FC<ProductVariantCreateProps> = ({
|
|
|
|
productId
|
|
|
|
}) => {
|
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
|
|
|
|
|
|
|
return (
|
2020-02-20 14:18:22 +00:00
|
|
|
<TypedProductVariantCreateQuery displayLoader variables={{ id: productId }}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{({ data, loading: productLoading }) => {
|
2020-02-20 14:18:22 +00:00
|
|
|
const product = data?.product;
|
|
|
|
|
|
|
|
if (product === null) {
|
|
|
|
return <NotFoundPage onBack={() => navigate(productListUrl())} />;
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const handleCreateSuccess = (data: VariantCreate) => {
|
2020-03-06 14:25:23 +00:00
|
|
|
if (data.productVariantCreate.errors.length === 0) {
|
2019-08-26 17:53:22 +00:00
|
|
|
notify({
|
2020-03-06 14:25:23 +00:00
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
2019-08-26 17:53:22 +00:00
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
navigate(
|
|
|
|
productVariantEditUrl(
|
|
|
|
productId,
|
|
|
|
data.productVariantCreate.productVariant.id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedVariantCreateMutation onCompleted={handleCreateSuccess}>
|
|
|
|
{(variantCreate, variantCreateResult) => {
|
|
|
|
const handleBack = () => navigate(productUrl(productId));
|
2019-08-09 11:14:35 +00:00
|
|
|
const handleSubmit = (
|
|
|
|
formData: ProductVariantCreatePageSubmitData
|
|
|
|
) =>
|
2019-06-19 14:40:52 +00:00
|
|
|
variantCreate({
|
|
|
|
variables: {
|
2019-10-02 13:34:34 +00:00
|
|
|
input: {
|
|
|
|
attributes: formData.attributes
|
|
|
|
.filter(attribute => attribute.value !== "")
|
|
|
|
.map(attribute => ({
|
|
|
|
id: attribute.id,
|
|
|
|
values: [attribute.value]
|
|
|
|
})),
|
|
|
|
costPrice: decimal(formData.costPrice),
|
|
|
|
priceOverride: decimal(formData.priceOverride),
|
|
|
|
product: productId,
|
|
|
|
sku: formData.sku,
|
2020-03-27 11:06:11 +00:00
|
|
|
stocks: formData.stocks.map(stock => ({
|
|
|
|
quantity: parseInt(stock.value, 0),
|
|
|
|
warehouse: stock.id
|
|
|
|
})),
|
2019-10-02 13:34:34 +00:00
|
|
|
trackInventory: true
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
const handleVariantClick = (id: string) =>
|
|
|
|
navigate(productVariantEditUrl(productId, id));
|
|
|
|
|
|
|
|
const disableForm = productLoading || variantCreateResult.loading;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2019-08-26 17:53:22 +00:00
|
|
|
<WindowTitle
|
|
|
|
title={intl.formatMessage({
|
2019-09-05 13:33:50 +00:00
|
|
|
defaultMessage: "Create variant",
|
2019-08-26 17:53:22 +00:00
|
|
|
description: "window title"
|
|
|
|
})}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
<ProductVariantCreatePage
|
2020-03-27 11:06:11 +00:00
|
|
|
currencySymbol={shop?.defaultCurrency}
|
2020-03-27 14:51:21 +00:00
|
|
|
disabled={disableForm}
|
2020-03-06 14:25:23 +00:00
|
|
|
errors={
|
|
|
|
variantCreateResult.data?.productVariantCreate.errors ||
|
2019-06-19 14:40:52 +00:00
|
|
|
[]
|
2020-03-06 14:25:23 +00:00
|
|
|
}
|
2019-08-26 17:53:22 +00:00
|
|
|
header={intl.formatMessage({
|
2019-09-05 13:33:50 +00:00
|
|
|
defaultMessage: "Create Variant",
|
2019-08-26 17:53:22 +00:00
|
|
|
description: "header"
|
|
|
|
})}
|
2020-03-06 14:25:23 +00:00
|
|
|
product={data?.product}
|
2019-06-19 14:40:52 +00:00
|
|
|
onBack={handleBack}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
onVariantClick={handleVariantClick}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonBarState={variantCreateResult.status}
|
2020-05-04 15:29:06 +00:00
|
|
|
warehouses={
|
|
|
|
warehouses.data?.warehouses.edges.map(
|
|
|
|
edge => edge.node
|
|
|
|
) || []
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedVariantCreateMutation>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedProductVariantCreateQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ProductVariant;
|