2019-08-09 11:55:45 +00:00
|
|
|
import placeholderImg from "@assets/images/placeholder255x255.png";
|
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";
|
2019-08-26 17:53:22 +00:00
|
|
|
import { commonMessages } from "@saleor/intl";
|
2020-03-27 10:40:34 +00:00
|
|
|
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { useWarehouseList } from "@saleor/warehouses/queries";
|
2020-05-14 09:30:32 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
2020-03-27 10:40:34 +00:00
|
|
|
import { decimal } from "../../misc";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ProductVariantDeleteDialog from "../components/ProductVariantDeleteDialog";
|
2019-08-09 11:14:35 +00:00
|
|
|
import ProductVariantPage, {
|
|
|
|
ProductVariantPageSubmitData
|
|
|
|
} from "../components/ProductVariantPage";
|
2019-06-19 14:40:52 +00:00
|
|
|
import ProductVariantOperations from "../containers/ProductVariantOperations";
|
|
|
|
import { TypedProductVariantQuery } from "../queries";
|
2020-02-20 14:18:22 +00:00
|
|
|
import {
|
|
|
|
VariantUpdate,
|
2020-03-09 14:59:58 +00:00
|
|
|
VariantUpdate_productVariantUpdate_errors
|
2020-02-20 14:18:22 +00:00
|
|
|
} from "../types/VariantUpdate";
|
2019-06-19 14:40:52 +00:00
|
|
|
import {
|
|
|
|
productUrl,
|
|
|
|
productVariantAddUrl,
|
|
|
|
productVariantEditUrl,
|
2020-05-14 09:30:32 +00:00
|
|
|
ProductVariantEditUrlDialog,
|
|
|
|
ProductVariantEditUrlQueryParams
|
2019-06-19 14:40:52 +00:00
|
|
|
} from "../urls";
|
2020-05-04 15:29:06 +00:00
|
|
|
import { mapFormsetStockToStockInput } from "../utils/data";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
interface ProductUpdateProps {
|
|
|
|
variantId: string;
|
|
|
|
productId: string;
|
|
|
|
params: ProductVariantEditUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
2019-11-07 11:34:54 +00:00
|
|
|
export const ProductVariant: React.FC<ProductUpdateProps> = ({
|
2019-06-19 14:40:52 +00:00
|
|
|
variantId,
|
|
|
|
productId,
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-26 17:53:22 +00:00
|
|
|
const intl = useIntl();
|
2020-02-20 14:18:22 +00:00
|
|
|
const [errors, setErrors] = useState<
|
2020-03-09 14:59:58 +00:00
|
|
|
VariantUpdate_productVariantUpdate_errors[]
|
2020-02-20 14:18:22 +00:00
|
|
|
>([]);
|
2019-12-03 12:26:07 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setErrors([]);
|
|
|
|
}, [variantId]);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2020-05-04 15:29:06 +00:00
|
|
|
const warehouses = useWarehouseList({
|
|
|
|
displayLoader: true,
|
2020-03-27 10:40:34 +00:00
|
|
|
variables: {
|
2020-05-04 15:29:06 +00:00
|
|
|
first: 50
|
2020-03-27 10:40:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-04 15:29:06 +00:00
|
|
|
const [openModal] = createDialogActionHandlers<
|
2020-03-27 10:40:34 +00:00
|
|
|
ProductVariantEditUrlDialog,
|
|
|
|
ProductVariantEditUrlQueryParams
|
|
|
|
>(
|
|
|
|
navigate,
|
|
|
|
params => productVariantEditUrl(productId, variantId, params),
|
|
|
|
params
|
|
|
|
);
|
|
|
|
|
2020-02-20 14:18:22 +00:00
|
|
|
const handleBack = () => navigate(productUrl(productId));
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
return (
|
2020-02-20 14:18:22 +00:00
|
|
|
<TypedProductVariantQuery displayLoader variables={{ id: variantId }}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{({ data, loading }) => {
|
2020-02-20 14:18:22 +00:00
|
|
|
const variant = data?.productVariant;
|
|
|
|
|
|
|
|
if (variant === null) {
|
|
|
|
return <NotFoundPage onBack={handleBack} />;
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const handleDelete = () => {
|
2019-08-26 17:53:22 +00:00
|
|
|
notify({
|
2020-07-01 09:39:36 +00:00
|
|
|
status: "success",
|
2019-08-26 17:53:22 +00:00
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Variant removed"
|
|
|
|
})
|
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
navigate(productUrl(productId));
|
|
|
|
};
|
|
|
|
const handleUpdate = (data: VariantUpdate) => {
|
2020-03-09 14:59:58 +00:00
|
|
|
if (data.productVariantUpdate.errors.length === 0) {
|
2020-07-01 09:39:36 +00:00
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage(commonMessages.savedChanges)
|
|
|
|
});
|
2019-12-03 09:17:01 +00:00
|
|
|
} else {
|
2020-03-09 14:59:58 +00:00
|
|
|
setErrors(data.productVariantUpdate.errors);
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ProductVariantOperations
|
|
|
|
onDelete={handleDelete}
|
|
|
|
onUpdate={handleUpdate}
|
|
|
|
>
|
|
|
|
{({ assignImage, deleteVariant, updateVariant, unassignImage }) => {
|
|
|
|
const disableFormSave =
|
|
|
|
loading ||
|
|
|
|
deleteVariant.opts.loading ||
|
|
|
|
updateVariant.opts.loading ||
|
|
|
|
assignImage.opts.loading ||
|
|
|
|
unassignImage.opts.loading;
|
2019-12-06 17:11:46 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
const handleImageSelect = (id: string) => () => {
|
|
|
|
if (variant) {
|
|
|
|
if (
|
|
|
|
variant.images &&
|
|
|
|
variant.images.map(image => image.id).indexOf(id) !== -1
|
|
|
|
) {
|
|
|
|
unassignImage.mutate({
|
|
|
|
imageId: id,
|
|
|
|
variantId: variant.id
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
assignImage.mutate({
|
|
|
|
imageId: id,
|
|
|
|
variantId: variant.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-03-27 10:40:34 +00:00
|
|
|
<WindowTitle title={data?.productVariant?.name} />
|
2019-06-19 14:40:52 +00:00
|
|
|
<ProductVariantPage
|
2019-12-03 09:17:01 +00:00
|
|
|
errors={errors}
|
2019-12-06 17:17:44 +00:00
|
|
|
saveButtonBarState={updateVariant.opts.status}
|
2019-06-19 14:40:52 +00:00
|
|
|
loading={disableFormSave}
|
|
|
|
placeholderImage={placeholderImg}
|
|
|
|
variant={variant}
|
2020-03-27 10:40:34 +00:00
|
|
|
header={variant?.name || variant?.sku}
|
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
|
|
|
onAdd={() => navigate(productVariantAddUrl(productId))}
|
|
|
|
onBack={handleBack}
|
2020-05-04 15:29:06 +00:00
|
|
|
onDelete={() => openModal("remove")}
|
2019-06-19 14:40:52 +00:00
|
|
|
onImageSelect={handleImageSelect}
|
2020-03-27 10:40:34 +00:00
|
|
|
onSubmit={(data: ProductVariantPageSubmitData) =>
|
|
|
|
updateVariant.mutate({
|
2020-05-04 15:29:06 +00:00
|
|
|
addStocks: data.addStocks.map(
|
|
|
|
mapFormsetStockToStockInput
|
|
|
|
),
|
2020-03-27 10:40:34 +00:00
|
|
|
attributes: data.attributes.map(attribute => ({
|
|
|
|
id: attribute.id,
|
|
|
|
values: [attribute.value]
|
|
|
|
})),
|
|
|
|
costPrice: decimal(data.costPrice),
|
|
|
|
id: variantId,
|
2020-06-12 11:19:44 +00:00
|
|
|
price: decimal(data.price),
|
2020-05-04 15:29:06 +00:00
|
|
|
removeStocks: data.removeStocks,
|
2020-03-27 10:40:34 +00:00
|
|
|
sku: data.sku,
|
2020-05-04 15:29:06 +00:00
|
|
|
stocks: data.updateStocks.map(
|
|
|
|
mapFormsetStockToStockInput
|
|
|
|
),
|
2020-03-27 10:40:34 +00:00
|
|
|
trackInventory: data.trackInventory
|
|
|
|
})
|
|
|
|
}
|
2019-06-19 14:40:52 +00:00
|
|
|
onVariantClick={variantId => {
|
|
|
|
navigate(productVariantEditUrl(productId, variantId));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<ProductVariantDeleteDialog
|
2019-12-06 17:17:44 +00:00
|
|
|
confirmButtonState={deleteVariant.opts.status}
|
2019-06-19 14:40:52 +00:00
|
|
|
onClose={() =>
|
|
|
|
navigate(productVariantEditUrl(productId, variantId))
|
|
|
|
}
|
|
|
|
onConfirm={() =>
|
|
|
|
deleteVariant.mutate({
|
|
|
|
id: variantId
|
|
|
|
})
|
|
|
|
}
|
|
|
|
open={params.action === "remove"}
|
2020-03-27 10:40:34 +00:00
|
|
|
name={data?.productVariant?.name}
|
|
|
|
/>
|
2019-06-19 14:40:52 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ProductVariantOperations>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedProductVariantQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ProductVariant;
|