Skip to content

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

Add product type bulk delete

dominik-zeglene18621cc0a80
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
index 84948d6711..fc0ae30514 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeList/ProductTypeList.tsx
@@ -1,10 +1,10 @@
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 Typography from "@material-ui/core/Typography";
import * as React from "react";
@@ -13,8 +13,9 @@ import Skeleton from "../../../components/Skeleton";
import TablePagination from "../../../components/TablePagination";
import i18n from "../../../i18n";
import { maybe, renderCollection, translatedTaxRates } from "../../../misc";
-import { ListProps } from "../../../types";
+import { ListActions, ListProps } from "../../../types";
import { ProductTypeList_productTypes_edges_node } from "../../types/ProductTypeList";
+import TableHead from "../../../components/TableHead";
const styles = createStyles({
leftText: {
@@ -28,7 +29,10 @@ const styles = createStyles({
}
});
-interface ProductTypeListProps extends ListProps, WithStyles<typeof styles> {
+interface ProductTypeListProps
+ extends ListProps,
+ ListActions,
+ WithStyles<typeof styles> {
productTypes: ProductTypeList_productTypes_edges_node[];
}
@@ -40,12 +44,17 @@ const ProductTypeList = withStyles(styles, { name: "ProductTypeList" })(
pageInfo,
onNextPage,
onPreviousPage,
- onRowClick
+ onRowClick,
+ isChecked,
+ selected,
+ toggle,
+ toolbar
}: ProductTypeListProps) => (
<Card>
<Table>
- <TableHead>
+ <TableHead selected={selected} toolbar={toolbar}>
<TableRow>
+ <TableRow />
<TableCell className={classes.wideCell}>
{i18n.t("Type Name", { context: "table header" })}
</TableCell>
@@ -60,7 +69,7 @@ const ProductTypeList = withStyles(styles, { name: "ProductTypeList" })(
<TableFooter>
<TableRow>
<TablePagination
- colSpan={3}
+ colSpan={4}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
@@ -73,53 +82,72 @@ const ProductTypeList = withStyles(styles, { name: "ProductTypeList" })(
<TableBody>
{renderCollection(
productTypes,
- productType => (
- <TableRow
- className={!!productType ? classes.link : undefined}
- hover={!!productType}
- key={productType ? productType.id : "skeleton"}
- >
- <TableCell
+ productType => {
+ const isSelected = productType
+ ? isChecked(productType.id)
+ : false;
+ return (
+ <TableRow
+ className={!!productType ? classes.link : undefined}
+ hover={!!productType}
+ key={productType ? productType.id : "skeleton"}
onClick={productType ? onRowClick(productType.id) : undefined}
+ selected={isSelected}
>
- {productType ? (
- <>
- {productType.name}
- <Typography variant="caption">
- {maybe(() => productType.hasVariants)
- ? i18n.t("Configurable", { context: "product type" })
- : i18n.t("Simple product", {
- context: "product type"
- })}
- </Typography>
- </>
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.leftText}>
- {maybe(() => productType.isShippingRequired) !== undefined ? (
- productType.isShippingRequired ? (
- <>{i18n.t("Physical", { context: "product type" })}</>
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ checked={isSelected}
+ disabled={disabled}
+ onClick={event => {
+ toggle(productType.id);
+ event.stopPropagation();
+ }}
+ />
+ </TableCell>
+ <TableCell>
+ {productType ? (
+ <>
+ {productType.name}
+ <Typography variant="caption">
+ {maybe(() => productType.hasVariants)
+ ? i18n.t("Configurable", {
+ context: "product type"
+ })
+ : i18n.t("Simple product", {
+ context: "product type"
+ })}
+ </Typography>
+ </>
) : (
- <>{i18n.t("Digital", { context: "product type" })}</>
- )
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.leftText}>
- {maybe(() => productType.taxRate) ? (
- translatedTaxRates()[productType.taxRate]
- ) : (
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
- ),
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.leftText}>
+ {maybe(() => productType.isShippingRequired) !==
+ undefined ? (
+ productType.isShippingRequired ? (
+ <>{i18n.t("Physical", { context: "product type" })}</>
+ ) : (
+ <>{i18n.t("Digital", { context: "product type" })}</>
+ )
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.leftText}>
+ {maybe(() => productType.taxRate) ? (
+ translatedTaxRates()[productType.taxRate]
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ </TableRow>
+ );
+ },
() => (
<TableRow>
- <TableCell colSpan={3}>
+ <TableCell colSpan={4}>
{i18n.t("No product types found")}
</TableCell>
</TableRow>
diff --git a/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx b/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
index 5a63f7c47a..385b93933f 100644
--- a/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
+++ b/saleor/static/dashboard-next/productTypes/components/ProductTypeListPage/ProductTypeListPage.tsx
@@ -6,27 +6,18 @@ import AppHeader from "../../../components/AppHeader";
import Container from "../../../components/Container";
import PageHeader from "../../../components/PageHeader";
import i18n from "../../../i18n";
-import { PageListProps } from "../../../types";
+import { ListActions, PageListProps } from "../../../types";
import { ProductTypeList_productTypes_edges_node } from "../../types/ProductTypeList";
import ProductTypeList from "../ProductTypeList";
-interface ProductTypeListPageProps extends PageListProps {
+interface ProductTypeListPageProps extends PageListProps, ListActions {
productTypes: ProductTypeList_productTypes_edges_node[];
onBack: () => void;
}
const ProductTypeListPage: React.StatelessComponent<
ProductTypeListPageProps
-> = ({
- productTypes,
- disabled,
- pageInfo,
- onAdd,
- onBack,
- onNextPage,
- onPreviousPage,
- onRowClick
-}) => (
+> = ({ disabled, onAdd, onBack, ...listProps }) => (
<Container>
<AppHeader onBack={onBack}>{i18n.t("Configuration")}</AppHeader>
<PageHeader title={i18n.t("Product types")}>
@@ -39,14 +30,7 @@ const ProductTypeListPage: React.StatelessComponent<
{i18n.t("Add product type")} <AddIcon />
</Button>
</PageHeader>
- <ProductTypeList
- productTypes={productTypes}
- disabled={disabled}
- pageInfo={pageInfo}
- onNextPage={onNextPage}
- onPreviousPage={onPreviousPage}
- onRowClick={onRowClick}
- />
+ <ProductTypeList disabled={disabled} {...listProps} />
</Container>
);
ProductTypeListPage.displayName = "ProductTypeListPage";
diff --git a/saleor/static/dashboard-next/productTypes/index.tsx b/saleor/static/dashboard-next/productTypes/index.tsx
index 7a7e809fc9..06500fed46 100644
--- a/saleor/static/dashboard-next/productTypes/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/index.tsx
@@ -11,19 +11,14 @@ import {
ProductTypeUrlQueryParams
} from "./urls";
import ProductTypeCreate from "./views/ProductTypeCreate";
-import ProductTypeListComponent, {
- ProductTypeListQueryParams
-} from "./views/ProductTypeList";
+import ProductTypeListComponent from "./views/ProductTypeList";
import ProductTypeUpdateComponent from "./views/ProductTypeUpdate";
const ProductTypeList: React.StatelessComponent<RouteComponentProps<{}>> = ({
location
}) => {
const qs = parseQs(location.search.substr(1));
- const params: ProductTypeListQueryParams = {
- after: qs.after,
- before: qs.before
- };
+ const params: ProductTypeListUrlQueryParams = qs;
return <ProductTypeListComponent params={params} />;
};
diff --git a/saleor/static/dashboard-next/productTypes/mutations.ts b/saleor/static/dashboard-next/productTypes/mutations.ts
index ae507193de..de7cc95438 100644
--- a/saleor/static/dashboard-next/productTypes/mutations.ts
+++ b/saleor/static/dashboard-next/productTypes/mutations.ts
@@ -14,6 +14,10 @@ import {
AttributeUpdate,
AttributeUpdateVariables
} from "./types/AttributeUpdate";
+import {
+ ProductTypeBulkDelete,
+ ProductTypeBulkDeleteVariables
+} from "./types/ProductTypeBulkDelete";
import {
ProductTypeCreate,
ProductTypeCreateVariables
@@ -45,6 +49,21 @@ export const TypedProductTypeDeleteMutation = TypedMutation<
ProductTypeDeleteVariables
>(productTypeDeleteMutation);
+export const productTypeBulkDeleteMutation = gql`
+ mutation ProductTypeBulkDelete($ids: [ID]!) {
+ productTypeBulkDelete(ids: $ids) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedProductTypeBulkDeleteMutation = TypedMutation<
+ ProductTypeBulkDelete,
+ ProductTypeBulkDeleteVariables
+>(productTypeBulkDeleteMutation);
+
export const productTypeUpdateMutation = gql`
${productTypeDetailsFragment}
mutation ProductTypeUpdate($id: ID!, $input: ProductTypeInput!) {
diff --git a/saleor/static/dashboard-next/productTypes/types/ProductTypeBulkDelete.ts b/saleor/static/dashboard-next/productTypes/types/ProductTypeBulkDelete.ts
new file mode 100644
index 0000000000..5924436f60
--- /dev/null
+++ b/saleor/static/dashboard-next/productTypes/types/ProductTypeBulkDelete.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: ProductTypeBulkDelete
+// ====================================================
+
+export interface ProductTypeBulkDelete_productTypeBulkDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface ProductTypeBulkDelete_productTypeBulkDelete {
+ __typename: "ProductTypeBulkDelete";
+ errors: ProductTypeBulkDelete_productTypeBulkDelete_errors[] | null;
+}
+
+export interface ProductTypeBulkDelete {
+ productTypeBulkDelete: ProductTypeBulkDelete_productTypeBulkDelete | null;
+}
+
+export interface ProductTypeBulkDeleteVariables {
+ ids: (string | null)[];
+}
diff --git a/saleor/static/dashboard-next/productTypes/urls.ts b/saleor/static/dashboard-next/productTypes/urls.ts
index e300bfb0ed..509f22d478 100644
--- a/saleor/static/dashboard-next/productTypes/urls.ts
+++ b/saleor/static/dashboard-next/productTypes/urls.ts
@@ -1,13 +1,18 @@
import { stringify as stringifyQs } from "qs";
import * as urlJoin from "url-join";
-import { Dialog, SingleAction } from "../types";
+import { BulkAction, Dialog, Pagination, SingleAction } from "../types";
import { AttributeTypeEnum } from "../types/globalTypes";
const productTypeSection = "/product-types/";
export const productTypeListPath = productTypeSection;
-export const productTypeListUrl = productTypeListPath;
+export type ProductTypeListUrlDialog = "remove";
+export type ProductTypeListUrlQueryParams = BulkAction &
+ Dialog<ProductTypeListUrlDialog> &
+ Pagination;
+export const productTypeListUrl = (params?: ProductTypeListUrlQueryParams) =>
+ productTypeListPath + "?" + stringifyQs(params);
export const productTypeAddPath = urlJoin(productTypeSection, "add");
export const productTypeAddUrl = productTypeAddPath;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
index 124f29d8e9..72c04ded8a 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeList.tsx
@@ -1,56 +1,153 @@
+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 Navigator from "../../components/Navigator";
-import { createPaginationState, Paginator } from "../../components/Paginator";
+import ActionDialog from "../../components/ActionDialog";
+import { createPaginationState } from "../../components/Paginator";
import { configurationMenuUrl } from "../../configuration";
-import { maybe } from "../../misc";
-import { Pagination } from "../../types";
+import useBulkActions from "../../hooks/useBulkActions";
+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 ProductTypeListPage from "../components/ProductTypeListPage";
+import { TypedProductTypeBulkDeleteMutation } from "../mutations";
import { TypedProductTypeListQuery } from "../queries";
-import { productTypeAddUrl, productTypeUrl } from "../urls";
-
-export type ProductTypeListQueryParams = Pagination;
+import { ProductTypeBulkDelete } from "../types/ProductTypeBulkDelete";
+import {
+ productTypeAddUrl,
+ productTypeListUrl,
+ ProductTypeListUrlQueryParams,
+ productTypeUrl
+} from "../urls";
interface ProductTypeListProps {
- params: ProductTypeListQueryParams;
+ params: ProductTypeListUrlQueryParams;
}
const PAGINATE_BY = 20;
export const ProductTypeList: React.StatelessComponent<
ProductTypeListProps
-> = ({ params }) => (
- <Navigator>
- {navigate => {
- const paginationState = createPaginationState(PAGINATE_BY, params);
- return (
- <TypedProductTypeListQuery displayLoader variables={paginationState}>
- {({ data, loading }) => (
- <Paginator
- pageInfo={maybe(() => data.productTypes.pageInfo)}
- paginationState={paginationState}
- queryString={params}
- >
- {({ loadNextPage, loadPreviousPage, pageInfo }) => (
- <ProductTypeListPage
- disabled={loading}
- productTypes={maybe(() =>
- data.productTypes.edges.map(edge => edge.node)
- )}
- pageInfo={pageInfo}
- onAdd={() => navigate(productTypeAddUrl)}
- onBack={() => navigate(configurationMenuUrl)}
- onNextPage={loadNextPage}
- onPreviousPage={loadPreviousPage}
- onRowClick={id => () => navigate(productTypeUrl(id))}
- />
- )}
- </Paginator>
- )}
- </TypedProductTypeListQuery>
- );
- }}
- </Navigator>
-);
+> = ({ params }) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+ const paginate = usePaginator();
+ const { isSelected, listElements, reset, toggle } = useBulkActions();
+
+ const closeModal = () => navigate(productTypeListUrl(), true);
+
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+ return (
+ <TypedProductTypeListQuery displayLoader variables={paginationState}>
+ {({ data, loading, refetch }) => {
+ const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
+ maybe(() => data.productTypes.pageInfo),
+ paginationState,
+ params
+ );
+
+ const handleProductTypeBulkDelete = (data: ProductTypeBulkDelete) => {
+ if (data.productTypeBulkDelete.errors.length === 0) {
+ notify({
+ text: i18n.t("Removed product types")
+ });
+ reset();
+ refetch();
+ navigate(
+ productTypeListUrl({
+ ...params,
+ action: undefined,
+ ids: undefined
+ })
+ );
+ }
+ };
+
+ return (
+ <TypedProductTypeBulkDeleteMutation
+ onCompleted={handleProductTypeBulkDelete}
+ >
+ {(productTypeBulkDelete, productTypeBulkDeleteOpts) => {
+ const bulkRemoveTransitionState = getMutationState(
+ productTypeBulkDeleteOpts.called,
+ productTypeBulkDeleteOpts.loading,
+ maybe(
+ () =>
+ productTypeBulkDeleteOpts.data.productTypeBulkDelete.errors
+ )
+ );
+
+ const onProductTypeBulkDelete = () =>
+ productTypeBulkDelete({
+ variables: {
+ ids: params.ids
+ }
+ });
+ return (
+ <>
+ <ProductTypeListPage
+ disabled={loading}
+ productTypes={maybe(() =>
+ data.productTypes.edges.map(edge => edge.node)
+ )}
+ pageInfo={pageInfo}
+ onAdd={() => navigate(productTypeAddUrl)}
+ onBack={() => navigate(configurationMenuUrl)}
+ onNextPage={loadNextPage}
+ onPreviousPage={loadPreviousPage}
+ onRowClick={id => () => navigate(productTypeUrl(id))}
+ isChecked={isSelected}
+ selected={listElements.length}
+ toggle={toggle}
+ toolbar={
+ <IconButton
+ color="primary"
+ onClick={() =>
+ navigate(
+ productTypeListUrl({
+ action: "remove",
+ ids: listElements
+ })
+ )
+ }
+ >
+ <DeleteIcon />
+ </IconButton>
+ }
+ />
+ <ActionDialog
+ confirmButtonState={bulkRemoveTransitionState}
+ onClose={closeModal}
+ onConfirm={onProductTypeBulkDelete}
+ open={params.action === "remove"}
+ title={i18n.t("Remove Product Types")}
+ variant="delete"
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to remove <strong>{{ number }}</strong> product types?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ );
+ }}
+ </TypedProductTypeBulkDeleteMutation>
+ );
+ }}
+ </TypedProductTypeListQuery>
+ );
+};
ProductTypeList.displayName = "ProductTypeList";
export default ProductTypeList;
diff --git a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
index 55dd1c43f8..8b29cb2aad 100644
--- a/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
+++ b/saleor/static/dashboard-next/productTypes/views/ProductTypeUpdate/index.tsx
@@ -101,7 +101,7 @@ export const ProductTypeUpdate: React.StatelessComponent<
context: "notification"
})
});
- navigate(productTypeListUrl);
+ navigate(productTypeListUrl(), true);
}
};
const handleProductTypeUpdateSuccess = (
@@ -258,7 +258,7 @@ export const ProductTypeUpdate: React.StatelessComponent<
})
)
}
- onBack={() => navigate(productTypeListUrl)}
+ onBack={() => navigate(productTypeListUrl())}
onDelete={() =>
navigate(
productTypeUrl(id, {