Skip to content

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

Move category bulk delete logic higher

dominik-zeglen05443b288665
diff --git a/saleor/static/dashboard-next/categories/components/CategoryList/CategoryList.tsx b/saleor/static/dashboard-next/categories/components/CategoryList/CategoryList.tsx
index 3c0b92b8ae..d94865c768 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryList/CategoryList.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryList/CategoryList.tsx
@@ -1,24 +1,21 @@
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import Checkbox from "@material-ui/core/Checkbox";
-import IconButton from "@material-ui/core/IconButton";
import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter";
import TableRow from "@material-ui/core/TableRow";
-import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
import CardTitle from "../../../components/CardTitle";
import Skeleton from "../../../components/Skeleton";
import TableHead from "../../../components/TableHead";
import TablePagination from "../../../components/TablePagination";
-import useBulkActions from "../../../hooks/useBulkActions";
import i18n from "../../../i18n";
import { renderCollection } from "../../../misc";
-import { ListActionProps, ListProps } from "../../../types";
+import { ListActions, ListProps } from "../../../types";
const styles = createStyles({
centerText: {
@@ -34,7 +31,7 @@ const styles = createStyles({
interface CategoryListProps
extends ListProps,
- ListActionProps<"onBulkDelete">,
+ ListActions,
WithStyles<typeof styles> {
categories?: Array<{
id: string;
@@ -57,133 +54,118 @@ const CategoryList = withStyles(styles, { name: "CategoryList" })(
disabled,
isRoot,
pageInfo,
+ isChecked,
+ selected,
+ toggle,
+ toolbar,
onAdd,
- onBulkDelete,
onNextPage,
onPreviousPage,
onRowClick
- }: CategoryListProps) => {
- const { isMember: isChecked, listElements, toggle } = useBulkActions(
- categories
- );
+ }: CategoryListProps) => (
+ <Card>
+ {!isRoot && (
+ <CardTitle
+ title={i18n.t("All Subcategories")}
+ toolbar={
+ <Button color="primary" variant="text" onClick={onAdd}>
+ {i18n.t("Add subcategory")}
+ </Button>
+ }
+ />
+ )}
+ <Table>
+ <TableHead selected={selected} toolbar={toolbar}>
+ <TableRow>
+ <TableCell />
+ <TableCell className={classes.wideColumn}>
+ {i18n.t("Category Name", { context: "object" })}
+ </TableCell>
+ <TableCell className={classes.centerText}>
+ {i18n.t("Subcategories", { context: "object" })}
+ </TableCell>
+ <TableCell className={classes.centerText}>
+ {i18n
+ .t("No. Products", { context: "object" })
+ .replace(" ", "\xa0")}
+ </TableCell>
+ </TableRow>
+ </TableHead>
+ <TableFooter>
+ <TableRow>
+ <TablePagination
+ colSpan={4}
+ hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
+ onNextPage={onNextPage}
+ hasPreviousPage={
+ pageInfo && !disabled ? pageInfo.hasPreviousPage : false
+ }
+ onPreviousPage={onPreviousPage}
+ />
+ </TableRow>
+ </TableFooter>
+ <TableBody>
+ {renderCollection(
+ categories,
+ category => {
+ const isSelected = category ? isChecked(category.id) : false;
- return (
- <Card>
- {!isRoot && (
- <CardTitle
- title={i18n.t("All Subcategories")}
- toolbar={
- <Button color="primary" variant="text" onClick={onAdd}>
- {i18n.t("Add subcategory")}
- </Button>
- }
- />
- )}
- <Table>
- <TableHead
- selected={listElements.length}
- toolbar={
- <IconButton
- color="primary"
- onClick={() => onBulkDelete(listElements)}
- >
- <DeleteIcon />
- </IconButton>
- }
- >
- <TableRow>
- <TableCell />
- <TableCell className={classes.wideColumn}>
- {i18n.t("Category Name", { context: "object" })}
- </TableCell>
- <TableCell className={classes.centerText}>
- {i18n.t("Subcategories", { context: "object" })}
- </TableCell>
- <TableCell className={classes.centerText}>
- {i18n
- .t("No. Products", { context: "object" })
- .replace(" ", "\xa0")}
- </TableCell>
- </TableRow>
- </TableHead>
- <TableFooter>
- <TableRow>
- <TablePagination
- colSpan={4}
- hasNextPage={
- pageInfo && !disabled ? pageInfo.hasNextPage : false
- }
- onNextPage={onNextPage}
- hasPreviousPage={
- pageInfo && !disabled ? pageInfo.hasPreviousPage : false
- }
- onPreviousPage={onPreviousPage}
- />
- </TableRow>
- </TableFooter>
- <TableBody>
- {renderCollection(
- categories,
- category => {
- const isSelected = category ? isChecked(category.id) : false;
-
- return (
- <TableRow
- className={classes.tableRow}
- hover={!!category}
- onClick={category ? onRowClick(category.id) : undefined}
- key={category ? category.id : "skeleton"}
- >
- <TableCell padding="checkbox">
- <Checkbox
- color="primary"
- checked={isSelected}
- disabled={disabled}
- onClick={event => {
- toggle(category.id);
- event.stopPropagation();
- }}
- />
- </TableCell>
- <TableCell className={classes.wideColumn}>
- {category && category.name ? category.name : <Skeleton />}
- </TableCell>
- <TableCell className={classes.centerText}>
- {category &&
- category.children &&
- category.children.totalCount !== undefined ? (
- category.children.totalCount
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.centerText}>
- {category &&
- category.products &&
- category.products.totalCount !== undefined ? (
- category.products.totalCount
- ) : (
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
- );
- },
- () => (
- <TableRow>
- <TableCell colSpan={4}>
- {isRoot
- ? i18n.t("No categories found")
- : i18n.t("No subcategories found")}
+ return (
+ <TableRow
+ className={classes.tableRow}
+ hover={!!category}
+ onClick={category ? onRowClick(category.id) : undefined}
+ key={category ? category.id : "skeleton"}
+ >
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ checked={isSelected}
+ disabled={disabled}
+ onClick={event => {
+ toggle(category.id);
+ event.stopPropagation();
+ }}
+ />
+ </TableCell>
+ <TableCell className={classes.wideColumn}>
+ {category && category.name ? category.name : <Skeleton />}
+ </TableCell>
+ <TableCell className={classes.centerText}>
+ {category &&
+ category.children &&
+ category.children.totalCount !== undefined ? (
+ category.children.totalCount
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.centerText}>
+ {category &&
+ category.products &&
+ category.products.totalCount !== undefined ? (
+ category.products.totalCount
+ ) : (
+ <Skeleton />
+ )}
</TableCell>
</TableRow>
- )
- )}
- </TableBody>
- </Table>
- </Card>
- );
- }
+ );
+ },
+ () => (
+ <TableRow>
+ <TableCell colSpan={4}>
+ {isRoot
+ ? i18n.t("No categories found")
+ : i18n.t("No subcategories found")}
+ </TableCell>
+ </TableRow>
+ )
+ )}
+ </TableBody>
+ </Table>
+ </Card>
+ )
);
CategoryList.displayName = "CategoryList";
export default CategoryList;
diff --git a/saleor/static/dashboard-next/categories/components/CategoryListPage/CategoryListPage.tsx b/saleor/static/dashboard-next/categories/components/CategoryListPage/CategoryListPage.tsx
index b91e30544b..02c2b51649 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryListPage/CategoryListPage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryListPage/CategoryListPage.tsx
@@ -5,12 +5,10 @@ import * as React from "react";
import Container from "../../../components/Container";
import PageHeader from "../../../components/PageHeader";
import i18n from "../../../i18n";
-import { ListActionProps, PageListProps } from "../../../types";
+import { ListActions, PageListProps } from "../../../types";
import CategoryList from "../CategoryList";
-export interface CategoryTableProps
- extends PageListProps,
- ListActionProps<"onBulkDelete"> {
+export interface CategoryTableProps extends PageListProps, ListActions {
categories: Array<{
id: string;
name: string;
@@ -27,11 +25,14 @@ export const CategoryListPage: React.StatelessComponent<CategoryTableProps> = ({
categories,
disabled,
onAdd,
- onBulkDelete,
onNextPage,
onPreviousPage,
onRowClick,
- pageInfo
+ pageInfo,
+ isChecked,
+ selected,
+ toggle,
+ toolbar
}) => (
<Container>
<PageHeader title={i18n.t("Categories")}>
@@ -42,13 +43,16 @@ export const CategoryListPage: React.StatelessComponent<CategoryTableProps> = ({
<CategoryList
categories={categories}
onAdd={onAdd}
- onBulkDelete={onBulkDelete}
onRowClick={onRowClick}
disabled={disabled}
isRoot={true}
onNextPage={onNextPage}
onPreviousPage={onPreviousPage}
pageInfo={pageInfo}
+ isChecked={isChecked}
+ selected={selected}
+ toggle={toggle}
+ toolbar={toolbar}
/>
</Container>
);
diff --git a/saleor/static/dashboard-next/categories/views/CategoryList.tsx b/saleor/static/dashboard-next/categories/views/CategoryList.tsx
index 86795300f5..00112a1916 100644
--- a/saleor/static/dashboard-next/categories/views/CategoryList.tsx
+++ b/saleor/static/dashboard-next/categories/views/CategoryList.tsx
@@ -1,8 +1,11 @@
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 ActionDialog from "../../components/ActionDialog";
import { createPaginationState } from "../../components/Paginator";
+import useBulkActions from "../../hooks/useBulkActions";
import useNavigator from "../../hooks/useNavigator";
import usePaginator from "../../hooks/usePaginator";
import i18n from "../../i18n";
@@ -29,6 +32,7 @@ export const CategoryList: React.StatelessComponent<CategoryListProps> = ({
}) => {
const navigate = useNavigator();
const paginate = usePaginator();
+ const { isSelected, listElements, toggle, reset } = useBulkActions();
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
@@ -44,6 +48,7 @@ export const CategoryList: React.StatelessComponent<CategoryListProps> = ({
if (data.categoryBulkDelete.errors.length === 0) {
navigate(categoryListUrl(), true);
refetch();
+ reset();
}
};
return (
@@ -67,20 +72,30 @@ export const CategoryList: React.StatelessComponent<CategoryListProps> = ({
[]
)}
onAdd={() => navigate(categoryAddUrl())}
- onBulkDelete={ids =>
- navigate(
- categoryListUrl({
- ...params,
- action: "delete",
- ids
- })
- )
- }
onRowClick={id => () => navigate(categoryUrl(id))}
disabled={loading}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
pageInfo={pageInfo}
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
+ toolbar={
+ <IconButton
+ color="primary"
+ onClick={() =>
+ navigate(
+ categoryListUrl({
+ ...params,
+ action: "delete",
+ ids: listElements
+ })
+ )
+ }
+ >
+ <DeleteIcon />
+ </IconButton>
+ }
/>
<ActionDialog
confirmButtonState={bulkDeleteState}
diff --git a/saleor/static/dashboard-next/hooks/useBulkActions.ts b/saleor/static/dashboard-next/hooks/useBulkActions.ts
index d772c5d437..8c24a4bd48 100644
--- a/saleor/static/dashboard-next/hooks/useBulkActions.ts
+++ b/saleor/static/dashboard-next/hooks/useBulkActions.ts
@@ -1,14 +1,35 @@
-import { useEffect } from "react";
+import { useState } from "react";
-import useListSelector from "./useListSelector";
+function useBulkActions(initial: string[] = []) {
+ const [listElements, setListElements] = useState(initial);
-interface ConnectionNode {
- id: string;
-}
-function useBulkActions(list: ConnectionNode[]) {
- const listSelectorFuncs = useListSelector();
- useEffect(() => listSelectorFuncs.reset, [list === undefined, list === null]);
+ function isSelected(id: string) {
+ return !!listElements.find(listElement => listElement === id);
+ }
+
+ function add(id: string) {
+ setListElements([...listElements, id]);
+ }
+
+ function remove(id: string) {
+ setListElements(listElements.filter(listElement => listElement !== id));
+ }
+
+ function reset() {
+ setListElements(initial);
+ }
+
+ function toggle(id: string) {
+ isSelected(id) ? remove(id) : add(id);
+ }
- return listSelectorFuncs;
+ return {
+ add,
+ isSelected,
+ listElements,
+ remove,
+ reset,
+ toggle
+ };
}
export default useBulkActions;
diff --git a/saleor/static/dashboard-next/hooks/useListSelector.ts b/saleor/static/dashboard-next/hooks/useListSelector.ts
deleted file mode 100644
index d2889e3dbc..0000000000
--- a/saleor/static/dashboard-next/hooks/useListSelector.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-import { useState } from "react";
-
-function useListSelector(initial: string[] = []) {
- const [listElements, setListElements] = useState(initial);
-
- function isMember(id: string) {
- return !!listElements.find(listElement => listElement === id);
- }
-
- function add(id: string) {
- setListElements([...listElements, id]);
- }
-
- function remove(id: string) {
- setListElements(listElements.filter(listElement => listElement !== id));
- }
-
- function reset() {
- setListElements(initial);
- }
-
- function toggle(id: string) {
- isMember(id) ? remove(id) : add(id);
- }
-
- return {
- add,
- isMember,
- listElements,
- remove,
- reset,
- toggle
- };
-}
-export default useListSelector;