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

117 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-08-09 10:26:22 +00:00
import React from "react";
import { useIntl } from "react-intl";
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";
2019-12-06 17:11:46 +00:00
import { decimal, maybe } 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";
import { productUrl, productVariantEditUrl } from "../urls";
interface ProductUpdateProps {
productId: string;
}
export const ProductVariant: React.FC<ProductUpdateProps> = ({ productId }) => {
2019-06-19 14:40:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const shop = useShop();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
return (
<TypedProductVariantCreateQuery
displayLoader
variables={{ id: productId }}
require={["product"]}
>
{({ data, loading: productLoading }) => {
const handleCreateSuccess = (data: VariantCreate) => {
2019-10-17 15:29:13 +00:00
if (data.productVariantCreate.productErrors.length === 0) {
notify({
text: intl.formatMessage({
defaultMessage: "Product created"
})
});
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,
2020-01-20 12:42:45 +00:00
quantity: parseInt(formData.quantity, 0),
2019-10-02 13:34:34 +00:00
sku: formData.sku,
trackInventory: true
}
2019-06-19 14:40:52 +00:00
}
});
const handleVariantClick = (id: string) =>
navigate(productVariantEditUrl(productId, id));
const disableForm = productLoading || variantCreateResult.loading;
return (
<>
<WindowTitle
title={intl.formatMessage({
2019-09-05 13:33:50 +00:00
defaultMessage: "Create variant",
description: "window title"
})}
/>
2019-06-19 14:40:52 +00:00
<ProductVariantCreatePage
currencySymbol={maybe(() => shop.defaultCurrency)}
errors={maybe(
() =>
2019-10-17 15:29:13 +00:00
variantCreateResult.data.productVariantCreate
.productErrors,
2019-06-19 14:40:52 +00:00
[]
)}
header={intl.formatMessage({
2019-09-05 13:33:50 +00:00
defaultMessage: "Create Variant",
description: "header"
})}
2019-06-19 14:40:52 +00:00
loading={disableForm}
product={maybe(() => data.product)}
onBack={handleBack}
onSubmit={handleSubmit}
onVariantClick={handleVariantClick}
2019-12-06 17:17:44 +00:00
saveButtonBarState={variantCreateResult.status}
2019-06-19 14:40:52 +00:00
/>
</>
);
}}
</TypedVariantCreateMutation>
);
}}
</TypedProductVariantCreateQuery>
);
};
export default ProductVariant;