Skip to content

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

Add product variant bulk delete

dominik-zeglen0c6799543df6
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index c0e286f452..00c20c568d 100644
--- a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
@@ -12,7 +12,7 @@ import SaveButtonBar from "../../../components/SaveButtonBar/SaveButtonBar";
import SeoForm from "../../../components/SeoForm";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
-import { UserError } from "../../../types";
+import { ListActionProps, UserError } from "../../../types";
import {
ProductDetails_product,
ProductDetails_product_attributes_attribute,
@@ -27,7 +27,8 @@ import ProductPricing from "../ProductPricing";
import ProductStock from "../ProductStock";
import ProductVariants from "../ProductVariants";
-interface ProductUpdateProps {
+interface ProductUpdateProps
+ extends ListActionProps<"onBulkProductVariantDelete"> {
errors: UserError[];
placeholderImage: string;
collections?: Array<{
@@ -114,6 +115,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
variants,
onAttributesEdit,
onBack,
+ onBulkProductVariantDelete,
onDelete,
onImageDelete,
onImageEdit,
@@ -234,9 +236,11 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
<CardSpacer />
{hasVariants ? (
<ProductVariants
+ disabled={disabled}
variants={variants}
fallbackPrice={product ? product.price : undefined}
onAttributesEdit={onAttributesEdit}
+ onBulkDelete={onBulkProductVariantDelete}
onRowClick={onVariantShow}
onVariantAdd={onVariantAdd}
/>
diff --git a/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx b/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
index b1c008a1dd..98dcbc2be7 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
@@ -1,7 +1,9 @@
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent";
+import Checkbox from "@material-ui/core/Checkbox";
import Hidden from "@material-ui/core/Hidden";
+import IconButton from "@material-ui/core/IconButton";
import {
createStyles,
Theme,
@@ -11,9 +13,9 @@ import {
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
-import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import Typography from "@material-ui/core/Typography";
+import DeleteIcon from "@material-ui/icons/Delete";
import * as classNames from "classnames";
import * as React from "react";
@@ -21,8 +23,11 @@ import CardTitle from "../../../components/CardTitle";
import Money from "../../../components/Money";
import Skeleton from "../../../components/Skeleton";
import StatusLabel from "../../../components/StatusLabel";
+import TableHead from "../../../components/TableHead";
+import useBulkActions from "../../../hooks/useBulkActions";
import i18n from "../../../i18n";
import { renderCollection } from "../../../misc";
+import { ListActionProps } from "../../../types";
import { ProductDetails_product_variants } from "../../types/ProductDetails";
import { ProductVariant_costPrice } from "../../types/ProductVariant";
@@ -44,8 +49,10 @@ const styles = (theme: Theme) =>
}
});
-interface ProductVariantsProps extends WithStyles<typeof styles> {
- disabled?: boolean;
+interface ProductVariantsProps
+ extends ListActionProps<"onBulkDelete">,
+ WithStyles<typeof styles> {
+ disabled: boolean;
variants: ProductDetails_product_variants[];
fallbackPrice?: ProductVariant_costPrice;
onAttributesEdit: () => void;
@@ -56,104 +63,143 @@ interface ProductVariantsProps extends WithStyles<typeof styles> {
export const ProductVariants = withStyles(styles, { name: "ProductVariants" })(
({
classes,
+ disabled,
variants,
fallbackPrice,
onAttributesEdit,
+ onBulkDelete,
onRowClick,
onVariantAdd
- }: ProductVariantsProps) => (
- <Card>
- <CardTitle
- title={i18n.t("Variants")}
- toolbar={
- <>
- <Button onClick={onAttributesEdit} variant="text" color="primary">
- {i18n.t("Edit attributes")}
- </Button>
- <Button onClick={onVariantAdd} variant="text" color="primary">
- {i18n.t("Add variant")}
- </Button>
- </>
- }
- />
- <CardContent>
- <Typography>
- {i18n.t(
- "Use variants for products that come in a variety of version for example different sizes or colors"
- )}
- </Typography>
- </CardContent>
- <Table className={classes.denseTable}>
- <TableHead>
- <TableRow>
- <TableCell className={classes.textLeft}>{i18n.t("Name")}</TableCell>
- <TableCell>{i18n.t("Status")}</TableCell>
- <TableCell>{i18n.t("SKU")}</TableCell>
- <Hidden smDown>
- <TableCell className={classes.textRight}>
- {i18n.t("Price")}
- </TableCell>
- </Hidden>
- </TableRow>
- </TableHead>
- <TableBody>
- {renderCollection(
- variants,
- variant => (
- <TableRow
- hover={!!variant}
- key={variant ? variant.id : "skeleton"}
+ }: ProductVariantsProps) => {
+ const { isMember, listElements, toggle } = useBulkActions(variants);
+
+ return (
+ <Card>
+ <CardTitle
+ title={i18n.t("Variants")}
+ toolbar={
+ <>
+ <Button onClick={onAttributesEdit} variant="text" color="primary">
+ {i18n.t("Edit attributes")}
+ </Button>
+ <Button onClick={onVariantAdd} variant="text" color="primary">
+ {i18n.t("Add variant")}
+ </Button>
+ </>
+ }
+ />
+ <CardContent>
+ <Typography>
+ {i18n.t(
+ "Use variants for products that come in a variety of version for example different sizes or colors"
+ )}
+ </Typography>
+ </CardContent>
+ <Table className={classes.denseTable}>
+ <TableHead
+ selected={listElements.length}
+ toolbar={
+ <IconButton
+ color="primary"
+ onClick={() => onBulkDelete(listElements)}
>
- <TableCell
- className={classNames(classes.textLeft, classes.link)}
- onClick={onRowClick(variant.id)}
- >
- {variant ? variant.name || variant.sku : <Skeleton />}
- </TableCell>
- <TableCell>
- {variant ? (
- <StatusLabel
- status={variant.stockQuantity > 0 ? "success" : "error"}
- label={
- variant.stockQuantity > 0
- ? i18n.t("Available")
- : i18n.t("Unavailable")
- }
- />
- ) : (
- <Skeleton />
- )}
+ <DeleteIcon />
+ </IconButton>
+ }
+ >
+ <TableRow>
+ <TableCell />
+ <TableCell className={classes.textLeft}>
+ {i18n.t("Name")}
+ </TableCell>
+ <TableCell>{i18n.t("Status")}</TableCell>
+ <TableCell>{i18n.t("SKU")}</TableCell>
+ <Hidden smDown>
+ <TableCell className={classes.textRight}>
+ {i18n.t("Price")}
</TableCell>
- <TableCell>{variant ? variant.sku : <Skeleton />}</TableCell>
- <Hidden smDown>
- <TableCell className={classes.textRight}>
- {variant ? (
- variant.priceOverride ? (
- <Money money={variant.priceOverride} />
- ) : fallbackPrice ? (
- <Money money={fallbackPrice} />
+ </Hidden>
+ </TableRow>
+ </TableHead>
+ <TableBody>
+ {renderCollection(
+ variants,
+ variant => {
+ const isSelected = variant ? isMember(variant.id) : false;
+
+ return (
+ <TableRow
+ selected={isSelected}
+ hover={!!variant}
+ onClick={onRowClick(variant.id)}
+ key={variant ? variant.id : "skeleton"}
+ >
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ checked={isSelected}
+ disabled={disabled}
+ onClick={event => {
+ toggle(variant.id);
+ event.stopPropagation();
+ }}
+ />
+ </TableCell>
+ <TableCell
+ className={classNames(classes.textLeft, classes.link)}
+ >
+ {variant ? variant.name || variant.sku : <Skeleton />}
+ </TableCell>
+ <TableCell>
+ {variant ? (
+ <StatusLabel
+ status={
+ variant.stockQuantity > 0 ? "success" : "error"
+ }
+ label={
+ variant.stockQuantity > 0
+ ? i18n.t("Available")
+ : i18n.t("Unavailable")
+ }
+ />
) : (
<Skeleton />
- )
- ) : (
- <Skeleton />
- )}
+ )}
+ </TableCell>
+ <TableCell>
+ {variant ? variant.sku : <Skeleton />}
+ </TableCell>
+ <Hidden smDown>
+ <TableCell className={classes.textRight}>
+ {variant ? (
+ variant.priceOverride ? (
+ <Money money={variant.priceOverride} />
+ ) : fallbackPrice ? (
+ <Money money={fallbackPrice} />
+ ) : (
+ <Skeleton />
+ )
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ </Hidden>
+ </TableRow>
+ );
+ },
+ () => (
+ <TableRow>
+ <TableCell colSpan={2}>
+ {i18n.t("This product has no variants")}
</TableCell>
- </Hidden>
- </TableRow>
- ),
- () => (
- <TableRow>
- <TableCell colSpan={2}>
- {i18n.t("This product has no variants")}
- </TableCell>
- </TableRow>
- )
- )}
- </TableBody>
- </Table>
- </Card>
- )
+ </TableRow>
+ )
+ )}
+ </TableBody>
+ </Table>
+ </Card>
+ );
+ }
);
ProductVariants.displayName = "ProductVariants";
export default ProductVariants;
diff --git a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
index f52ddd051a..e00558c411 100644
--- a/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
+++ b/saleor/static/dashboard-next/products/containers/ProductUpdateOperations.tsx
@@ -7,6 +7,7 @@ import {
TypedProductImageCreateMutation,
TypedProductImageDeleteMutation,
TypedProductUpdateMutation,
+ TypedProductVariantBulkDeleteMutation,
TypedSimpleProductUpdateMutation
} from "../mutations";
import { ProductDelete, ProductDeleteVariables } from "../types/ProductDelete";
@@ -24,6 +25,10 @@ import {
ProductImageReorderVariables
} from "../types/ProductImageReorder";
import { ProductUpdate, ProductUpdateVariables } from "../types/ProductUpdate";
+import {
+ ProductVariantBulkDelete,
+ ProductVariantBulkDeleteVariables
+} from "../types/ProductVariantBulkDelete";
import {
SimpleProductUpdate,
SimpleProductUpdateVariables
@@ -32,34 +37,37 @@ import ProductImagesReorderProvider from "./ProductImagesReorder";
interface ProductUpdateOperationsProps {
product: ProductDetails_product;
- children: (
- props: {
- createProductImage: PartialMutationProviderOutput<
- ProductImageCreate,
- ProductImageCreateVariables
- >;
- deleteProduct: PartialMutationProviderOutput<
- ProductDelete,
- ProductDeleteVariables
- >;
- deleteProductImage: PartialMutationProviderOutput<
- ProductImageDelete,
- ProductImageDeleteVariables
- >;
- reorderProductImages: PartialMutationProviderOutput<
- ProductImageReorder,
- ProductImageReorderVariables
- >;
- updateProduct: PartialMutationProviderOutput<
- ProductUpdate,
- ProductUpdateVariables
- >;
- updateSimpleProduct: PartialMutationProviderOutput<
- SimpleProductUpdate,
- SimpleProductUpdateVariables
- >;
- }
- ) => React.ReactNode;
+ children: (props: {
+ bulkProductVariantDelete: PartialMutationProviderOutput<
+ ProductVariantBulkDelete,
+ ProductVariantBulkDeleteVariables
+ >;
+ createProductImage: PartialMutationProviderOutput<
+ ProductImageCreate,
+ ProductImageCreateVariables
+ >;
+ deleteProduct: PartialMutationProviderOutput<
+ ProductDelete,
+ ProductDeleteVariables
+ >;
+ deleteProductImage: PartialMutationProviderOutput<
+ ProductImageDelete,
+ ProductImageDeleteVariables
+ >;
+ reorderProductImages: PartialMutationProviderOutput<
+ ProductImageReorder,
+ ProductImageReorderVariables
+ >;
+ updateProduct: PartialMutationProviderOutput<
+ ProductUpdate,
+ ProductUpdateVariables
+ >;
+ updateSimpleProduct: PartialMutationProviderOutput<
+ SimpleProductUpdate,
+ SimpleProductUpdateVariables
+ >;
+ }) => React.ReactNode;
+ onBulkProductVariantDelete?: (data: ProductVariantBulkDelete) => void;
onDelete?: (data: ProductDelete) => void;
onImageCreate?: (data: ProductImageCreate) => void;
onImageDelete?: (data: ProductImageDelete) => void;
@@ -72,6 +80,7 @@ const ProductUpdateOperations: React.StatelessComponent<
> = ({
product,
children,
+ onBulkProductVariantDelete,
onDelete,
onImageDelete,
onImageCreate,
@@ -99,28 +108,37 @@ const ProductUpdateOperations: React.StatelessComponent<
<TypedSimpleProductUpdateMutation
onCompleted={onUpdate}
>
- {(...updateSimpleProduct) =>
- children({
- createProductImage: getMutationProviderData(
- ...createProductImage
- ),
- deleteProduct: getMutationProviderData(
- ...deleteProduct
- ),
- deleteProductImage: getMutationProviderData(
- ...deleteProductImage
- ),
- reorderProductImages: getMutationProviderData(
- ...reorderProductImages
- ),
- updateProduct: getMutationProviderData(
- ...updateProduct
- ),
- updateSimpleProduct: getMutationProviderData(
- ...updateSimpleProduct
- )
- })
- }
+ {(...updateSimpleProduct) => (
+ <TypedProductVariantBulkDeleteMutation
+ onCompleted={onBulkProductVariantDelete}
+ >
+ {(...bulkProductVariantDelete) =>
+ children({
+ bulkProductVariantDelete: getMutationProviderData(
+ ...bulkProductVariantDelete
+ ),
+ createProductImage: getMutationProviderData(
+ ...createProductImage
+ ),
+ deleteProduct: getMutationProviderData(
+ ...deleteProduct
+ ),
+ deleteProductImage: getMutationProviderData(
+ ...deleteProductImage
+ ),
+ reorderProductImages: getMutationProviderData(
+ ...reorderProductImages
+ ),
+ updateProduct: getMutationProviderData(
+ ...updateProduct
+ ),
+ updateSimpleProduct: getMutationProviderData(
+ ...updateSimpleProduct
+ )
+ })
+ }
+ </TypedProductVariantBulkDeleteMutation>
+ )}
</TypedSimpleProductUpdateMutation>
)}
</TypedProductImageDeleteMutation>
diff --git a/saleor/static/dashboard-next/products/mutations.ts b/saleor/static/dashboard-next/products/mutations.ts
index e27855a81f..bd817bcdc3 100644
--- a/saleor/static/dashboard-next/products/mutations.ts
+++ b/saleor/static/dashboard-next/products/mutations.ts
@@ -36,15 +36,15 @@ import {
} from "./types/VariantImageUnassign";
import { VariantUpdate, VariantUpdateVariables } from "./types/VariantUpdate";
-import {
- fragmentVariant,
- productFragment,
- productFragmentDetails
-} from "./queries";
+import { fragmentVariant, productFragmentDetails } from "./queries";
import {
productBulkDelete,
productBulkDeleteVariables
} from "./types/productBulkDelete";
+import {
+ ProductVariantBulkDelete,
+ ProductVariantBulkDeleteVariables
+} from "./types/ProductVariantBulkDelete";
export const productImageCreateMutation = gql`
${productFragmentDetails}
@@ -432,3 +432,18 @@ export const TypedProductBulkDeleteMutation = TypedMutation<
productBulkDelete,
productBulkDeleteVariables
>(productBulkDeleteMutation);
+
+export const ProductVariantBulkDeleteMutation = gql`
+ mutation ProductVariantBulkDelete($ids: [ID!]!) {
+ productVariantBulkDelete(ids: $ids) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedProductVariantBulkDeleteMutation = TypedMutation<
+ ProductVariantBulkDelete,
+ ProductVariantBulkDeleteVariables
+>(ProductVariantBulkDeleteMutation);
diff --git a/saleor/static/dashboard-next/products/types/ProductVariantBulkDelete.ts b/saleor/static/dashboard-next/products/types/ProductVariantBulkDelete.ts
new file mode 100644
index 0000000000..d77fb6ca3e
--- /dev/null
+++ b/saleor/static/dashboard-next/products/types/ProductVariantBulkDelete.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: ProductVariantBulkDelete
+// ====================================================
+
+export interface ProductVariantBulkDelete_productVariantBulkDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface ProductVariantBulkDelete_productVariantBulkDelete {
+ __typename: "ProductVariantBulkDelete";
+ errors: ProductVariantBulkDelete_productVariantBulkDelete_errors[] | null;
+}
+
+export interface ProductVariantBulkDelete {
+ productVariantBulkDelete: ProductVariantBulkDelete_productVariantBulkDelete | null;
+}
+
+export interface ProductVariantBulkDeleteVariables {
+ ids: string[];
+}
diff --git a/saleor/static/dashboard-next/products/urls.ts b/saleor/static/dashboard-next/products/urls.ts
index fac828f3ba..9c1bdebc26 100644
--- a/saleor/static/dashboard-next/products/urls.ts
+++ b/saleor/static/dashboard-next/products/urls.ts
@@ -23,7 +23,8 @@ export const productListUrl = (params?: ProductListUrlQueryParams): string =>
export const productPath = (id: string) => urlJoin(productSection + id);
export type ProductUrlDialog = "remove";
-export type ProductUrlQueryParams = Dialog<"remove">;
+export type ProductUrlQueryParams = BulkAction &
+ Dialog<"remove" | "remove-variants">;
export const productUrl = (id: string, params?: ProductUrlQueryParams) =>
productPath(encodeURIComponent(id)) + "?" + stringifyQs(params);
diff --git a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
index 692c0acafa..44c2efba28 100644
--- a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
@@ -19,6 +19,7 @@ import {
ProductImageCreate,
ProductImageCreateVariables
} from "../types/ProductImageCreate";
+import { ProductVariantBulkDelete } from "../types/productVariantBulkDelete";
import {
productImageUrl,
productListUrl,
@@ -53,7 +54,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
require={["product"]}
variables={{ id }}
>
- {({ data, loading }) => {
+ {({ data, loading, refetch }) => {
const handleDelete = () => {
pushMessage({ text: i18n.t("Product removed") });
navigate(productListUrl());
@@ -79,16 +80,27 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
const handleVariantAdd = () =>
navigate(productVariantAddUrl(id));
+ const handleBulkProductVariantDelete = (
+ data: ProductVariantBulkDelete
+ ) => {
+ if (data.productVariantBulkDelete.errors.length === 0) {
+ navigate(productUrl(id));
+ refetch();
+ }
+ };
+
const product = data ? data.product : undefined;
return (
<ProductUpdateOperations
product={product}
+ onBulkProductVariantDelete={handleBulkProductVariantDelete}
onDelete={handleDelete}
onImageCreate={handleImageCreate}
onImageDelete={handleImageDeleteSuccess}
onUpdate={handleUpdate}
>
{({
+ bulkProductVariantDelete,
createProductImage,
deleteProduct,
deleteProductImage,
@@ -178,6 +190,17 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
() => deleteProduct.opts.data.productDelete.errors
)
);
+
+ const bulkProductVariantDeleteTransitionState = getMutationState(
+ bulkProductVariantDelete.opts.called,
+ bulkProductVariantDelete.opts.loading,
+ maybe(
+ () =>
+ bulkProductVariantDelete.opts.data
+ .productVariantBulkDelete.errors
+ )
+ );
+
return (
<>
<WindowTitle title={maybe(() => data.product.name)} />
@@ -216,6 +239,14 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
onBack={() => {
navigate(productListUrl());
}}
+ onBulkProductVariantDelete={ids =>
+ navigate(
+ productUrl(id, {
+ action: "remove-variants",
+ ids
+ })
+ )
+ }
onDelete={() =>
navigate(
productUrl(id, {
@@ -258,7 +289,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
/>
<ActionDialog
open={params.action === "remove"}
- onClose={() => navigate(productUrl(id))}
+ onClose={() => navigate(productUrl(id), true)}
confirmButtonState={deleteTransitionState}
onConfirm={() => deleteProduct.mutate({ id })}
variant="delete"
@@ -275,6 +306,34 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
}}
/>
</ActionDialog>
+ <ActionDialog
+ open={params.action === "remove-variants"}
+ onClose={() => navigate(productUrl(id), true)}
+ confirmButtonState={
+ bulkProductVariantDeleteTransitionState
+ }
+ onConfirm={() =>
+ bulkProductVariantDelete.mutate({
+ ids: params.ids
+ })
+ }
+ variant="delete"
+ title={i18n.t("Remove product variants")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ number }}</strong> variants?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
</>
);
}}