Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add collection bulk delete
dominik-zeglenecf86ecabffd
diff --git a/saleor/static/dashboard-next/collections/components/CollectionList/CollectionList.tsx b/saleor/static/dashboard-next/collections/components/CollectionList/CollectionList.tsxindex 65e07ac2d7..f8cb9e3e0b 100644--- a/saleor/static/dashboard-next/collections/components/CollectionList/CollectionList.tsx+++ b/saleor/static/dashboard-next/collections/components/CollectionList/CollectionList.tsx@@ -1,19 +1,24 @@+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 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 Skeleton from "../../../components/Skeleton";import StatusLabel from "../../../components/StatusLabel";+import TableHead from "../../../components/TableHead";import TablePagination from "../../../components/TablePagination";+import useBulkActions from "../../../hooks/useBulkActions";import i18n from "../../../i18n";import { maybe, renderCollection } from "../../../misc";-import { ListProps } from "../../../types";+import { ListActionProps, ListProps } from "../../../types";import { CollectionList_collections_edges_node } from "../../types/CollectionList";const styles = createStyles({@@ -31,7 +36,10 @@ const styles = createStyles({}});-interface CollectionListProps extends ListProps, WithStyles<typeof styles> {+interface CollectionListProps+ extends ListProps,+ ListActionProps<"onBulkPublish" | "onBulkUnpublish" | "onBulkDelete">,+ WithStyles<typeof styles> {collections: CollectionList_collections_edges_node[];}@@ -40,89 +48,142 @@ const CollectionList = withStyles(styles, { name: "CollectionList" })(classes,collections,disabled,+ onBulkDelete,+ onBulkPublish,+ onBulkUnpublish,onNextPage,onPreviousPage,onRowClick,pageInfo- }: CollectionListProps) => (- <Card>- <Table>- <TableHead>- <TableRow>- <TableCell className={classes.name}>- {i18n.t("Category Name", { context: "table cell" })}- </TableCell>- <TableCell className={classes.textCenter}>- {i18n- .t("No. Products", { context: "table cell" })- .replace(" ", "\xa0")}- </TableCell>- <TableCell className={classes.textLeft}>- {i18n.t("Availability", { context: "table cell" })}- </TableCell>- </TableRow>- </TableHead>- <TableFooter>- <TableRow>- <TablePagination- colSpan={5}- hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}- onNextPage={onNextPage}- hasPreviousPage={- pageInfo && !disabled ? pageInfo.hasPreviousPage : false- }- onPreviousPage={onPreviousPage}- />- </TableRow>- </TableFooter>- <TableBody>- {renderCollection(- collections,- collection => (- <TableRow- className={classes.tableRow}- hover={!!collection}- onClick={collection ? onRowClick(collection.id) : undefined}- key={collection ? collection.id : "skeleton"}- >- <TableCell>- {maybe<React.ReactNode>(() => collection.name, <Skeleton />)}- </TableCell>- <TableCell className={classes.textCenter}>- {maybe<React.ReactNode>(- () => collection.products.totalCount,- <Skeleton />- )}- </TableCell>- <TableCell className={classes.textLeft}>- {maybe(- () => (- <StatusLabel- status={collection.isPublished ? "success" : "error"}- label={- collection.isPublished- ? i18n.t("Published")- : i18n.t("Not published")- }+ }: CollectionListProps) => {+ const { isMember, listElements, toggle } = useBulkActions(collections);++ return (+ <Card>+ <Table>+ <TableHead+ selected={listElements.length}+ toolbar={+ <>+ <Button+ color="primary"+ onClick={() => onBulkUnpublish(listElements)}+ >+ {i18n.t("Unpublish")}+ </Button>+ <Button+ color="primary"+ onClick={() => onBulkPublish(listElements)}+ >+ {i18n.t("Publish")}+ </Button>+ <IconButton+ color="primary"+ onClick={() => onBulkDelete(listElements)}+ >+ <DeleteIcon />+ </IconButton>+ </>+ }+ >+ <TableRow>+ <TableCell />+ <TableCell className={classes.name}>+ {i18n.t("Category Name", { context: "table cell" })}+ </TableCell>+ <TableCell className={classes.textCenter}>+ {i18n+ .t("No. Products", { context: "table cell" })+ .replace(" ", "\xa0")}+ </TableCell>+ <TableCell className={classes.textLeft}>+ {i18n.t("Availability", { context: "table cell" })}+ </TableCell>+ </TableRow>+ </TableHead>+ <TableFooter>+ <TableRow>+ <TablePagination+ colSpan={5}+ hasNextPage={+ pageInfo && !disabled ? pageInfo.hasNextPage : false+ }+ onNextPage={onNextPage}+ hasPreviousPage={+ pageInfo && !disabled ? pageInfo.hasPreviousPage : false+ }+ onPreviousPage={onPreviousPage}+ />+ </TableRow>+ </TableFooter>+ <TableBody>+ {renderCollection(+ collections,+ collection => {+ const isSelected = collection ? isMember(collection.id) : false;+ return (+ <TableRow+ className={classes.tableRow}+ hover={!!collection}+ onClick={collection ? onRowClick(collection.id) : undefined}+ key={collection ? collection.id : "skeleton"}+ >+ <TableCell padding="checkbox">+ <Checkbox+ color="primary"+ checked={isSelected}+ disabled={disabled}+ onClick={event => {+ toggle(collection.id);+ event.stopPropagation();+ }}/>- ),- <Skeleton />- )}- </TableCell>- </TableRow>- ),- () => (- <TableRow>- <TableCell colSpan={3}>- {i18n.t("No collections found")}- </TableCell>- </TableRow>- )- )}- </TableBody>- </Table>- </Card>- )+ </TableCell>+ <TableCell>+ {maybe<React.ReactNode>(+ () => collection.name,+ <Skeleton />+ )}+ </TableCell>+ <TableCell className={classes.textCenter}>+ {maybe<React.ReactNode>(+ () => collection.products.totalCount,+ <Skeleton />+ )}+ </TableCell>+ <TableCell className={classes.textLeft}>+ {maybe(+ () => (+ <StatusLabel+ status={+ collection.isPublished ? "success" : "error"+ }+ label={+ collection.isPublished+ ? i18n.t("Published")+ : i18n.t("Not published")+ }+ />+ ),+ <Skeleton />+ )}+ </TableCell>+ </TableRow>+ );+ },+ () => (+ <TableRow>+ <TableCell colSpan={3}>+ {i18n.t("No collections found")}+ </TableCell>+ </TableRow>+ )+ )}+ </TableBody>+ </Table>+ </Card>+ );+ });CollectionList.displayName = "CollectionList";export default CollectionList;diff --git a/saleor/static/dashboard-next/collections/components/CollectionListPage/CollectionListPage.tsx b/saleor/static/dashboard-next/collections/components/CollectionListPage/CollectionListPage.tsxindex fc1d47a190..f1d7fc6f3c 100644--- a/saleor/static/dashboard-next/collections/components/CollectionListPage/CollectionListPage.tsx+++ b/saleor/static/dashboard-next/collections/components/CollectionListPage/CollectionListPage.tsx@@ -5,11 +5,13 @@ import * as React from "react";import { Container } from "../../../components/Container";import PageHeader from "../../../components/PageHeader";import i18n from "../../../i18n";-import { PageListProps } from "../../../types";+import { ListActionProps, PageListProps } from "../../../types";import { CollectionList_collections_edges_node } from "../../types/CollectionList";import CollectionList from "../CollectionList/CollectionList";-export interface CollectionListPageProps extends PageListProps {+export interface CollectionListPageProps+ extends PageListProps,+ ListActionProps<"onBulkPublish" | "onBulkUnpublish" | "onBulkDelete"> {collections: CollectionList_collections_edges_node[];}diff --git a/saleor/static/dashboard-next/collections/index.tsx b/saleor/static/dashboard-next/collections/index.tsxindex 5b811df95e..9706de5cea 100644--- a/saleor/static/dashboard-next/collections/index.tsx+++ b/saleor/static/dashboard-next/collections/index.tsx@@ -7,23 +7,19 @@ import i18n from "../i18n";import {collectionAddPath,collectionListPath,+ CollectionListUrlQueryParams,collectionPath,CollectionUrlQueryParams} from "./urls";import CollectionCreate from "./views/CollectionCreate";import CollectionDetailsView from "./views/CollectionDetails";-import CollectionListView, {- CollectionListQueryParams-} from "./views/CollectionList";+import CollectionListView from "./views/CollectionList";const CollectionList: React.StatelessComponent<RouteComponentProps<{}>> = ({location}) => {const qs = parseQs(location.search.substr(1));- const params: CollectionListQueryParams = {- after: qs.after,- before: qs.before- };+ const params: CollectionListUrlQueryParams = qs;return <CollectionListView params={params} />;};diff --git a/saleor/static/dashboard-next/collections/mutations.ts b/saleor/static/dashboard-next/collections/mutations.tsindex 4caf528fd5..4997c7d81e 100644--- a/saleor/static/dashboard-next/collections/mutations.ts+++ b/saleor/static/dashboard-next/collections/mutations.ts@@ -9,6 +9,10 @@ import {CollectionAssignProduct,CollectionAssignProductVariables} from "./types/CollectionAssignProduct";+import {+ CollectionBulkDelete,+ CollectionBulkDeleteVariables+} from "./types/CollectionBulkDelete";import {CollectionUpdate,CollectionUpdateVariables@@ -203,3 +207,18 @@ export const TypedUnassignCollectionProductMutation = TypedMutation<UnassignCollectionProduct,UnassignCollectionProductVariables>(unassignCollectionProduct);++const collectionBulkDelete = gql`+ mutation CollectionBulkDelete($ids: [ID]!) {+ collectionBulkDelete(ids: $ids) {+ errors {+ field+ message+ }+ }+ }+`;+export const TypedCollectionBulkDelete = TypedMutation<+ CollectionBulkDelete,+ CollectionBulkDeleteVariables+>(collectionBulkDelete);diff --git a/saleor/static/dashboard-next/collections/types/CollectionBulkDelete.ts b/saleor/static/dashboard-next/collections/types/CollectionBulkDelete.tsnew file mode 100644index 0000000000..3afafab505--- /dev/null+++ b/saleor/static/dashboard-next/collections/types/CollectionBulkDelete.ts@@ -0,0 +1,26 @@+/* tslint:disable */+/* eslint-disable */+// This file was automatically generated and should not be edited.++// ====================================================+// GraphQL mutation operation: CollectionBulkDelete+// ====================================================++export interface CollectionBulkDelete_collectionBulkDelete_errors {+ __typename: "Error";+ field: string | null;+ message: string | null;+}++export interface CollectionBulkDelete_collectionBulkDelete {+ __typename: "CollectionBulkDelete";+ errors: CollectionBulkDelete_collectionBulkDelete_errors[] | null;+}++export interface CollectionBulkDelete {+ collectionBulkDelete: CollectionBulkDelete_collectionBulkDelete | null;+}++export interface CollectionBulkDeleteVariables {+ ids: (string | null)[];+}diff --git a/saleor/static/dashboard-next/collections/urls.ts b/saleor/static/dashboard-next/collections/urls.tsindex d18942425c..2afe4a32d7 100644--- a/saleor/static/dashboard-next/collections/urls.ts+++ b/saleor/static/dashboard-next/collections/urls.ts@@ -1,12 +1,17 @@import { stringify as stringifyQs } from "qs";import * as urlJoin from "url-join";-import { Dialog, Pagination } from "../types";+import { BulkAction, Dialog, Pagination } from "../types";const collectionSectionUrl = "/collections/";export const collectionListPath = collectionSectionUrl;-export const collectionListUrl = collectionSectionUrl;+export type CollectionListUrlDialog = "publish" | "unpublish" | "remove";+export type CollectionListUrlQueryParams = BulkAction &+ Dialog<CollectionListUrlDialog> &+ Pagination;+export const collectionListUrl = (params?: CollectionListUrlQueryParams) =>+ collectionSectionUrl + "?" + stringifyQs(params);export const collectionPath = (id: string) => urlJoin(collectionSectionUrl, id);export type CollectionUrlDialog = "remove" | "removeImage" | "assign";diff --git a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsxindex 8f381faee2..8497bc77fe 100644--- a/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx+++ b/saleor/static/dashboard-next/collections/views/CollectionDetails.tsx@@ -210,7 +210,7 @@ export const CollectionDetails: React.StatelessComponent<{({ loadNextPage, loadPreviousPage, pageInfo }) => (<CollectionDetailsPageonAdd={() => openModal("assign")}- onBack={() => navigate(collectionListUrl)}+ onBack={() => navigate(collectionListUrl())}disabled={loading}collection={data.collection}isFeatured={maybe(diff --git a/saleor/static/dashboard-next/collections/views/CollectionList.tsx b/saleor/static/dashboard-next/collections/views/CollectionList.tsxindex ac43250b7d..3ab6bd19b3 100644--- a/saleor/static/dashboard-next/collections/views/CollectionList.tsx+++ b/saleor/static/dashboard-next/collections/views/CollectionList.tsx@@ -1,53 +1,189 @@+import DialogContentText from "@material-ui/core/DialogContentText";import * as React from "react";-import Navigator from "../../components/Navigator";-import { createPaginationState, Paginator } from "../../components/Paginator";-import { maybe } from "../../misc";-import { Pagination } from "../../types";+import ActionDialog from "../../components/ActionDialog";+import { createPaginationState } from "../../components/Paginator";+import useNavigator from "../../hooks/useNavigator";+import useNotifier from "../../hooks/useNotifier";+import usePaginator from "../../hooks/usePaginator";+import i18n from "../../i18n";+import { getMutationState, maybe } from "../../misc";import CollectionListPage from "../components/CollectionListPage/CollectionListPage";+import { TypedCollectionBulkDelete } from "../mutations";import { TypedCollectionListQuery } from "../queries";-import { collectionAddUrl, collectionUrl } from "../urls";--export type CollectionListQueryParams = Pagination;+import { CollectionBulkDelete } from "../types/CollectionBulkDelete";+import {+ collectionAddUrl,+ collectionListUrl,+ CollectionListUrlDialog,+ CollectionListUrlQueryParams,+ collectionUrl+} from "../urls";interface CollectionListProps {- params: CollectionListQueryParams;+ params: CollectionListUrlQueryParams;}const PAGINATE_BY = 20;export const CollectionList: React.StatelessComponent<CollectionListProps> = ({params-}) => (- <Navigator>- {navigate => {- const paginationState = createPaginationState(PAGINATE_BY, params);- return (- <TypedCollectionListQuery displayLoader variables={paginationState}>- {({ data, loading }) => (- <Paginator- pageInfo={maybe(() => data.collections.pageInfo)}- paginationState={paginationState}- queryString={params}- >- {({ loadNextPage, loadPreviousPage, pageInfo }) => (- <CollectionListPage- onAdd={() => navigate(collectionAddUrl)}- disabled={loading}- collections={maybe(() =>- data.collections.edges.map(edge => edge.node)- )}- onNextPage={loadNextPage}- onPreviousPage={loadPreviousPage}- pageInfo={pageInfo}- onRowClick={id => () => navigate(collectionUrl(id))}- />- )}- </Paginator>- )}- </TypedCollectionListQuery>- );- }}- </Navigator>-);+}) => {+ const navigate = useNavigator();+ const notify = useNotifier();+ const paginate = usePaginator();++ const closeModal = () =>+ navigate(+ collectionListUrl({+ ...params,+ action: undefined,+ ids: undefined+ }),+ true+ );++ const openModal = (action: CollectionListUrlDialog, ids: string[]) =>+ navigate(+ collectionListUrl({+ action,+ ids+ })+ );++ const paginationState = createPaginationState(PAGINATE_BY, params);+ return (+ <TypedCollectionListQuery displayLoader variables={paginationState}>+ {({ data, loading, refetch }) => {+ const { loadNextPage, loadPreviousPage, pageInfo } = paginate(+ maybe(() => data.collections.pageInfo),+ paginationState,+ params+ );++ const handleCollectionBulkDelete = (data: CollectionBulkDelete) => {+ if (data.collectionBulkDelete.errors.length === 0) {+ notify({+ text: i18n.t("Removed collections")+ });+ refetch();+ navigate(+ collectionListUrl({+ ...params,+ action: undefined,+ ids: undefined+ })+ );+ }+ };++ return (+ <TypedCollectionBulkDelete onCompleted={handleCollectionBulkDelete}>+ {(collectionBulkDelete, collectionBulkDeleteOpts) => {+ const bulkDeleteTransitionState = getMutationState(+ collectionBulkDeleteOpts.called,+ collectionBulkDeleteOpts.loading,+ maybe(+ () =>+ collectionBulkDeleteOpts.data.collectionBulkDelete.errors+ )+ );++ return (+ <>+ <CollectionListPage+ onAdd={() => navigate(collectionAddUrl)}+ disabled={loading}+ collections={maybe(() =>+ data.collections.edges.map(edge => edge.node)+ )}+ onBulkDelete={ids => openModal("remove", ids)}+ onBulkPublish={ids => openModal("publish", ids)}+ onBulkUnpublish={ids => openModal("unpublish", ids)}+ onNextPage={loadNextPage}+ onPreviousPage={loadPreviousPage}+ pageInfo={pageInfo}+ onRowClick={id => () => navigate(collectionUrl(id))}+ />+ <ActionDialog+ open={params.action === "publish"}+ onClose={closeModal}+ confirmButtonState={"default"}+ onConfirm={() => undefined}+ variant="default"+ title={i18n.t("Publish collections")}+ >+ <DialogContentText+ dangerouslySetInnerHTML={{+ __html: i18n.t(+ "Are you sure you want to publish <strong>{{ number }}</strong> collections?",+ {+ number: maybe(+ () => params.ids.length.toString(),+ "..."+ )+ }+ )+ }}+ />+ </ActionDialog>+ <ActionDialog+ open={params.action === "unpublish"}+ onClose={closeModal}+ confirmButtonState={"default"}+ onConfirm={() => undefined}+ variant="default"+ title={i18n.t("Unpublish collections")}+ >+ <DialogContentText+ dangerouslySetInnerHTML={{+ __html: i18n.t(+ "Are you sure you want to unpublish <strong>{{ number }}</strong> collections?",+ {+ number: maybe(+ () => params.ids.length.toString(),+ "..."+ )+ }+ )+ }}+ />+ </ActionDialog>+ <ActionDialog+ open={params.action === "remove"}+ onClose={closeModal}+ confirmButtonState={bulkDeleteTransitionState}+ onConfirm={() =>+ collectionBulkDelete({+ variables: {+ ids: params.ids+ }+ })+ }+ variant="delete"+ title={i18n.t("Remove collections")}+ >+ <DialogContentText+ dangerouslySetInnerHTML={{+ __html: i18n.t(+ "Are you sure you want to remove <strong>{{ number }}</strong> collections?",+ {+ number: maybe(+ () => params.ids.length.toString(),+ "..."+ )+ }+ )+ }}+ />+ </ActionDialog>+ </>+ );+ }}+ </TypedCollectionBulkDelete>+ );+ }}+ </TypedCollectionListQuery>+ );+};export default CollectionList;diff --git a/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts b/saleor/static/dashboard-next/components/AppLayout/menuStructure.tsindex a7c8b5fb19..81d275c7e5 100644--- a/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts+++ b/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts@@ -38,7 +38,7 @@ const menuStructure: IMenuItem[] = [{ariaLabel: "collections",label: i18n.t("Collections", { context: "Menu label" }),- url: collectionListUrl+ url: collectionListUrl()}],label: i18n.t("Catalogue", { context: "Menu label" }),diff --git a/saleor/static/dashboard-next/configuration/index.tsx b/saleor/static/dashboard-next/configuration/index.tsxindex fc0c5b2710..9f2998df6d 100644--- a/saleor/static/dashboard-next/configuration/index.tsx+++ b/saleor/static/dashboard-next/configuration/index.tsx@@ -67,7 +67,7 @@ export const configurationMenu: MenuItem[] = [icon: <Pages fontSize="inherit" viewBox="0 0 44 44" />,permission: PermissionEnum.MANAGE_PAGES,title: i18n.t("Pages"),- url: pageListUrl+ url: pageListUrl()}];diff --git a/saleor/static/dashboard-next/pages/urls.ts b/saleor/static/dashboard-next/pages/urls.tsindex 6fa38314b1..a70ae7716e 100644--- a/saleor/static/dashboard-next/pages/urls.ts+++ b/saleor/static/dashboard-next/pages/urls.ts@@ -10,7 +10,7 @@ export type PageListUrlDialog = "remove";export type PageListUrlQueryParams = BulkAction &Dialog<PageListUrlDialog> &Pagination;-export const pageListUrl = (params: PageListUrlQueryParams) =>+export const pageListUrl = (params?: PageListUrlQueryParams) =>pageListPath + "?" + stringifyQs(params);export const pagePath = (id: string) => urlJoin(pagesSection, id);