Skip to content

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

Add menu delete

dominik-zeglen0b0eabe3e2ed
diff --git a/saleor/static/dashboard-next/navigation/components/MenuList/MenuList.tsx b/saleor/static/dashboard-next/navigation/components/MenuList/MenuList.tsx
index 90155bb098..50ed529660 100644
--- a/saleor/static/dashboard-next/navigation/components/MenuList/MenuList.tsx
+++ b/saleor/static/dashboard-next/navigation/components/MenuList/MenuList.tsx
@@ -10,20 +10,20 @@ 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 TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
import IconButtonTableCell from "../../../components/IconButtonTableCell";
import Skeleton from "../../../components/Skeleton";
+import TableHead from "../../../components/TableHead";
import TablePagination from "../../../components/TablePagination";
import i18n from "../../../i18n";
-import { maybe, renderCollection } from "../../../misc";
-import { ListProps } from "../../../types";
+import { maybe, renderCollection, stopPropagation } from "../../../misc";
+import { ListActions, ListProps } from "../../../types";
import { MenuList_menus_edges_node } from "../../types/MenuList";
-export interface MenuListProps extends ListProps {
+export interface MenuListProps extends ListProps, ListActions {
menus: MenuList_menus_edges_node[];
onDelete: (id: string) => void;
}
@@ -48,16 +48,20 @@ const MenuList = withStyles(styles, { name: "MenuList" })(
({
classes,
disabled,
+ isChecked,
menus,
onDelete,
onNextPage,
onPreviousPage,
onRowClick,
- pageInfo
+ pageInfo,
+ selected,
+ toggle,
+ toolbar
}: MenuListProps & WithStyles<typeof styles>) => (
<Card>
<Table>
- <TableHead>
+ <TableHead selected={selected} toolbar={toolbar}>
<TableRow>
<TableCell padding="checkbox" />
<TableCell className={classes.colTitle}>
@@ -85,33 +89,42 @@ const MenuList = withStyles(styles, { name: "MenuList" })(
<TableBody>
{renderCollection(
menus,
- menu => (
- <TableRow
- hover={!!menu}
- key={menu ? menu.id : "skeleton"}
- onClick={menu && onRowClick(menu.id)}
- className={classes.row}
- >
- <TableCell>
- <Checkbox />
- </TableCell>
- <TableCell className={classes.colTitle}>
- {maybe<React.ReactNode>(() => menu.name, <Skeleton />)}
- </TableCell>
- <TableCell className={classes.colItems}>
- {maybe<React.ReactNode>(
- () => menu.items.length,
- <Skeleton />
- )}
- </TableCell>
- <IconButtonTableCell
- disabled={disabled}
- onClick={() => onDelete(menu.id)}
+ menu => {
+ const isSelected = menu ? isChecked(menu.id) : false;
+
+ return (
+ <TableRow
+ hover={!!menu}
+ key={menu ? menu.id : "skeleton"}
+ onClick={menu && onRowClick(menu.id)}
+ className={classes.row}
+ selected={isSelected}
>
- <DeleteIcon />
- </IconButtonTableCell>
- </TableRow>
- ),
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ disabled={disabled}
+ onClick={stopPropagation(() => toggle(menu.id))}
+ />
+ </TableCell>
+ <TableCell className={classes.colTitle}>
+ {maybe<React.ReactNode>(() => menu.name, <Skeleton />)}
+ </TableCell>
+ <TableCell className={classes.colItems}>
+ {maybe<React.ReactNode>(
+ () => menu.items.length,
+ <Skeleton />
+ )}
+ </TableCell>
+ <IconButtonTableCell
+ disabled={disabled}
+ onClick={() => onDelete(menu.id)}
+ >
+ <DeleteIcon />
+ </IconButtonTableCell>
+ </TableRow>
+ );
+ },
() => (
<TableRow>
<TableCell colSpan={4}>{i18n.t("No menus found")}</TableCell>
diff --git a/saleor/static/dashboard-next/navigation/components/MenuListPage/MenuListPage.tsx b/saleor/static/dashboard-next/navigation/components/MenuListPage/MenuListPage.tsx
index 315540b101..6c1c8434b7 100644
--- a/saleor/static/dashboard-next/navigation/components/MenuListPage/MenuListPage.tsx
+++ b/saleor/static/dashboard-next/navigation/components/MenuListPage/MenuListPage.tsx
@@ -1,11 +1,11 @@
import * as React from "react";
import Container from "../../../components/Container";
-import { PageListProps } from "../../../types";
+import { ListActions, PageListProps } from "../../../types";
import { MenuList_menus_edges_node } from "../../types/MenuList";
import MenuList from "../MenuList/MenuList";
-export interface MenuListPageProps extends PageListProps {
+export interface MenuListPageProps extends PageListProps, ListActions {
menus: MenuList_menus_edges_node[];
onDelete: (id: string) => void;
}
diff --git a/saleor/static/dashboard-next/navigation/mutations.ts b/saleor/static/dashboard-next/navigation/mutations.ts
new file mode 100644
index 0000000000..bd03e7313d
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/mutations.ts
@@ -0,0 +1,37 @@
+import gql from "graphql-tag";
+import { TypedMutation } from "../mutations";
+import {
+ MenuBulkDelete,
+ MenuBulkDeleteVariables
+} from "./types/MenuBulkDelete";
+import { MenuDelete, MenuDeleteVariables } from "./types/MenuDelete";
+
+const menuBulkDelete = gql`
+ mutation MenuBulkDelete($ids: [ID]!) {
+ menuBulkDelete(ids: $ids) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const MenuBulkDeleteMutation = TypedMutation<
+ MenuBulkDelete,
+ MenuBulkDeleteVariables
+>(menuBulkDelete);
+
+const menuDelete = gql`
+ mutation MenuDelete($id: ID!) {
+ menuDelete(id: $id) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const MenuDeleteMutation = TypedMutation<
+ MenuDelete,
+ MenuDeleteVariables
+>(menuDelete);
diff --git a/saleor/static/dashboard-next/navigation/types/MenuBulkDelete.ts b/saleor/static/dashboard-next/navigation/types/MenuBulkDelete.ts
new file mode 100644
index 0000000000..b253b78b9f
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/types/MenuBulkDelete.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: MenuBulkDelete
+// ====================================================
+
+export interface MenuBulkDelete_menuBulkDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface MenuBulkDelete_menuBulkDelete {
+ __typename: "MenuBulkDelete";
+ errors: MenuBulkDelete_menuBulkDelete_errors[] | null;
+}
+
+export interface MenuBulkDelete {
+ menuBulkDelete: MenuBulkDelete_menuBulkDelete | null;
+}
+
+export interface MenuBulkDeleteVariables {
+ ids: (string | null)[];
+}
diff --git a/saleor/static/dashboard-next/navigation/types/MenuDelete.ts b/saleor/static/dashboard-next/navigation/types/MenuDelete.ts
new file mode 100644
index 0000000000..c5cf072b56
--- /dev/null
+++ b/saleor/static/dashboard-next/navigation/types/MenuDelete.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: MenuDelete
+// ====================================================
+
+export interface MenuDelete_menuDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface MenuDelete_menuDelete {
+ __typename: "MenuDelete";
+ errors: MenuDelete_menuDelete_errors[] | null;
+}
+
+export interface MenuDelete {
+ menuDelete: MenuDelete_menuDelete | null;
+}
+
+export interface MenuDeleteVariables {
+ id: string;
+}
diff --git a/saleor/static/dashboard-next/navigation/views/MenuList.tsx b/saleor/static/dashboard-next/navigation/views/MenuList.tsx
index 1198d0b735..0b6e63b8f5 100644
--- a/saleor/static/dashboard-next/navigation/views/MenuList.tsx
+++ b/saleor/static/dashboard-next/navigation/views/MenuList.tsx
@@ -1,12 +1,20 @@
+import Button from "@material-ui/core/Button";
+import DialogContentText from "@material-ui/core/DialogContentText";
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 useNotifier from "../../hooks/useNotifier";
import usePaginator from "../../hooks/usePaginator";
-import { maybe } from "../../misc";
+import i18n from "../../i18n";
+import { getMutationState, maybe } from "../../misc";
import MenuListPage from "../components/MenuListPage";
+import { MenuBulkDeleteMutation, MenuDeleteMutation } from "../mutations";
import { MenuListQuery } from "../queries";
+import { MenuBulkDelete } from "../types/MenuBulkDelete";
+import { MenuDelete } from "../types/MenuDelete";
import { menuListUrl, MenuListUrlQueryParams, menuUrl } from "../urls";
const PAGINATE_BY = 20;
@@ -18,42 +26,185 @@ const MenuList: React.FC<MenuListProps> = ({ params }) => {
const navigate = useNavigator();
const notify = useNotifier();
const paginate = usePaginator();
+ const { isSelected, listElements, reset, toggle } = useBulkActions(
+ params.ids
+ );
+
+ const closeModal = () =>
+ navigate(
+ menuListUrl({
+ ...params,
+ action: undefined,
+ id: undefined,
+ ids: undefined
+ }),
+ true
+ );
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<MenuListQuery variables={paginationState}>
- {({ data, loading }) => {
+ {({ data, loading, refetch }) => {
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
maybe(() => data.menus.pageInfo),
paginationState,
params
);
+ const handleBulkDelete = (data: MenuBulkDelete) => {
+ if (data.menuBulkDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Removed menus", {
+ context: "notification"
+ })
+ });
+ closeModal();
+ reset();
+ refetch();
+ }
+ };
+
+ const handleDelete = (data: MenuDelete) => {
+ if (data.menuDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Removed menu", {
+ context: "notification"
+ })
+ });
+ closeModal();
+ refetch();
+ }
+ };
+
return (
- <MenuListPage
- disabled={loading}
- menus={maybe(() => data.menus.edges.map(edge => edge.node))}
- onAdd={() =>
- navigate(
- menuListUrl({
- action: "add"
- })
- )
- }
- onDelete={id =>
- navigate(
- menuListUrl({
- action: "remove",
- id
- })
- )
- }
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () => navigate(menuUrl(id))}
- pageInfo={pageInfo}
- />
+ <MenuDeleteMutation onCompleted={handleDelete}>
+ {(menuDelete, menuDeleteOpts) => (
+ <MenuBulkDeleteMutation onCompleted={handleBulkDelete}>
+ {(menuBulkDelete, menuBulkDeleteOpts) => {
+ const deleteTransitionState = getMutationState(
+ menuDeleteOpts.called,
+ menuDeleteOpts.loading,
+ maybe(() => menuDeleteOpts.data.menuDelete.errors)
+ );
+
+ const bulkDeleteTransitionState = getMutationState(
+ menuBulkDeleteOpts.called,
+ menuBulkDeleteOpts.loading,
+ maybe(() => menuBulkDeleteOpts.data.menuBulkDelete.errors)
+ );
+
+ return (
+ <>
+ <MenuListPage
+ disabled={loading}
+ menus={maybe(() =>
+ data.menus.edges.map(edge => edge.node)
+ )}
+ onAdd={() =>
+ navigate(
+ menuListUrl({
+ action: "add"
+ })
+ )
+ }
+ onDelete={id =>
+ navigate(
+ menuListUrl({
+ action: "remove",
+ id
+ })
+ )
+ }
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => navigate(menuUrl(id))}
+ pageInfo={pageInfo}
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
+ toolbar={
+ <Button
+ color="primary"
+ onClick={() =>
+ navigate(
+ menuListUrl({
+ ...params,
+ action: "remove-many",
+ ids: listElements
+ })
+ )
+ }
+ >
+ {i18n.t("Remove")}
+ </Button>
+ }
+ />
+ <ActionDialog
+ open={params.action === "remove"}
+ onClose={closeModal}
+ confirmButtonState={deleteTransitionState}
+ onConfirm={() =>
+ menuDelete({
+ variables: {
+ id: params.id
+ }
+ })
+ }
+ variant="delete"
+ title={i18n.t("Remove menu")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ name }}</strong>?",
+ {
+ name: maybe(
+ () =>
+ data.menus.edges.find(
+ edge => edge.node.id === params.id
+ ).node.name,
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ <ActionDialog
+ open={params.action === "remove-many"}
+ onClose={closeModal}
+ confirmButtonState={bulkDeleteTransitionState}
+ onConfirm={() =>
+ menuBulkDelete({
+ variables: {
+ ids: params.ids
+ }
+ })
+ }
+ variant="delete"
+ title={i18n.t("Remove menus")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ number }}</strong> menus?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ );
+ }}
+ </MenuBulkDeleteMutation>
+ )}
+ </MenuDeleteMutation>
);
}}
</MenuListQuery>