2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 17:53:22 +00:00
|
|
|
import { useIntl } from "react-intl";
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-08-09 11:55:45 +00:00
|
|
|
import placeholderImg from "@assets/images/placeholder255x255.png";
|
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";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { decimal, getMutationState, maybe } from "../../misc";
|
|
|
|
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";
|
|
|
|
import { VariantUpdate } from "../types/VariantUpdate";
|
|
|
|
import {
|
|
|
|
productUrl,
|
|
|
|
productVariantAddUrl,
|
|
|
|
productVariantEditUrl,
|
|
|
|
ProductVariantEditUrlQueryParams
|
|
|
|
} from "../urls";
|
|
|
|
|
|
|
|
interface ProductUpdateProps {
|
|
|
|
variantId: string;
|
|
|
|
productId: string;
|
|
|
|
params: ProductVariantEditUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const ProductVariant: React.StatelessComponent<ProductUpdateProps> = ({
|
|
|
|
variantId,
|
|
|
|
productId,
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
2019-08-26 17:53:22 +00:00
|
|
|
const intl = useIntl();
|
2019-06-19 14:40:52 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TypedProductVariantQuery
|
|
|
|
displayLoader
|
|
|
|
variables={{ id: variantId }}
|
|
|
|
require={["productVariant"]}
|
|
|
|
>
|
|
|
|
{({ data, loading }) => {
|
|
|
|
const variant = data ? data.productVariant : undefined;
|
|
|
|
const handleBack = () => navigate(productUrl(productId));
|
|
|
|
const handleDelete = () => {
|
2019-08-26 17:53:22 +00:00
|
|
|
notify({
|
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "Variant removed"
|
|
|
|
})
|
|
|
|
});
|
2019-06-19 14:40:52 +00:00
|
|
|
navigate(productUrl(productId));
|
|
|
|
};
|
|
|
|
const handleUpdate = (data: VariantUpdate) => {
|
2019-10-17 15:29:13 +00:00
|
|
|
if (!maybe(() => data.productVariantUpdate.productErrors.length)) {
|
2019-08-26 17:53:22 +00:00
|
|
|
notify({ text: intl.formatMessage(commonMessages.savedChanges) });
|
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;
|
|
|
|
const formTransitionState = getMutationState(
|
|
|
|
updateVariant.opts.called,
|
|
|
|
updateVariant.opts.loading,
|
2019-10-17 15:29:13 +00:00
|
|
|
maybe(
|
|
|
|
() =>
|
|
|
|
updateVariant.opts.data.productVariantUpdate.productErrors
|
|
|
|
)
|
2019-06-19 14:40:52 +00:00
|
|
|
);
|
|
|
|
const removeTransitionState = getMutationState(
|
|
|
|
deleteVariant.opts.called,
|
|
|
|
deleteVariant.opts.loading,
|
|
|
|
maybe(() => deleteVariant.opts.data.productVariantDelete.errors)
|
|
|
|
);
|
|
|
|
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 (
|
|
|
|
<>
|
|
|
|
<WindowTitle title={maybe(() => data.productVariant.name)} />
|
|
|
|
<ProductVariantPage
|
|
|
|
errors={maybe(
|
2019-10-17 15:29:13 +00:00
|
|
|
() =>
|
|
|
|
updateVariant.opts.data.productVariantUpdate
|
|
|
|
.productErrors,
|
2019-06-19 14:40:52 +00:00
|
|
|
[]
|
|
|
|
)}
|
|
|
|
saveButtonBarState={formTransitionState}
|
|
|
|
loading={disableFormSave}
|
|
|
|
placeholderImage={placeholderImg}
|
|
|
|
variant={variant}
|
|
|
|
header={variant ? variant.name || variant.sku : undefined}
|
|
|
|
onAdd={() => navigate(productVariantAddUrl(productId))}
|
|
|
|
onBack={handleBack}
|
|
|
|
onDelete={() =>
|
|
|
|
navigate(
|
|
|
|
productVariantEditUrl(productId, variantId, {
|
|
|
|
action: "remove"
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onImageSelect={handleImageSelect}
|
2019-08-09 11:14:35 +00:00
|
|
|
onSubmit={(data: ProductVariantPageSubmitData) => {
|
2019-06-19 14:40:52 +00:00
|
|
|
if (variant) {
|
|
|
|
updateVariant.mutate({
|
2019-08-09 11:14:35 +00:00
|
|
|
attributes: data.attributes.map(attribute => ({
|
|
|
|
id: attribute.id,
|
|
|
|
values: [attribute.value]
|
|
|
|
})),
|
2019-06-19 14:40:52 +00:00
|
|
|
costPrice: decimal(data.costPrice),
|
|
|
|
id: variantId,
|
|
|
|
priceOverride: decimal(data.priceOverride),
|
|
|
|
quantity: data.quantity || null,
|
|
|
|
sku: data.sku,
|
|
|
|
trackInventory: true // FIXME: missing in UI
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onVariantClick={variantId => {
|
|
|
|
navigate(productVariantEditUrl(productId, variantId));
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<ProductVariantDeleteDialog
|
|
|
|
confirmButtonState={removeTransitionState}
|
|
|
|
onClose={() =>
|
|
|
|
navigate(productVariantEditUrl(productId, variantId))
|
|
|
|
}
|
|
|
|
onConfirm={() =>
|
|
|
|
deleteVariant.mutate({
|
|
|
|
id: variantId
|
|
|
|
})
|
|
|
|
}
|
|
|
|
open={params.action === "remove"}
|
|
|
|
name={maybe(() => data.productVariant.name)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ProductVariantOperations>
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
</TypedProductVariantQuery>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default ProductVariant;
|