Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Enable user to delete product image from list

dominik-zeglen95e2b4a24056
diff --git a/saleor/static/dashboard-next/products/components/ProductImages/ProductImages.tsx b/saleor/static/dashboard-next/products/components/ProductImages/ProductImages.tsx
index 1385c8a136..9e2c0b3d67 100644
--- a/saleor/static/dashboard-next/products/components/ProductImages/ProductImages.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductImages/ProductImages.tsx
@@ -1,6 +1,7 @@
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
+import DialogContentText from "@material-ui/core/DialogContentText";
import IconButton from "@material-ui/core/IconButton";
import { withStyles } from "@material-ui/core/styles";
import Typography from "@material-ui/core/Typography";
@@ -10,7 +11,9 @@ import EditIcon from "@material-ui/icons/Edit";
import * as React from "react";
import { SortableContainer, SortableElement } from "react-sortable-hoc";
+import ActionDialog from "../../../components/ActionDialog";
import CardTitle from "../../../components/CardTitle";
+import Toggle from "../../../components/Toggle";
import i18n from "../../../i18n";
interface ProductImagesProps {
@@ -22,7 +25,8 @@ interface ProductImagesProps {
url: string;
}>;
loading?: boolean;
- onImageEdit?(id: string);
+ onImageDelete: (id: string) => () => void;
+ onImageEdit: (id: string) => () => void;
onImageUpload?(event: React.ChangeEvent<any>);
onImageReorder?(event: { oldIndex: number; newIndex: number });
}
@@ -34,12 +38,14 @@ interface ImageListElementProps {
sortOrder: number;
url: string;
};
- onImageEdit(event: React.ChangeEvent<any>);
+ onImageDelete: () => void;
+ onImageEdit: (event: React.ChangeEvent<any>) => void;
}
interface ImageListContainerProps {
items: any;
- onImageEdit(id: string);
+ onImageDelete: (id: string) => () => void;
+ onImageEdit: (id: string) => () => void;
}
const decorate = withStyles(theme => ({
@@ -116,35 +122,58 @@ const decorate = withStyles(theme => ({
}));
const ImageListElement = SortableElement(
- decorate<ImageListElementProps>(({ classes, onImageEdit, tile }) => (
- <div className={classes.imageContainer}>
- <div className={classes.imageOverlay}>
- <div className={classes.imageOverlayToolbar}>
- <IconButton color="secondary" onClick={onImageEdit}>
- <EditIcon />
- </IconButton>
- <IconButton color="secondary">
- <DeleteIcon />
- </IconButton>
+ decorate<ImageListElementProps>(
+ ({ classes, onImageDelete, onImageEdit, tile }) => (
+ <div className={classes.imageContainer}>
+ <div className={classes.imageOverlay}>
+ <div className={classes.imageOverlayToolbar}>
+ <IconButton color="secondary" onClick={onImageEdit}>
+ <EditIcon />
+ </IconButton>
+ <IconButton color="secondary" onClick={onImageDelete}>
+ <DeleteIcon />
+ </IconButton>
+ </div>
</div>
+ <img className={classes.image} src={tile.url} alt={tile.alt} />
</div>
- <img className={classes.image} src={tile.url} alt={tile.alt} />
- </div>
- ))
+ )
+ )
);
const ImageListContainer = SortableContainer(
decorate<ImageListContainerProps>(
- ({ classes, items, onImageEdit, ...props }) => {
+ ({ classes, items, onImageDelete, onImageEdit, ...props }) => {
return (
<div {...props}>
{items.map((image, index) => (
- <ImageListElement
- key={`item-${index}`}
- index={index}
- tile={image}
- onImageEdit={onImageEdit ? onImageEdit(image.id) : null}
- />
+ <Toggle>
+ {(opened, { toggle }) => (
+ <>
+ <ImageListElement
+ key={`item-${index}`}
+ index={index}
+ tile={image}
+ onImageEdit={onImageEdit ? onImageEdit(image.id) : null}
+ onImageDelete={toggle}
+ />
+ <ActionDialog
+ open={opened}
+ onClose={toggle}
+ onConfirm={() => {
+ onImageDelete(image.id)();
+ toggle();
+ }}
+ variant="delete"
+ title={i18n.t("Remove product image")}
+ >
+ <DialogContentText>
+ {i18n.t("Are you sure you want to delete this image?")}
+ </DialogContentText>
+ </ActionDialog>
+ </>
+ )}
+ </Toggle>
))}
</div>
);
@@ -159,6 +188,7 @@ const ProductImages = decorate<ProductImagesProps>(
placeholderImage,
loading,
onImageEdit,
+ onImageDelete,
onImageReorder,
onImageUpload
}) => (
@@ -200,6 +230,7 @@ const ProductImages = decorate<ProductImagesProps>(
items={images}
onSortEnd={onImageReorder}
className={classes.root}
+ onImageDelete={onImageDelete}
onImageEdit={onImageEdit}
/>
) : (
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 26adbe8a77..0c7f46ff8f 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -92,6 +92,7 @@ interface ProductUpdateProps {
header: string;
saveButtonBarState?: SaveButtonBarState;
onVariantShow: (id: string) => () => void;
+ onImageDelete: (id: string) => () => void;
onAttributesEdit: () => void;
onBack?();
onDelete?(id: string);
@@ -141,6 +142,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(
onAttributesEdit,
onBack,
onDelete,
+ onImageDelete,
onImageEdit,
onImageReorder,
onImageUpload,
@@ -247,6 +249,7 @@ export const ProductUpdate = decorate<ProductUpdateProps>(
<ProductImages
images={images}
placeholderImage={placeholderImage}
+ onImageDelete={onImageDelete}
onImageReorder={onImageReorder}
onImageEdit={onImageEdit}
onImageUpload={onImageUpload}
diff --git a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
index 83ce85a76c..1dfa1960c2 100644
--- a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
@@ -13,6 +13,8 @@ import {
ProductDetailsQuery,
ProductImageCreateMutation,
ProductImageCreateMutationVariables,
+ ProductImageDeleteMutation,
+ ProductImageDeleteMutationVariables,
ProductImageReorderMutation,
ProductImageReorderMutationVariables,
ProductUpdateMutation,
@@ -21,8 +23,10 @@ import {
import {
productDeleteMutation,
productImageCreateMutation,
+ productImageDeleteMutation,
TypedProductDeleteMutation,
- TypedProductImageCreateMutation
+ TypedProductImageCreateMutation,
+ TypedProductImageDeleteMutation
} from "../mutations";
import { productDetailsQuery } from "../queries";
import ProductImagesReorderProvider from "./ProductImagesReorder";
@@ -97,6 +101,44 @@ const ProductImageCreateProvider: React.StatelessComponent<
</TypedProductImageCreateMutation>
);
+interface ProductImageDeleteProviderProps
+ extends PartialMutationProviderProps<ProductImageDeleteMutation> {
+ children: PartialMutationProviderRenderProps<
+ ProductImageDeleteMutation,
+ ProductImageDeleteMutationVariables
+ >;
+ productId: string;
+}
+
+const ProductImageDeleteProvider: React.StatelessComponent<
+ ProductImageDeleteProviderProps
+> = ({ children, productId, onError, onSuccess }) => (
+ <TypedProductImageDeleteMutation
+ mutation={productImageDeleteMutation}
+ onCompleted={onSuccess}
+ onError={onError}
+ update={(cache, { data: { productImageDelete } }) => {
+ const data: ProductDetailsQuery = cache.readQuery({
+ query: productDetailsQuery,
+ variables: { id: productId }
+ });
+ data.product.images.edges = data.product.images.edges.filter(
+ edge => edge.node.id !== productImageDelete.productImage.id
+ );
+ cache.writeQuery({ query: productDetailsQuery, data });
+ }}
+ >
+ {(mutate, { data, error, loading }) =>
+ children({
+ data,
+ error,
+ loading,
+ mutate
+ })
+ }
+ </TypedProductImageDeleteMutation>
+);
+
interface ProductUpdateOperationsProps extends MutationProviderProps {
product?: ProductDetailsQuery["product"];
children: MutationProviderRenderProps<{
@@ -105,6 +147,10 @@ interface ProductUpdateOperationsProps extends MutationProviderProps {
ProductImageCreateMutationVariables
>;
deleteProduct: PartialMutationProviderOutput;
+ deleteProductImage: PartialMutationProviderOutput<
+ ProductImageDeleteMutation,
+ ProductImageDeleteMutationVariables
+ >;
reorderProductImages: PartialMutationProviderOutput<
ProductImageReorderMutation,
ProductImageReorderMutationVariables
@@ -116,6 +162,7 @@ interface ProductUpdateOperationsProps extends MutationProviderProps {
}>;
onDelete?: (data: ProductDeleteMutation) => void;
onImageCreate?: (data: ProductImageCreateMutation) => void;
+ onImageDelete?: (data: ProductImageDeleteMutation) => void;
onImageReorder?: (data: ProductImageReorderMutation) => void;
onUpdate?: (data: ProductUpdateMutation) => void;
}
@@ -127,6 +174,7 @@ const ProductUpdateOperations: React.StatelessComponent<
children,
onDelete,
onError,
+ onImageDelete,
onImageCreate,
onImageReorder,
onUpdate
@@ -156,61 +204,89 @@ const ProductUpdateOperations: React.StatelessComponent<
onError={onError}
onSuccess={onDelete}
>
- {deleteProduct =>
- children({
- createProductImage: {
- data: createProductImage.data,
- loading: createProductImage.loading,
- mutate: variables =>
- createProductImage.mutate({ variables })
- },
- deleteProduct: {
- data: deleteProduct.data,
- loading: deleteProduct.loading,
- mutate: deleteProduct.mutate
- },
- errors:
- updateProduct &&
- updateProduct.data &&
- updateProduct.data.productUpdate
- ? updateProduct.data.productUpdate.errors
- : [],
- reorderProductImages: {
- data: reorderProductImages.data,
- loading: reorderProductImages.loading,
- mutate: variables => {
- const imagesMap = {};
- product.images.edges.forEach(edge => {
- const image = edge.node;
- imagesMap[image.id] = image;
- });
- const productImages = variables.imagesIds.map(
- (id, index) => ({
- __typename: "ProductImage",
- ...imagesMap[id],
- sortOrder: index
- })
- );
- const optimisticResponse = {
- productImageReorder: {
- __typename: "ProductImageReorder",
- errors: null,
- productImages
+ {deleteProduct => (
+ <ProductImageDeleteProvider
+ productId={productId}
+ onError={onError}
+ onSuccess={onImageDelete}
+ >
+ {deleteProductImage =>
+ children({
+ createProductImage: {
+ data: createProductImage.data,
+ loading: createProductImage.loading,
+ mutate: variables =>
+ createProductImage.mutate({ variables })
+ },
+ deleteProduct: {
+ data: deleteProduct.data,
+ loading: deleteProduct.loading,
+ mutate: deleteProduct.mutate
+ },
+ deleteProductImage: {
+ data: deleteProductImage.data,
+ loading: deleteProductImage.loading,
+ mutate: variables => {
+ const optimisticResponse = {
+ productImageDelete: {
+ __typename: "ProductImageDelete",
+ productImage: {
+ __typename: "ProductImage",
+ id: variables.id
+ }
+ }
+ };
+ deleteProductImage.mutate({
+ optimisticResponse,
+ variables
+ });
+ }
+ },
+ errors:
+ updateProduct &&
+ updateProduct.data &&
+ updateProduct.data.productUpdate
+ ? updateProduct.data.productUpdate.errors
+ : [],
+ reorderProductImages: {
+ data: reorderProductImages.data,
+ loading: reorderProductImages.loading,
+ mutate: variables => {
+ const imagesMap = {};
+ product.images.edges.forEach(edge => {
+ const image = edge.node;
+ imagesMap[image.id] = image;
+ });
+ const productImages = variables.imagesIds.map(
+ (id, index) => ({
+ __typename: "ProductImage",
+ ...imagesMap[id],
+ sortOrder: index
+ })
+ );
+ const optimisticResponse = {
+ productImageReorder: {
+ __typename: "ProductImageReorder",
+ errors: null,
+ productImages
+ }
+ };
+ reorderProductImages.mutate({
+ optimisticResponse,
+ variables
+ });
}
- };
- reorderProductImages.mutate({
- optimisticResponse,
- variables
- });
- }
- },
- updateProduct: {
- data: updateProduct.data,
- loading: updateProduct.loading,
- mutate: variables => updateProduct.mutate({ variables })
+ },
+ updateProduct: {
+ data: updateProduct.data,
+ loading: updateProduct.loading,
+ mutate: variables =>
+ updateProduct.mutate({ variables })
+ }
+ })
}
- })
- }
+ </ProductImageDeleteProvider>
+ )}
</ProductDeleteProvider>
)}
</ProductImageCreateProvider>
diff --git a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
index 68b1b51527..81f3dc3856 100644
--- a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
@@ -56,6 +56,10 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
pushMessage({
text: i18n.t("Image successfully uploaded")
});
+ const handleImageDeleteSuccess = () =>
+ pushMessage({
+ text: i18n.t("Image successfully deleted")
+ });
const handleVariantAdd = () =>
navigate(productVariantAddUrl(id));
@@ -78,15 +82,19 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
onDelete={handleDelete}
onError={handleError}
onImageCreate={handleImageCreate}
+ onImageDelete={handleImageDeleteSuccess}
onUpdate={handleUpdate}
>
{({
createProductImage,
deleteProduct,
+ deleteProductImage,
errors,
reorderProductImages,
updateProduct
}) => {
+ const handleImageDelete = (id: string) => () =>
+ deleteProductImage.mutate({ id });
const handleImageEdit = (imageId: string) => () =>
navigate(productImageUrl(id, imageId));
const handleSubmit = data => {
@@ -177,6 +185,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
}
}}
onImageEdit={handleImageEdit}
+ onImageDelete={handleImageDelete}
/>
);
}}