Skip to content

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

Add customer bulk delete

dominik-zeglen1639a708c800
diff --git a/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts b/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts
index 81d275c7e5..3515a54e7e 100644
--- a/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts
+++ b/saleor/static/dashboard-next/components/AppLayout/menuStructure.ts
@@ -67,7 +67,7 @@ const menuStructure: IMenuItem[] = [
ariaLabel: "customers",
label: i18n.t("Customers", { context: "Menu label" }),
permission: PermissionEnum.MANAGE_USERS,
- url: customerListUrl
+ url: customerListUrl()
},
{
diff --git a/saleor/static/dashboard-next/customers/components/CustomerList/CustomerList.tsx b/saleor/static/dashboard-next/customers/components/CustomerList/CustomerList.tsx
index db7c2e49aa..02790f2c65 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerList/CustomerList.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerList/CustomerList.tsx
@@ -1,18 +1,19 @@
import Card from "@material-ui/core/Card";
+import Checkbox from "@material-ui/core/Checkbox";
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 * as React from "react";
import Skeleton from "../../../components/Skeleton";
+import TableHead from "../../../components/TableHead";
import TablePagination from "../../../components/TablePagination";
import i18n from "../../../i18n";
import { getUserName, maybe, renderCollection } from "../../../misc";
-import { ListProps } from "../../../types";
+import { ListActions, ListProps } from "../../../types";
import { ListCustomers_customers_edges_node } from "../../types/ListCustomers";
const styles = createStyles({
@@ -29,6 +30,7 @@ const styles = createStyles({
export interface CustomerListProps
extends ListProps,
+ ListActions,
WithStyles<typeof styles> {
customers: ListCustomers_customers_edges_node[];
}
@@ -41,12 +43,17 @@ const CustomerList = withStyles(styles, { name: "CustomerList" })(
pageInfo,
onNextPage,
onPreviousPage,
- onRowClick
+ onRowClick,
+ toolbar,
+ toggle,
+ selected,
+ isChecked
}: CustomerListProps) => (
<Card>
<Table>
- <TableHead>
+ <TableHead selected={selected} toolbar={toolbar}>
<TableRow>
+ <TableCell />
<TableCell>
{i18n.t("Customer Name", { context: "table header" })}
</TableCell>
@@ -61,7 +68,7 @@ const CustomerList = withStyles(styles, { name: "CustomerList" })(
<TableFooter>
<TableRow>
<TablePagination
- colSpan={3}
+ colSpan={4}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
@@ -74,31 +81,46 @@ const CustomerList = withStyles(styles, { name: "CustomerList" })(
<TableBody>
{renderCollection(
customers,
- customer => (
- <TableRow
- className={!!customer ? classes.tableRow : undefined}
- hover={!!customer}
- key={customer ? customer.id : "skeleton"}
- >
- <TableCell
- onClick={customer ? onRowClick(customer.id) : undefined}
+ customer => {
+ const isSelected = customer ? isChecked(customer.id) : false;
+
+ return (
+ <TableRow
+ className={!!customer ? classes.tableRow : undefined}
+ hover={!!customer}
+ key={customer ? customer.id : "skeleton"}
>
- {getUserName(customer)}
- </TableCell>
- <TableCell>
- {maybe<React.ReactNode>(() => customer.email, <Skeleton />)}
- </TableCell>
- <TableCell className={classes.textCenter}>
- {maybe<React.ReactNode>(
- () => customer.orders.totalCount,
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
- ),
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ checked={isSelected}
+ disabled={disabled}
+ onClick={event => {
+ toggle(customer.id);
+ event.stopPropagation();
+ }}
+ />
+ </TableCell>
+ <TableCell
+ onClick={customer ? onRowClick(customer.id) : undefined}
+ >
+ {getUserName(customer)}
+ </TableCell>
+ <TableCell>
+ {maybe<React.ReactNode>(() => customer.email, <Skeleton />)}
+ </TableCell>
+ <TableCell className={classes.textCenter}>
+ {maybe<React.ReactNode>(
+ () => customer.orders.totalCount,
+ <Skeleton />
+ )}
+ </TableCell>
+ </TableRow>
+ );
+ },
() => (
<TableRow>
- <TableCell colSpan={3}>
+ <TableCell colSpan={4}>
{i18n.t("No customers found")}
</TableCell>
</TableRow>
diff --git a/saleor/static/dashboard-next/customers/components/CustomerListPage/CustomerListPage.tsx b/saleor/static/dashboard-next/customers/components/CustomerListPage/CustomerListPage.tsx
index fd70648705..fefae26181 100644
--- a/saleor/static/dashboard-next/customers/components/CustomerListPage/CustomerListPage.tsx
+++ b/saleor/static/dashboard-next/customers/components/CustomerListPage/CustomerListPage.tsx
@@ -5,11 +5,11 @@ import * as React from "react";
import Container from "../../../components/Container";
import PageHeader from "../../../components/PageHeader";
import i18n from "../../../i18n";
-import { PageListProps } from "../../../types";
+import { ListActions, PageListProps } from "../../../types";
import { ListCustomers_customers_edges_node } from "../../types/ListCustomers";
import CustomerList from "../CustomerList/CustomerList";
-export interface CustomerListPageProps extends PageListProps {
+export interface CustomerListPageProps extends PageListProps, ListActions {
customers: ListCustomers_customers_edges_node[];
}
diff --git a/saleor/static/dashboard-next/customers/index.tsx b/saleor/static/dashboard-next/customers/index.tsx
index b097b13642..b06576887b 100644
--- a/saleor/static/dashboard-next/customers/index.tsx
+++ b/saleor/static/dashboard-next/customers/index.tsx
@@ -9,24 +9,20 @@ import {
customerAddressesPath,
CustomerAddressesUrlQueryParams,
customerListPath,
+ CustomerListUrlQueryParams,
customerPath,
CustomerUrlQueryParams
} from "./urls";
import CustomerAddressesViewComponent from "./views/CustomerAddresses";
import CustomerCreateView from "./views/CustomerCreate";
import CustomerDetailsViewComponent from "./views/CustomerDetails";
-import CustomerListViewComponent, {
- CustomerListQueryParams
-} from "./views/CustomerList";
+import CustomerListViewComponent from "./views/CustomerList";
const CustomerListView: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params: CustomerListQueryParams = {
- after: qs.after,
- before: qs.before
- };
+ const params: CustomerListUrlQueryParams = qs;
return <CustomerListViewComponent params={params} />;
};
diff --git a/saleor/static/dashboard-next/customers/mutations.ts b/saleor/static/dashboard-next/customers/mutations.ts
index a2ef027b00..1c80da368a 100644
--- a/saleor/static/dashboard-next/customers/mutations.ts
+++ b/saleor/static/dashboard-next/customers/mutations.ts
@@ -3,6 +3,10 @@ import gql from "graphql-tag";
import { TypedMutation } from "../mutations";
import { fragmentAddress } from "../orders/queries";
import { customerAddressesFragment, customerDetailsFragment } from "./queries";
+import {
+ BulkRemoveCustomers,
+ BulkRemoveCustomersVariables
+} from "./types/BulkRemoveCustomers";
import {
CreateCustomer,
CreateCustomerVariables
@@ -167,3 +171,18 @@ export const TypedRemoveCustomerAddressMutation = TypedMutation<
RemoveCustomerAddress,
RemoveCustomerAddressVariables
>(removeCustomerAddress);
+
+export const bulkRemoveCustomers = gql`
+ mutation BulkRemoveCustomers($ids: [ID]!) {
+ customerBulkDelete(ids: $ids) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedBulkRemoveCustomers = TypedMutation<
+ BulkRemoveCustomers,
+ BulkRemoveCustomersVariables
+>(bulkRemoveCustomers);
diff --git a/saleor/static/dashboard-next/customers/types/BulkRemoveCustomers.ts b/saleor/static/dashboard-next/customers/types/BulkRemoveCustomers.ts
new file mode 100644
index 0000000000..06251bbbc7
--- /dev/null
+++ b/saleor/static/dashboard-next/customers/types/BulkRemoveCustomers.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: BulkRemoveCustomers
+// ====================================================
+
+export interface BulkRemoveCustomers_customerBulkDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface BulkRemoveCustomers_customerBulkDelete {
+ __typename: "CustomerBulkDelete";
+ errors: BulkRemoveCustomers_customerBulkDelete_errors[] | null;
+}
+
+export interface BulkRemoveCustomers {
+ customerBulkDelete: BulkRemoveCustomers_customerBulkDelete | null;
+}
+
+export interface BulkRemoveCustomersVariables {
+ ids: (string | null)[];
+}
diff --git a/saleor/static/dashboard-next/customers/urls.ts b/saleor/static/dashboard-next/customers/urls.ts
index 5cc8373195..53662c0f5b 100644
--- a/saleor/static/dashboard-next/customers/urls.ts
+++ b/saleor/static/dashboard-next/customers/urls.ts
@@ -1,12 +1,17 @@
import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
-import { Dialog, SingleAction } from "../types";
+import { BulkAction, Dialog, Pagination, SingleAction } from "../types";
export const customerSection = "/customers/";
export const customerListPath = customerSection;
-export const customerListUrl = customerListPath;
+export type CustomerListUrlDialog = "remove";
+export type CustomerListUrlQueryParams = BulkAction &
+ Dialog<CustomerListUrlDialog> &
+ Pagination;
+export const customerListUrl = (params?: CustomerListUrlQueryParams) =>
+ customerListPath + "?" + stringifyQs(params);
export const customerPath = (id: string) => urlJoin(customerSection, id);
export type CustomerUrlDialog = "remove";
diff --git a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
index f148d91e61..f9b6eaa6de 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerCreate.tsx
@@ -53,7 +53,7 @@ export const CustomerCreate: React.StatelessComponent<{}> = () => (
saveButtonBar={
createCustomerOpts.loading ? "loading" : "default"
}
- onBack={() => navigate(customerListUrl)}
+ onBack={() => navigate(customerListUrl())}
onSubmit={formData => {
const address = {
city: formData.city,
diff --git a/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx b/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx
index c00958a24a..16bb4c3854 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerDetails.tsx
@@ -50,7 +50,7 @@ export const CustomerDetailsView: React.StatelessComponent<
context: "notification"
})
});
- navigate(customerListUrl);
+ navigate(customerListUrl());
}
};
return (
@@ -97,7 +97,7 @@ export const CustomerDetailsView: React.StatelessComponent<
onAddressManageClick={() =>
navigate(customerAddressesUrl(id))
}
- onBack={() => navigate(customerListUrl)}
+ onBack={() => navigate(customerListUrl())}
onRowClick={id => navigate(orderUrl(id))}
onSubmit={formData =>
updateCustomer({
diff --git a/saleor/static/dashboard-next/customers/views/CustomerList.tsx b/saleor/static/dashboard-next/customers/views/CustomerList.tsx
index cfcb938ec7..01c02ca869 100644
--- a/saleor/static/dashboard-next/customers/views/CustomerList.tsx
+++ b/saleor/static/dashboard-next/customers/views/CustomerList.tsx
@@ -1,49 +1,147 @@
+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 useNotifier from "../../hooks/useNotifier";
import usePaginator from "../../hooks/usePaginator";
-import { maybe } from "../../misc";
-import { Pagination } from "../../types";
+import i18n from "../../i18n";
+import { getMutationState, maybe } from "../../misc";
import CustomerListPage from "../components/CustomerListPage";
+import { TypedBulkRemoveCustomers } from "../mutations";
import { TypedCustomerListQuery } from "../queries";
-import { customerAddUrl, customerUrl } from "../urls";
+import { BulkRemoveCustomers } from "../types/BulkRemoveCustomers";
+import {
+ customerAddUrl,
+ customerListUrl,
+ CustomerListUrlQueryParams,
+ customerUrl
+} from "../urls";
const PAGINATE_BY = 20;
-export type CustomerListQueryParams = Pagination;
-
interface CustomerListProps {
- params: CustomerListQueryParams;
+ params: CustomerListUrlQueryParams;
}
export const CustomerList: React.StatelessComponent<CustomerListProps> = ({
params
}) => {
const navigate = useNavigator();
+ const notify = useNotifier();
const paginate = usePaginator();
+ const { isSelected, listElements, reset, toggle } = useBulkActions();
+
+ const closeModal = () =>
+ navigate(
+ customerListUrl({
+ ...params,
+ action: undefined,
+ ids: undefined
+ }),
+ true
+ );
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<TypedCustomerListQuery displayLoader variables={paginationState}>
- {({ data, loading }) => {
+ {({ data, loading, refetch }) => {
const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
maybe(() => data.customers.pageInfo),
paginationState,
params
);
+ const handleBulkCustomerDelete = (data: BulkRemoveCustomers) => {
+ if (data.customerBulkDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Customers removed")
+ });
+ reset();
+ refetch();
+ closeModal();
+ }
+ };
+
return (
- <CustomerListPage
- customers={maybe(() => data.customers.edges.map(edge => edge.node))}
- disabled={loading}
- pageInfo={pageInfo}
- onAdd={() => navigate(customerAddUrl)}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () => navigate(customerUrl(id))}
- />
+ <TypedBulkRemoveCustomers onCompleted={handleBulkCustomerDelete}>
+ {(bulkRemoveCustomers, bulkRemoveCustomersOpts) => {
+ const removeTransitionState = getMutationState(
+ bulkRemoveCustomersOpts.called,
+ bulkRemoveCustomersOpts.loading,
+ maybe(
+ () => bulkRemoveCustomersOpts.data.customerBulkDelete.errors
+ )
+ );
+
+ return (
+ <>
+ <CustomerListPage
+ customers={maybe(() =>
+ data.customers.edges.map(edge => edge.node)
+ )}
+ disabled={loading}
+ pageInfo={pageInfo}
+ onAdd={() => navigate(customerAddUrl)}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => navigate(customerUrl(id))}
+ toolbar={
+ <IconButton
+ color="primary"
+ onClick={() =>
+ navigate(
+ customerListUrl({
+ action: "remove",
+ ids: listElements
+ })
+ )
+ }
+ >
+ <DeleteIcon />
+ </IconButton>
+ }
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
+ />
+ <ActionDialog
+ open={params.action === "remove"}
+ onClose={closeModal}
+ confirmButtonState={removeTransitionState}
+ onConfirm={() =>
+ bulkRemoveCustomers({
+ variables: {
+ ids: params.ids
+ }
+ })
+ }
+ variant="delete"
+ title={i18n.t("Remove customers")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ number }}</strong> customers?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ );
+ }}
+ </TypedBulkRemoveCustomers>
);
}}
</TypedCustomerListQuery>