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

120 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import DialogContentText from "@material-ui/core/DialogContentText";
import ActionDialog from "@saleor/components/ActionDialog";
import NotFoundPage from "@saleor/components/NotFoundPage";
2019-06-19 14:40:52 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
2020-07-01 09:39:36 +00:00
import { commonMessages } from "@saleor/intl";
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import ProductImagePage from "../components/ProductImagePage";
import {
2020-08-24 10:14:59 +00:00
useProductImageDeleteMutation,
useProductImageUpdateMutation
2019-06-19 14:40:52 +00:00
} from "../mutations";
2020-08-24 10:14:59 +00:00
import { useProductImageQuery } from "../queries";
2019-06-19 14:40:52 +00:00
import {
productImageUrl,
ProductImageUrlQueryParams,
productListUrl,
productUrl
2019-06-19 14:40:52 +00:00
} from "../urls";
interface ProductImageProps {
imageId: string;
productId: string;
params: ProductImageUrlQueryParams;
}
export const ProductImage: React.FC<ProductImageProps> = ({
2019-06-19 14:40:52 +00:00
imageId,
productId,
params
}) => {
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
2019-06-19 14:40:52 +00:00
const handleBack = () => navigate(productUrl(productId));
2020-08-24 10:14:59 +00:00
const { data, loading } = useProductImageQuery({
displayLoader: true,
variables: {
imageId,
productId
2019-06-19 14:40:52 +00:00
}
2020-08-24 10:14:59 +00:00
});
2019-06-19 14:40:52 +00:00
2020-08-24 10:14:59 +00:00
const [updateImage, updateResult] = useProductImageUpdateMutation({
onCompleted: data => {
if (data.productImageUpdate.errors.length === 0) {
notify({
status: "success",
text: intl.formatMessage(commonMessages.savedChanges)
});
}
}
});
const [deleteImage, deleteResult] = useProductImageDeleteMutation({
onCompleted: handleBack
});
2020-02-20 14:18:22 +00:00
2020-08-24 10:14:59 +00:00
const product = data?.product;
2020-02-20 14:18:22 +00:00
2020-08-24 10:14:59 +00:00
if (product === null) {
return <NotFoundPage onBack={() => navigate(productListUrl())} />;
}
const handleDelete = () => deleteImage({ variables: { id: imageId } });
const handleImageClick = (id: string) => () =>
navigate(productImageUrl(productId, id));
const handleUpdate = (formData: { description: string }) => {
updateImage({
variables: {
alt: formData.description,
id: imageId
}
});
};
const image = data?.product?.mainImage;
return (
<>
<ProductImagePage
disabled={loading}
product={data?.product?.name}
image={image || null}
images={data?.product?.images}
onBack={handleBack}
onDelete={() =>
navigate(
productImageUrl(productId, imageId, {
action: "remove"
})
)
}
onRowClick={handleImageClick}
onSubmit={handleUpdate}
saveButtonBarState={updateResult.status}
/>
<ActionDialog
onClose={() => navigate(productImageUrl(productId, imageId), true)}
onConfirm={handleDelete}
open={params.action === "remove"}
title={intl.formatMessage({
defaultMessage: "Delete Image",
description: "dialog header"
})}
variant="delete"
confirmButtonState={deleteResult.status}
>
<DialogContentText>
<FormattedMessage defaultMessage="Are you sure you want to delete this image?" />
</DialogContentText>
</ActionDialog>
</>
2019-06-19 14:40:52 +00:00
);
};
export default ProductImage;