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

118 lines
4 KiB
TypeScript
Raw Normal View History

2019-06-19 14:40:52 +00:00
import DialogContentText from "@material-ui/core/DialogContentText";
2019-08-09 10:26:22 +00:00
import React from "react";
import { FormattedMessage, useIntl } from "react-intl";
2019-06-19 14:40:52 +00:00
import ActionDialog from "@saleor/components/ActionDialog";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
2019-12-06 17:11:46 +00:00
import { maybe } from "../../misc";
2019-06-19 14:40:52 +00:00
import ProductImagePage from "../components/ProductImagePage";
import {
TypedProductImageDeleteMutation,
TypedProductImageUpdateMutation
} from "../mutations";
import { TypedProductImageQuery } from "../queries";
import { ProductImageUpdate } from "../types/ProductImageUpdate";
import {
productImageUrl,
ProductImageUrlQueryParams,
productUrl
} 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));
const handleUpdateSuccess = (data: ProductImageUpdate) => {
if (data.productImageUpdate.errors.length === 0) {
notify({ text: "Saved changes" });
}
};
return (
<TypedProductImageQuery
displayLoader
variables={{
imageId,
productId
}}
require={["product"]}
>
2019-12-02 10:49:14 +00:00
{({ data, loading }) => (
<TypedProductImageUpdateMutation onCompleted={handleUpdateSuccess}>
{(updateImage, updateResult) => (
<TypedProductImageDeleteMutation onCompleted={handleBack}>
{(deleteImage, deleteResult) => {
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 && data.product && data.product.mainImage;
2019-06-19 14:40:52 +00:00
2019-12-02 10:49:14 +00:00
return (
<>
<ProductImagePage
disabled={loading}
product={maybe(() => data.product.name)}
image={image || null}
images={maybe(() => data.product.images)}
onBack={handleBack}
onDelete={() =>
navigate(
productImageUrl(productId, imageId, {
action: "remove"
})
)
}
onRowClick={handleImageClick}
onSubmit={handleUpdate}
2019-12-06 17:11:46 +00:00
saveButtonBarState={updateResult.state}
2019-12-02 10:49:14 +00:00
/>
<ActionDialog
onClose={() =>
navigate(productImageUrl(productId, imageId), true)
}
onConfirm={handleDelete}
open={params.action === "remove"}
title={intl.formatMessage({
defaultMessage: "Delete Image",
description: "dialog header"
})}
variant="delete"
2019-12-06 17:11:46 +00:00
confirmButtonState={deleteResult.state}
2019-12-02 10:49:14 +00:00
>
<DialogContentText>
<FormattedMessage defaultMessage="Are you sure you want to delete this image?" />
</DialogContentText>
</ActionDialog>
</>
);
}}
</TypedProductImageDeleteMutation>
)}
</TypedProductImageUpdateMutation>
)}
2019-06-19 14:40:52 +00:00
</TypedProductImageQuery>
);
};
export default ProductImage;