Skip to content

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

Move bulk variant delete to the view level

dominik-zeglen975b4e187e82
diff --git a/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx b/saleor/static/dashboard-next/products/components/ProductUpdatePage/ProductUpdatePage.tsx
index 00c20c568d..0ff003e2ca 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 { ListActionProps, UserError } from "../../../types";
+import { ListActions, UserError } from "../../../types";
import {
ProductDetails_product,
ProductDetails_product_attributes_attribute,
@@ -27,8 +27,7 @@ import ProductPricing from "../ProductPricing";
import ProductStock from "../ProductStock";
import ProductVariants from "../ProductVariants";
-interface ProductUpdateProps
- extends ListActionProps<"onBulkProductVariantDelete"> {
+interface ProductUpdateProps extends ListActions {
errors: UserError[];
placeholderImage: string;
collections?: Array<{
@@ -115,7 +114,6 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
variants,
onAttributesEdit,
onBack,
- onBulkProductVariantDelete,
onDelete,
onImageDelete,
onImageEdit,
@@ -124,7 +122,11 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
onSeoClick,
onSubmit,
onVariantAdd,
- onVariantShow
+ onVariantShow,
+ isChecked,
+ selected,
+ toggle,
+ toolbar
}) => {
const initialData: FormData = {
attributes: maybe(
@@ -240,9 +242,12 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
variants={variants}
fallbackPrice={product ? product.price : undefined}
onAttributesEdit={onAttributesEdit}
- onBulkDelete={onBulkProductVariantDelete}
onRowClick={onVariantShow}
onVariantAdd={onVariantAdd}
+ toolbar={toolbar}
+ isChecked={isChecked}
+ selected={selected}
+ toggle={toggle}
/>
) : (
<ProductStock
diff --git a/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx b/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
index 98dcbc2be7..500f08596c 100644
--- a/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductVariants/ProductVariants.tsx
@@ -3,7 +3,6 @@ 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,
@@ -15,7 +14,6 @@ import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
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";
@@ -24,10 +22,9 @@ 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 { ListActions } from "../../../types";
import { ProductDetails_product_variants } from "../../types/ProductDetails";
import { ProductVariant_costPrice } from "../../types/ProductVariant";
@@ -49,9 +46,7 @@ const styles = (theme: Theme) =>
}
});
-interface ProductVariantsProps
- extends ListActionProps<"onBulkDelete">,
- WithStyles<typeof styles> {
+interface ProductVariantsProps extends ListActions, WithStyles<typeof styles> {
disabled: boolean;
variants: ProductDetails_product_variants[];
fallbackPrice?: ProductVariant_costPrice;
@@ -67,139 +62,122 @@ export const ProductVariants = withStyles(styles, { name: "ProductVariants" })(
variants,
fallbackPrice,
onAttributesEdit,
- onBulkDelete,
onRowClick,
- onVariantAdd
- }: 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)}
- >
- <DeleteIcon />
- </IconButton>
- }
- >
- <TableRow>
- <TableCell />
- <TableCell className={classes.textLeft}>
- {i18n.t("Name")}
+ onVariantAdd,
+ isChecked,
+ selected,
+ toggle,
+ toolbar
+ }: 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 selected={selected} toolbar={toolbar}>
+ <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>{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 => {
- const isSelected = variant ? isMember(variant.id) : false;
+ </Hidden>
+ </TableRow>
+ </TableHead>
+ <TableBody>
+ {renderCollection(
+ variants,
+ variant => {
+ const isSelected = variant ? isChecked(variant.id) : false;
- return (
- <TableRow
- selected={isSelected}
- hover={!!variant}
- onClick={onRowClick(variant.id)}
- key={variant ? variant.id : "skeleton"}
+ 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)}
>
- <TableCell padding="checkbox">
- <Checkbox
- color="primary"
- checked={isSelected}
- disabled={disabled}
- onClick={event => {
- toggle(variant.id);
- event.stopPropagation();
- }}
+ {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")
+ }
/>
- </TableCell>
- <TableCell
- className={classNames(classes.textLeft, classes.link)}
- >
- {variant ? variant.name || variant.sku : <Skeleton />}
- </TableCell>
- <TableCell>
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell>{variant ? variant.sku : <Skeleton />}</TableCell>
+ <Hidden smDown>
+ <TableCell className={classes.textRight}>
{variant ? (
- <StatusLabel
- status={
- variant.stockQuantity > 0 ? "success" : "error"
- }
- label={
- variant.stockQuantity > 0
- ? i18n.t("Available")
- : i18n.t("Unavailable")
- }
- />
+ variant.priceOverride ? (
+ <Money money={variant.priceOverride} />
+ ) : fallbackPrice ? (
+ <Money money={fallbackPrice} />
+ ) : (
+ <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>
- )
- )}
- </TableBody>
- </Table>
- </Card>
- );
- }
+ );
+ },
+ () => (
+ <TableRow>
+ <TableCell colSpan={2}>
+ {i18n.t("This product has no variants")}
+ </TableCell>
+ </TableRow>
+ )
+ )}
+ </TableBody>
+ </Table>
+ </Card>
+ )
);
ProductVariants.displayName = "ProductVariants";
export default ProductVariants;
diff --git a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
index 44c2efba28..ed99f27d29 100644
--- a/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductUpdate.tsx
@@ -1,10 +1,13 @@
import DialogContentText from "@material-ui/core/DialogContentText";
+import IconButton from "@material-ui/core/IconButton";
+import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
import { arrayMove } from "react-sortable-hoc";
import * as placeholderImg from "../../../images/placeholder255x255.png";
import ActionDialog from "../../components/ActionDialog";
import { WindowTitle } from "../../components/WindowTitle";
+import useBulkActions from "../../hooks/useBulkActions";
import useNavigator from "../../hooks/useNavigator";
import useNotifier from "../../hooks/useNotifier";
import i18n from "../../i18n";
@@ -39,7 +42,8 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
params
}) => {
const navigate = useNavigator();
- const pushMessage = useNotifier();
+ const notify = useNotifier();
+ const { isSelected, listElements, reset, toggle } = useBulkActions();
return (
<CategorySearchProvider>
@@ -56,11 +60,11 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
>
{({ data, loading, refetch }) => {
const handleDelete = () => {
- pushMessage({ text: i18n.t("Product removed") });
+ notify({ text: i18n.t("Product removed") });
navigate(productListUrl());
};
const handleUpdate = () =>
- pushMessage({ text: i18n.t("Saved changes") });
+ notify({ text: i18n.t("Saved changes") });
const handleImageCreate = (data: ProductImageCreate) => {
const imageError = data.productImageCreate.errors.find(
error =>
@@ -68,13 +72,13 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
("image" as keyof ProductImageCreateVariables)
);
if (imageError) {
- pushMessage({
+ notify({
text: imageError.message
});
}
};
const handleImageDeleteSuccess = () =>
- pushMessage({
+ notify({
text: i18n.t("Image successfully deleted")
});
const handleVariantAdd = () =>
@@ -85,6 +89,7 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
) => {
if (data.productVariantBulkDelete.errors.length === 0) {
navigate(productUrl(id));
+ reset();
refetch();
}
};
@@ -239,14 +244,6 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
onBack={() => {
navigate(productListUrl());
}}
- onBulkProductVariantDelete={ids =>
- navigate(
- productUrl(id, {
- action: "remove-variants",
- ids
- })
- )
- }
onDelete={() =>
navigate(
productUrl(id, {
@@ -286,6 +283,24 @@ export const ProductUpdate: React.StatelessComponent<ProductUpdateProps> = ({
}}
onImageEdit={handleImageEdit}
onImageDelete={handleImageDelete}
+ toolbar={
+ <IconButton
+ color="primary"
+ onClick={() =>
+ navigate(
+ productUrl(id, {
+ action: "remove-variants",
+ ids: listElements
+ })
+ )
+ }
+ >
+ <DeleteIcon />
+ </IconButton>
+ }
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
/>
<ActionDialog
open={params.action === "remove"}