Skip to content

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

Add bulk product delete to category details

dominik-zeglenbbe5a0788101
diff --git a/saleor/static/dashboard-next/categories/components/CategoryProductsCard/CategoryProductsCard.tsx b/saleor/static/dashboard-next/categories/components/CategoryProductsCard/CategoryProductsCard.tsx
index afd6605357..cea7c78d6c 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryProductsCard/CategoryProductsCard.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryProductsCard/CategoryProductsCard.tsx
@@ -1,14 +1,19 @@
import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
+import IconButton from "@material-ui/core/IconButton";
+import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
import CardTitle from "../../../components/CardTitle";
import ProductList from "../../../components/ProductList";
+import useBulkActions from "../../../hooks/useBulkActions";
import i18n from "../../../i18n";
-import { PageListProps } from "../../../types";
+import { ListActionProps, PageListProps } from "../../../types";
import { CategoryDetails_category_products_edges_node } from "../../types/CategoryDetails";
-interface CategoryProductsCardProps extends PageListProps {
+interface CategoryProductsCardProps
+ extends PageListProps,
+ ListActionProps<"onBulkDelete"> {
products: CategoryDetails_category_products_edges_node[];
categoryName: string;
}
@@ -20,30 +25,50 @@ export const CategoryProductsCard: React.StatelessComponent<
disabled,
pageInfo,
onAdd,
+ onBulkDelete,
onNextPage,
onPreviousPage,
onRowClick,
categoryName
-}) => (
- <Card>
- <CardTitle
- title={i18n.t("Products in {{ categoryName }}", { categoryName })}
- toolbar={
- <Button color="primary" variant="text" onClick={onAdd}>
- {i18n.t("Add product")}
- </Button>
- }
- />
- <ProductList
- products={products}
- disabled={disabled}
- pageInfo={pageInfo}
- onNextPage={onNextPage}
- onPreviousPage={onPreviousPage}
- onRowClick={onRowClick}
- />
- </Card>
-);
+}) => {
+ const { isMember: isChecked, listElements, toggle } = useBulkActions(
+ products
+ );
+
+ return (
+ <Card>
+ <CardTitle
+ title={i18n.t("Products in {{ categoryName }}", { categoryName })}
+ toolbar={
+ <Button color="primary" variant="text" onClick={onAdd}>
+ {i18n.t("Add product")}
+ </Button>
+ }
+ />
+ <ProductList
+ products={products}
+ disabled={disabled}
+ pageInfo={pageInfo}
+ onNextPage={onNextPage}
+ onPreviousPage={onPreviousPage}
+ onRowClick={onRowClick}
+ selected={listElements.length}
+ isChecked={isChecked}
+ toggle={toggle}
+ toolbar={
+ <>
+ <IconButton
+ color="primary"
+ onClick={() => onBulkDelete(listElements)}
+ >
+ <DeleteIcon />
+ </IconButton>
+ </>
+ }
+ />
+ </Card>
+ );
+};
CategoryProductsCard.displayName = "CategoryProductsCard";
export default CategoryProductsCard;
diff --git a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
index a0028d8d8a..818976394a 100644
--- a/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
+++ b/saleor/static/dashboard-next/categories/components/CategoryUpdatePage/CategoryUpdatePage.tsx
@@ -13,7 +13,7 @@ import { Tab } from "../../../components/Tab";
import TabContainer from "../../../components/Tab/TabContainer";
import i18n from "../../../i18n";
import { maybe } from "../../../misc";
-import { UserError } from "../../../types";
+import { UserError, ListActionProps } from "../../../types";
import CategoryDetailsForm from "../../components/CategoryDetailsForm";
import CategoryList from "../../components/CategoryList";
import {
@@ -37,7 +37,8 @@ export enum CategoryPageTab {
products = "products"
}
-export interface CategoryUpdatePageProps {
+export interface CategoryUpdatePageProps
+ extends ListActionProps<"onBulkProductDelete"> {
changeTab: (index: CategoryPageTab) => void;
currentTab: CategoryPageTab;
errors: UserError[];
@@ -81,6 +82,7 @@ export const CategoryUpdatePage: React.StatelessComponent<
onAddCategory,
onAddProduct,
onBack,
+ onBulkProductDelete,
onCategoryClick,
onDelete,
onNextPage,
@@ -174,6 +176,7 @@ export const CategoryUpdatePage: React.StatelessComponent<
)}
{currentTab === CategoryPageTab.products && (
<CategoryProductsCard
+ onBulkDelete={onBulkProductDelete}
categoryName={maybe(() => category.name)}
products={products}
disabled={disabled}
diff --git a/saleor/static/dashboard-next/categories/urls.ts b/saleor/static/dashboard-next/categories/urls.ts
index 60e99eb72b..abc6a6ab8d 100644
--- a/saleor/static/dashboard-next/categories/urls.ts
+++ b/saleor/static/dashboard-next/categories/urls.ts
@@ -1,7 +1,7 @@
import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
-import { ActiveTab, Dialog, Pagination } from "../types";
+import { ActiveTab, BulkAction, Dialog, Pagination } from "../types";
import { CategoryPageTab } from "./components/CategoryUpdatePage";
const categorySectionUrl = "/categories/";
@@ -10,7 +10,12 @@ export const categoryListPath = categorySectionUrl;
export const categoryListUrl = categorySectionUrl;
export const categoryPath = (id: string) => urlJoin(categorySectionUrl, id);
-export type CategoryUrlQueryParams = Dialog<"delete"> &
+export type CategoryUrlDialog =
+ | "delete"
+ | "delete-categories"
+ | "delete-products";
+export type CategoryUrlQueryParams = BulkAction &
+ Dialog<CategoryUrlDialog> &
Pagination &
ActiveTab<CategoryPageTab>;
export const categoryUrl = (id: string, params?: CategoryUrlQueryParams) =>
diff --git a/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
index 1db0f093fb..32697fb264 100644
--- a/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
+++ b/saleor/static/dashboard-next/categories/views/CategoryDetails.tsx
@@ -1,14 +1,16 @@
import DialogContentText from "@material-ui/core/DialogContentText";
-import { stringify as stringifyQs } from "qs";
import * as React from "react";
import ActionDialog from "../../components/ActionDialog";
-import { createPaginationState, Paginator } from "../../components/Paginator";
+import { createPaginationState } from "../../components/Paginator";
import { WindowTitle } from "../../components/WindowTitle";
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 { TypedProductBulkDeleteMutation } from "../../products/mutations";
+import { productBulkDelete } from "../../products/types/productBulkDelete";
import { productAddUrl, productUrl } from "../../products/urls";
import { CategoryInput } from "../../types/globalTypes";
import {
@@ -26,6 +28,7 @@ import {
categoryAddUrl,
categoryListUrl,
categoryUrl,
+ CategoryUrlDialog,
CategoryUrlQueryParams
} from "../urls";
@@ -47,6 +50,7 @@ export const CategoryDetails: React.StatelessComponent<
> = ({ id, params }) => {
const navigate = useNavigator();
const notify = useNotifier();
+ const paginate = usePaginator();
const handleCategoryDelete = (data: CategoryDelete) => {
if (data.categoryDelete.errors.length === 0) {
@@ -73,10 +77,28 @@ export const CategoryDetails: React.StatelessComponent<
const changeTab = (tabName: CategoryPageTab) =>
navigate(
- "?" +
- stringifyQs({
- activeTab: tabName
- })
+ categoryUrl(id, {
+ activeTab: tabName
+ })
+ );
+
+ const closeModal = () =>
+ navigate(
+ categoryUrl(id, {
+ ...params,
+ action: undefined,
+ ids: undefined
+ }),
+ true
+ );
+
+ const openModal = (action: CategoryUrlDialog, ids?: string[]) =>
+ navigate(
+ categoryUrl(id, {
+ ...params,
+ action,
+ ids
+ })
);
return (
@@ -101,128 +123,184 @@ export const CategoryDetails: React.StatelessComponent<
variables={{ ...paginationState, id }}
require={["category"]}
>
- {({ data, loading }) => (
- <>
- <WindowTitle title={maybe(() => data.category.name)} />
- <Paginator
- pageInfo={maybe(() => data.category.products.pageInfo)}
- paginationState={paginationState}
- queryString={params}
- >
- {({ loadNextPage, loadPreviousPage, pageInfo }) => (
- <CategoryUpdatePage
- changeTab={changeTab}
- currentTab={params.activeTab}
- category={maybe(() => data.category)}
- disabled={loading}
- errors={maybe(
- () => updateResult.data.categoryUpdate.errors
- )}
- onAddCategory={() => navigate(categoryAddUrl(id))}
- onAddProduct={() => navigate(productAddUrl)}
- onBack={() =>
- navigate(
- maybe(
- () => categoryUrl(data.category.parent.id),
- categoryListUrl
- )
- )
- }
- onCategoryClick={id => () =>
- navigate(categoryUrl(id))}
- onDelete={() =>
- navigate(
- categoryUrl(id, {
- ...params,
- action: "delete"
- })
+ {({ data, loading, refetch }) => {
+ const handleBulkProductDelete = (data: productBulkDelete) => {
+ if (data.productBulkDelete.errors.length === 0) {
+ closeModal();
+ notify({
+ text: i18n.t("Products removed")
+ });
+ refetch();
+ }
+ };
+
+ const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
+ maybe(() => data.category.products.pageInfo),
+ paginationState,
+ params
+ );
+
+ return (
+ <>
+ <WindowTitle title={maybe(() => data.category.name)} />
+ <TypedProductBulkDeleteMutation
+ onCompleted={handleBulkProductDelete}
+ >
+ {(productBulkDelete, productBulkDeleteOpts) => {
+ const bulkDeleteMutationState = getMutationState(
+ productBulkDeleteOpts.called,
+ productBulkDeleteOpts.loading,
+ maybe(
+ () =>
+ productBulkDeleteOpts.data.productBulkDelete
+ .errors
)
- }
- onImageDelete={() =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImage: null
+ );
+
+ return (
+ <>
+ <CategoryUpdatePage
+ changeTab={changeTab}
+ currentTab={params.activeTab}
+ category={maybe(() => data.category)}
+ disabled={loading}
+ errors={maybe(
+ () => updateResult.data.categoryUpdate.errors
+ )}
+ onAddCategory={() =>
+ navigate(categoryAddUrl(id))
+ }
+ onAddProduct={() => navigate(productAddUrl)}
+ onBack={() =>
+ navigate(
+ maybe(
+ () =>
+ categoryUrl(data.category.parent.id),
+ categoryListUrl
+ )
+ )
+ }
+ onBulkProductDelete={ids =>
+ openModal("delete-products", ids)
+ }
+ onCategoryClick={id => () =>
+ navigate(categoryUrl(id))}
+ onDelete={() => openModal("delete")}
+ onImageDelete={() =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: null
+ }
+ }
+ })
+ }
+ onImageUpload={file =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImage: file
+ }
+ }
+ })
+ }
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ pageInfo={pageInfo}
+ onProductClick={id => () =>
+ navigate(productUrl(id))}
+ onSubmit={formData =>
+ updateCategory({
+ variables: {
+ id,
+ input: {
+ backgroundImageAlt:
+ formData.backgroundImageAlt,
+ descriptionJson: JSON.stringify(
+ formData.description
+ ),
+ name: formData.name,
+ seo: {
+ description: formData.seoDescription,
+ title: formData.seoTitle
+ }
+ }
+ }
+ })
}
- }
- })
- }
- onImageUpload={file =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImage: file
+ products={maybe(() =>
+ data.category.products.edges.map(
+ edge => edge.node
+ )
+ )}
+ saveButtonBarState={formTransitionState}
+ subcategories={maybe(() =>
+ data.category.children.edges.map(
+ edge => edge.node
+ )
+ )}
+ />
+ <ActionDialog
+ confirmButtonState={removeDialogTransitionState}
+ onClose={() => closeModal}
+ onConfirm={() =>
+ deleteCategory({ variables: { id } })
}
- }
- })
- }
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- pageInfo={pageInfo}
- onProductClick={id => () => navigate(productUrl(id))}
- onSubmit={formData =>
- updateCategory({
- variables: {
- id,
- input: {
- backgroundImageAlt:
- formData.backgroundImageAlt,
- descriptionJson: JSON.stringify(
- formData.description
- ),
- name: formData.name,
- seo: {
- description: formData.seoDescription,
- title: formData.seoTitle
- }
+ open={params.action === "delete"}
+ title={i18n.t("Delete category", {
+ context: "modal title"
+ })}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ categoryName }}</strong>?",
+ {
+ categoryName: maybe(
+ () => data.category.name
+ ),
+ context: "modal message"
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ <ActionDialog
+ open={params.action === "delete-products"}
+ confirmButtonState={bulkDeleteMutationState}
+ onClose={closeModal}
+ onConfirm={() =>
+ productBulkDelete({
+ variables: { ids: params.ids }
+ })
}
- }
- })
- }
- products={maybe(() =>
- data.category.products.edges.map(edge => edge.node)
- )}
- saveButtonBarState={formTransitionState}
- subcategories={maybe(() =>
- data.category.children.edges.map(edge => edge.node)
- )}
- />
- )}
- </Paginator>
- <ActionDialog
- confirmButtonState={removeDialogTransitionState}
- onClose={() =>
- navigate(
- categoryUrl(id, {
- ...params,
- action: undefined
- }),
- true
- )
- }
- onConfirm={() => deleteCategory({ variables: { id } })}
- open={params.action === "delete"}
- title={i18n.t("Delete category", {
- context: "modal title"
- })}
- variant="delete"
- >
- <DialogContentText
- dangerouslySetInnerHTML={{
- __html: i18n.t(
- "Are you sure you want to remove <strong>{{ categoryName }}</strong>?",
- {
- categoryName: maybe(() => data.category.name),
- context: "modal message"
- }
- )
+ title={i18n.t("Remove products")}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ number }}</strong> products?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ );
}}
- />
- </ActionDialog>
- </>
- )}
+ </TypedProductBulkDeleteMutation>
+ </>
+ );
+ }}
</TypedCategoryDetailsQuery>
);
}}
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index a7a2d90713..95af7bd024 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -3,7 +3,6 @@ import Card from "@material-ui/core/Card";
import IconButton from "@material-ui/core/IconButton";
import AddIcon from "@material-ui/icons/Add";
import DeleteIcon from "@material-ui/icons/Delete";
-import * as CRC from "crc-32";
import * as React from "react";
import { CategoryDetails_category_products_edges_node } from "../../../categories/types/CategoryDetails";
@@ -12,9 +11,7 @@ import PageHeader from "../../../components/PageHeader";
import ProductList from "../../../components/ProductList";
import { Filter } from "../../../components/TableFilter";
import useBulkActions from "../../../hooks/useBulkActions";
-import useListSelector from "../../../hooks/useListSelector";
import i18n from "../../../i18n";
-import { maybe } from "../../../misc";
import { ListActionProps, PageListProps } from "../../../types";
import ProductListFilter, { ProductListFilterTabs } from "../ProductListFilter";
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index 5ab237296f..1f7e972650 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -54,3 +54,6 @@ export type ActiveTab<TTab extends string> = Partial<{
export type SingleAction = Partial<{
id: string;
}>;
+export type BulkAction = Partial<{
+ ids: string[];
+}>;