Skip to content

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

Add bulk delete to product list

dominik-zeglen1c99f4a69771
diff --git a/saleor/static/dashboard-next/components/ActionDialog/ActionDialog.tsx b/saleor/static/dashboard-next/components/ActionDialog/ActionDialog.tsx
index 37c67d3cfe..cec50eea2b 100644
--- a/saleor/static/dashboard-next/components/ActionDialog/ActionDialog.tsx
+++ b/saleor/static/dashboard-next/components/ActionDialog/ActionDialog.tsx
@@ -34,7 +34,7 @@ interface ActionDialogProps extends WithStyles<typeof styles> {
confirmButtonState: ConfirmButtonTransitionState;
open: boolean;
title: string;
- variant?: string;
+ variant?: "default" | "delete";
onClose?();
onConfirm();
}
diff --git a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
index d37216a325..73d179a6b0 100644
--- a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
@@ -1,3 +1,4 @@
+import Checkbox from "@material-ui/core/Checkbox";
import {
createStyles,
Theme,
@@ -8,7 +9,6 @@ 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";
@@ -16,10 +16,11 @@ import { CategoryDetails_category_products_edges_node } from "../../categories/t
import TableCellAvatar from "../../components/TableCellAvatar";
import i18n from "../../i18n";
import { maybe, renderCollection } from "../../misc";
-import { ListProps } from "../../types";
+import { ListActions, ListProps } from "../../types";
import Money from "../Money";
import Skeleton from "../Skeleton";
import StatusLabel from "../StatusLabel";
+import TableHead from "../TableHead";
import TablePagination from "../TablePagination";
const styles = (theme: Theme) =>
@@ -40,7 +41,10 @@ const styles = (theme: Theme) =>
}
});
-interface ProductListProps extends ListProps, WithStyles<typeof styles> {
+interface ProductListProps
+ extends ListProps,
+ ListActions,
+ WithStyles<typeof styles> {
products: CategoryDetails_category_products_edges_node[];
}
@@ -48,16 +52,21 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
({
classes,
disabled,
+ isChecked,
pageInfo,
+ products,
+ selected,
+ toggle,
+ toolbar,
onNextPage,
onPreviousPage,
- onRowClick,
- products
+ onRowClick
}: ProductListProps) => (
<Table>
- <TableHead>
+ <TableHead selected={selected} toolbar={toolbar}>
<TableRow>
- {(products === undefined || products.length > 0) && <TableCell />}
+ <TableCell />
+ <TableCell />
<TableCell className={classes.textLeft}>
{i18n.t("Name", { context: "object" })}
</TableCell>
@@ -71,7 +80,7 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
<TableFooter>
<TableRow>
<TablePagination
- colSpan={5}
+ colSpan={6}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
hasPreviousPage={
@@ -84,57 +93,77 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
<TableBody>
{renderCollection(
products,
- product => (
- <TableRow
- hover={!!product}
- key={product ? product.id : "skeleton"}
- onClick={product && onRowClick(product.id)}
- className={classes.link}
- >
- <TableCellAvatar thumbnail={maybe(() => product.thumbnail.url)} />
- <TableCell className={classes.textLeft}>
- {product ? product.name : <Skeleton />}
- </TableCell>
- <TableCell>
- {product && product.productType ? (
- product.productType.name
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell>
- {product &&
- product.availability &&
- product.availability.available !== undefined ? (
- <StatusLabel
- label={
- product.availability.available
- ? i18n.t("Published", { context: "product status" })
- : i18n.t("Not published", { context: "product status" })
- }
- status={
- product.availability.available ? "success" : "error"
- }
+ product => {
+ const isSelected = product ? isChecked(product.id) : false;
+
+ return (
+ <TableRow
+ selected={isSelected}
+ hover={!!product}
+ key={product ? product.id : "skeleton"}
+ onClick={product && onRowClick(product.id)}
+ className={classes.link}
+ >
+ <TableCell padding="checkbox">
+ <Checkbox
+ color="primary"
+ checked={isSelected}
+ disabled={disabled}
+ onClick={event => {
+ toggle(product.id);
+ event.stopPropagation();
+ }}
/>
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.textRight}>
- {product &&
- product.price &&
- product.price.amount !== undefined &&
- product.price.currency !== undefined ? (
- <Money money={product.price} />
- ) : (
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
- ),
+ </TableCell>
+ <TableCellAvatar
+ thumbnail={maybe(() => product.thumbnail.url)}
+ />
+ <TableCell className={classes.textLeft}>
+ {product ? product.name : <Skeleton />}
+ </TableCell>
+ <TableCell>
+ {product && product.productType ? (
+ product.productType.name
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell>
+ {product &&
+ product.availability &&
+ product.availability.available !== undefined ? (
+ <StatusLabel
+ label={
+ product.availability.available
+ ? i18n.t("Published", { context: "product status" })
+ : i18n.t("Not published", {
+ context: "product status"
+ })
+ }
+ status={
+ product.availability.available ? "success" : "error"
+ }
+ />
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ <TableCell className={classes.textRight}>
+ {product &&
+ product.price &&
+ product.price.amount !== undefined &&
+ product.price.currency !== undefined ? (
+ <Money money={product.price} />
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ </TableRow>
+ );
+ },
() => (
<TableRow>
- <TableCell colSpan={5}>{i18n.t("No products found")}</TableCell>
+ <TableCell colSpan={6}>{i18n.t("No products found")}</TableCell>
</TableRow>
)
)}
diff --git a/saleor/static/dashboard-next/components/TableActions/TableActions.tsx b/saleor/static/dashboard-next/components/TableActions/TableActions.tsx
new file mode 100644
index 0000000000..3c97b1dac2
--- /dev/null
+++ b/saleor/static/dashboard-next/components/TableActions/TableActions.tsx
@@ -0,0 +1,66 @@
+import {
+ createStyles,
+ Theme,
+ withStyles,
+ WithStyles
+} from "@material-ui/core/styles";
+import { fade } from "@material-ui/core/styles/colorManipulator";
+import TableCell from "@material-ui/core/TableCell";
+import TableRow from "@material-ui/core/TableRow";
+import Typography from "@material-ui/core/Typography";
+import * as React from "react";
+import i18n from "../../i18n";
+
+export interface TableActionsProps {
+ children: React.ReactNode;
+ selected?: number;
+}
+
+const styles = (theme: Theme) =>
+ createStyles({
+ cell: {
+ padding: 0
+ },
+ container: {
+ alignItems: "center",
+ display: "flex",
+ height: 56,
+ marginRight: -theme.spacing.unit * 2
+ },
+ root: {
+ backgroundColor: fade(theme.palette.primary.main, 0.05)
+ },
+ spacer: {
+ flex: 1
+ },
+ toolbar: {
+ "& > *": {
+ marginLeft: theme.spacing.unit
+ }
+ }
+ });
+const TableActions = withStyles(styles, { name: "TableActions" })(
+ ({
+ classes,
+ children,
+ selected
+ }: TableActionsProps & WithStyles<typeof styles>) => (
+ <TableRow className={classes.root}>
+ <TableCell className={classes.cell} colSpan={9999}>
+ <div className={classes.container}>
+ {selected && (
+ <Typography>
+ {i18n.t("Selected {{ number }} items", {
+ number: selected
+ })}
+ </Typography>
+ )}
+ <div className={classes.spacer} />
+ <div className={classes.toolbar}>{children}</div>
+ </div>
+ </TableCell>
+ </TableRow>
+ )
+);
+TableActions.displayName = "TableActions";
+export default TableActions;
diff --git a/saleor/static/dashboard-next/components/TableActions/index.ts b/saleor/static/dashboard-next/components/TableActions/index.ts
new file mode 100644
index 0000000000..01a797b515
--- /dev/null
+++ b/saleor/static/dashboard-next/components/TableActions/index.ts
@@ -0,0 +1,2 @@
+export { default } from './TableActions';
+export * from './TableActions';
\ No newline at end of file
diff --git a/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx b/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
index 60f39a0574..c940d32bbf 100644
--- a/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
+++ b/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
@@ -22,7 +22,7 @@ const styles = (theme: Theme) =>
padding: theme.spacing.unit / 2
},
root: {
- paddingLeft: theme.spacing.unit * 3,
+ // paddingLeft: theme.spacing.unit * 3,
paddingRight: theme.spacing.unit * 3,
width: "1%"
}
diff --git a/saleor/static/dashboard-next/components/TableHead/TableHead.tsx b/saleor/static/dashboard-next/components/TableHead/TableHead.tsx
new file mode 100644
index 0000000000..2f610ddb66
--- /dev/null
+++ b/saleor/static/dashboard-next/components/TableHead/TableHead.tsx
@@ -0,0 +1,28 @@
+import MuiTableHead, {
+ TableHeadProps as MuiTableHeadProps
+} from "@material-ui/core/TableHead";
+import * as React from "react";
+
+import TableActions from "../TableActions";
+
+export interface TableHeadProps extends MuiTableHeadProps {
+ selected: number;
+ toolbar: React.ReactNode | React.ReactNodeArray;
+}
+
+const TableHead: React.StatelessComponent<TableHeadProps> = ({
+ children,
+ selected,
+ toolbar,
+ ...muiTableHeadProps
+}) => (
+ <MuiTableHead {...muiTableHeadProps}>
+ {selected ? (
+ <TableActions selected={selected}>{toolbar}</TableActions>
+ ) : (
+ children
+ )}
+ </MuiTableHead>
+);
+TableHead.displayName = "TableHead";
+export default TableHead;
diff --git a/saleor/static/dashboard-next/components/TableHead/index.ts b/saleor/static/dashboard-next/components/TableHead/index.ts
new file mode 100644
index 0000000000..bbb62bd2ff
--- /dev/null
+++ b/saleor/static/dashboard-next/components/TableHead/index.ts
@@ -0,0 +1,2 @@
+export { default } from './TableHead';
+export * from './TableHead';
\ No newline at end of file
diff --git a/saleor/static/dashboard-next/hooks/useBulkActions.ts b/saleor/static/dashboard-next/hooks/useBulkActions.ts
new file mode 100644
index 0000000000..dee0b2e967
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useBulkActions.ts
@@ -0,0 +1,17 @@
+import { useEffect } from "react";
+
+import { maybe } from "../misc";
+import useListSelector from "./useListSelector";
+
+interface ConnectionNode {
+ id: string;
+}
+function useBulkActions(list: ConnectionNode[]) {
+ const listSelectorFuncs = useListSelector();
+ useEffect(() => listSelectorFuncs.reset, [
+ maybe(() => list.reduce((acc, curr) => acc + curr.id, ""))
+ ]);
+
+ return listSelectorFuncs;
+}
+export default useBulkActions;
diff --git a/saleor/static/dashboard-next/hooks/useListSelector.ts b/saleor/static/dashboard-next/hooks/useListSelector.ts
new file mode 100644
index 0000000000..d2889e3dbc
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useListSelector.ts
@@ -0,0 +1,35 @@
+import { useState } from "react";
+
+function useListSelector(initial: string[] = []) {
+ const [listElements, setListElements] = useState(initial);
+
+ function isMember(id: string) {
+ return !!listElements.find(listElement => listElement === id);
+ }
+
+ function add(id: string) {
+ setListElements([...listElements, id]);
+ }
+
+ function remove(id: string) {
+ setListElements(listElements.filter(listElement => listElement !== id));
+ }
+
+ function reset() {
+ setListElements(initial);
+ }
+
+ function toggle(id: string) {
+ isMember(id) ? remove(id) : add(id);
+ }
+
+ return {
+ add,
+ isMember,
+ listElements,
+ remove,
+ reset,
+ toggle
+ };
+}
+export default useListSelector;
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index 51be3aab41..a7a2d90713 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -1,6 +1,9 @@
import Button from "@material-ui/core/Button";
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";
@@ -8,62 +11,101 @@ import Container from "../../../components/Container";
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 { PageListProps } from "../../../types";
+import { maybe } from "../../../misc";
+import { ListActionProps, PageListProps } from "../../../types";
import ProductListFilter, { ProductListFilterTabs } from "../ProductListFilter";
-interface ProductListCardProps extends PageListProps {
+interface ProductListCardProps
+ extends PageListProps,
+ ListActionProps<"onBulkDelete" | "onBulkPublish" | "onBulkUnpublish"> {
currentTab: ProductListFilterTabs;
filtersList: Filter[];
products: CategoryDetails_category_products_edges_node[];
onAllProducts: () => void;
onAvailable: () => void;
- onOfStock: () => void;
onCustomFilter: () => void;
+ onOfStock: () => void;
}
export const ProductListCard: React.StatelessComponent<
ProductListCardProps
> = ({
- products,
+ currentTab,
disabled,
+ filtersList,
pageInfo,
+ products,
onAdd,
- onNextPage,
- onPreviousPage,
- onRowClick,
- filtersList,
- currentTab,
onAllProducts,
onAvailable,
+ onBulkDelete,
+ onBulkPublish,
+ onBulkUnpublish,
+ onCustomFilter,
+ onNextPage,
onOfStock,
- onCustomFilter
-}) => (
- <Container>
- <PageHeader title={i18n.t("Products")}>
- <Button onClick={onAdd} color="primary" variant="contained">
- {i18n.t("Add product")} <AddIcon />
- </Button>
- </PageHeader>
- <Card>
- <ProductListFilter
- currentTab={currentTab}
- filtersList={filtersList}
- onAvailable={onAvailable}
- onAllProducts={onAllProducts}
- onOfStock={onOfStock}
- onCustomFilter={onCustomFilter}
- />
- <ProductList
- products={products}
- disabled={disabled}
- pageInfo={pageInfo}
- onNextPage={onNextPage}
- onPreviousPage={onPreviousPage}
- onRowClick={onRowClick}
- />
- </Card>
- </Container>
-);
+ onPreviousPage,
+ onRowClick
+}) => {
+ const { isMember: isChecked, listElements, toggle } = useBulkActions(
+ products
+ );
+
+ return (
+ <Container>
+ <PageHeader title={i18n.t("Products")}>
+ <Button onClick={onAdd} color="primary" variant="contained">
+ {i18n.t("Add product")} <AddIcon />
+ </Button>
+ </PageHeader>
+ <Card>
+ <ProductListFilter
+ currentTab={currentTab}
+ filtersList={filtersList}
+ onAvailable={onAvailable}
+ onAllProducts={onAllProducts}
+ onOfStock={onOfStock}
+ onCustomFilter={onCustomFilter}
+ />
+ <ProductList
+ products={products}
+ disabled={disabled}
+ pageInfo={pageInfo}
+ 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>
+ </>
+ }
+ selected={listElements.length}
+ isChecked={isChecked}
+ toggle={toggle}
+ onNextPage={onNextPage}
+ onPreviousPage={onPreviousPage}
+ onRowClick={onRowClick}
+ />
+ </Card>
+ </Container>
+ );
+};
ProductListCard.displayName = "ProductListCard";
export default ProductListCard;
diff --git a/saleor/static/dashboard-next/products/index.tsx b/saleor/static/dashboard-next/products/index.tsx
index bcfe1a3465..122b993b4b 100644
--- a/saleor/static/dashboard-next/products/index.tsx
+++ b/saleor/static/dashboard-next/products/index.tsx
@@ -31,6 +31,8 @@ const ProductList: React.StatelessComponent<RouteComponentProps<any>> = ({
const params: ProductListQueryParams = {
after: qs.after,
before: qs.before,
+ dialog: qs.dialog,
+ ids: qs.ids,
status: qs.status
};
return <ProductListComponent params={params} />;
diff --git a/saleor/static/dashboard-next/products/mutations.ts b/saleor/static/dashboard-next/products/mutations.ts
index 9f847d2be6..e27855a81f 100644
--- a/saleor/static/dashboard-next/products/mutations.ts
+++ b/saleor/static/dashboard-next/products/mutations.ts
@@ -36,10 +36,18 @@ import {
} from "./types/VariantImageUnassign";
import { VariantUpdate, VariantUpdateVariables } from "./types/VariantUpdate";
-import { fragmentProduct, fragmentVariant } from "./queries";
+import {
+ fragmentVariant,
+ productFragment,
+ productFragmentDetails
+} from "./queries";
+import {
+ productBulkDelete,
+ productBulkDeleteVariables
+} from "./types/productBulkDelete";
export const productImageCreateMutation = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
mutation ProductImageCreate($product: ID!, $image: Upload!, $alt: String) {
productImageCreate(input: { alt: $alt, image: $image, product: $product }) {
errors {
@@ -100,7 +108,7 @@ export const TypedProductImagesReorder = TypedMutation<
>(productImagesReorder);
export const productUpdateMutation = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
mutation ProductUpdate(
$id: ID!
$attributes: [AttributeValueInput]
@@ -143,7 +151,7 @@ export const TypedProductUpdateMutation = TypedMutation<
>(productUpdateMutation);
export const simpleProductUpdateMutation = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
${fragmentVariant}
mutation SimpleProductUpdate(
$id: ID!
@@ -198,7 +206,7 @@ export const TypedSimpleProductUpdateMutation = TypedMutation<
>(simpleProductUpdateMutation);
export const productCreateMutation = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
mutation ProductCreate(
$attributes: [AttributeValueInput]
$publicationDate: Date
@@ -354,7 +362,7 @@ export const TypedProductImageDeleteMutation = TypedMutation<
>(productImageDeleteMutation);
export const productImageUpdateMutation = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
mutation ProductImageUpdate($id: ID!, $alt: String!) {
productImageUpdate(id: $id, input: { alt: $alt }) {
errors {
@@ -409,3 +417,18 @@ export const TypedVariantImageUnassignMutation = TypedMutation<
VariantImageUnassign,
VariantImageUnassignVariables
>(variantImageUnassignMutation);
+
+export const productBulkDeleteMutation = gql`
+ mutation productBulkDelete($ids: [ID!]!) {
+ productBulkDelete(ids: $ids) {
+ errors {
+ field
+ message
+ }
+ }
+ }
+`;
+export const TypedProductBulkDeleteMutation = TypedMutation<
+ productBulkDelete,
+ productBulkDeleteVariables
+>(productBulkDeleteMutation);
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index fa2c1653e8..c5b41927e7 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -44,7 +44,27 @@ export const fragmentProductImage = gql`
}
`;
-export const fragmentProduct = gql`
+export const productFragment = gql`
+ ${fragmentMoney}
+ fragment ProductFragment on Product {
+ id
+ name
+ thumbnail {
+ url
+ }
+ availability {
+ available
+ }
+ price {
+ ...Money
+ }
+ productType {
+ id
+ name
+ }
+ }
+`;
+export const productFragmentDetails = gql`
${fragmentProductImage}
${fragmentMoney}
fragment Product on Product {
@@ -193,7 +213,7 @@ export const fragmentVariant = gql`
`;
const productListQuery = gql`
- ${fragmentMoney}
+ ${productFragment}
query ProductList(
$first: Int
$after: String
@@ -210,21 +230,7 @@ const productListQuery = gql`
) {
edges {
node {
- id
- name
- thumbnail {
- url
- }
- availability {
- available
- }
- price {
- ...Money
- }
- productType {
- id
- name
- }
+ ...ProductFragment
}
}
pageInfo {
@@ -242,7 +248,7 @@ export const TypedProductListQuery = TypedQuery<
>(productListQuery);
const productDetailsQuery = gql`
- ${fragmentProduct}
+ ${productFragmentDetails}
query ProductDetails($id: ID!) {
product(id: $id) {
...Product
diff --git a/saleor/static/dashboard-next/products/types/ProductFragment.ts b/saleor/static/dashboard-next/products/types/ProductFragment.ts
new file mode 100644
index 0000000000..6b1f88316b
--- /dev/null
+++ b/saleor/static/dashboard-next/products/types/ProductFragment.ts
@@ -0,0 +1,39 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL fragment: ProductFragment
+// ====================================================
+
+export interface ProductFragment_thumbnail {
+ __typename: "Image";
+ url: string;
+}
+
+export interface ProductFragment_availability {
+ __typename: "ProductAvailability";
+ available: boolean | null;
+}
+
+export interface ProductFragment_price {
+ __typename: "Money";
+ amount: number;
+ currency: string;
+}
+
+export interface ProductFragment_productType {
+ __typename: "ProductType";
+ id: string;
+ name: string;
+}
+
+export interface ProductFragment {
+ __typename: "Product";
+ id: string;
+ name: string;
+ thumbnail: ProductFragment_thumbnail | null;
+ availability: ProductFragment_availability | null;
+ price: ProductFragment_price | null;
+ productType: ProductFragment_productType;
+}
diff --git a/saleor/static/dashboard-next/products/types/productBulkDelete.ts b/saleor/static/dashboard-next/products/types/productBulkDelete.ts
new file mode 100644
index 0000000000..51f2e18707
--- /dev/null
+++ b/saleor/static/dashboard-next/products/types/productBulkDelete.ts
@@ -0,0 +1,26 @@
+/* tslint:disable */
+/* eslint-disable */
+// This file was automatically generated and should not be edited.
+
+// ====================================================
+// GraphQL mutation operation: productBulkDelete
+// ====================================================
+
+export interface productBulkDelete_productBulkDelete_errors {
+ __typename: "Error";
+ field: string | null;
+ message: string | null;
+}
+
+export interface productBulkDelete_productBulkDelete {
+ __typename: "ProductBulkDelete";
+ errors: productBulkDelete_productBulkDelete_errors[] | null;
+}
+
+export interface productBulkDelete {
+ productBulkDelete: productBulkDelete_productBulkDelete | null;
+}
+
+export interface productBulkDeleteVariables {
+ ids: string[];
+}
diff --git a/saleor/static/dashboard-next/products/views/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList.tsx
index 8ba0e3080a..bff7691c8c 100644
--- a/saleor/static/dashboard-next/products/views/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList.tsx
@@ -1,22 +1,32 @@
+import DialogContentText from "@material-ui/core/DialogContentText";
import { stringify as stringifyQs } from "qs";
import * as React from "react";
-import Navigator from "../../components/Navigator";
-import { createPaginationState, Paginator } from "../../components/Paginator";
-import { maybe } from "../../misc";
+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 { maybe, getMutationState } from "../../misc";
import { StockAvailability } from "../../types/globalTypes";
import ProductListCard from "../components/ProductListCard";
import { getTabName } from "../misc";
+import { TypedProductBulkDeleteMutation } from "../mutations";
import { TypedProductListQuery } from "../queries";
+import { productBulkDelete } from "../types/productBulkDelete";
import { productAddUrl, productUrl } from "../urls";
export interface ProductListFilters {
status: StockAvailability;
}
+type ProductListDialog = "publish" | "unpublish" | "delete";
export type ProductListQueryParams = Partial<
{
after: string;
before: string;
+ dialog: ProductListDialog;
+ ids: string[];
} & ProductListFilters
>;
@@ -28,35 +38,79 @@ const PAGINATE_BY = 20;
export const ProductList: React.StatelessComponent<ProductListProps> = ({
params
-}) => (
- <Navigator>
- {navigate => {
- const changeFilters = (newParams: ProductListQueryParams) =>
- navigate(
- "?" +
- stringifyQs({
- ...params,
- ...newParams
- })
+}) => {
+ const navigate = useNavigator();
+ const notify = useNotifier();
+ const paginate = usePaginator();
+
+ const closeModal = () =>
+ navigate(
+ "?" +
+ stringifyQs({
+ ...params,
+ dialog: undefined,
+ ids: undefined
+ })
+ );
+
+ const changeFilters = (newParams: ProductListQueryParams) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ ...params,
+ ...newParams
+ })
+ );
+
+ const openModal = (dialog: ProductListDialog, ids: string[]) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ ...params,
+ dialog,
+ ids
+ })
+ );
+
+ const paginationState = createPaginationState(PAGINATE_BY, params);
+
+ return (
+ <TypedProductListQuery
+ displayLoader
+ variables={{
+ ...paginationState,
+ stockAvailability: params.status
+ }}
+ >
+ {({ data, loading, refetch }) => {
+ const currentTab = getTabName(params);
+ const { loadNextPage, loadPreviousPage, pageInfo } = paginate(
+ maybe(() => data.products.pageInfo),
+ paginationState,
+ params
);
- const paginationState = createPaginationState(PAGINATE_BY, params);
- return (
- <TypedProductListQuery
- displayLoader
- variables={{
- ...paginationState,
- stockAvailability: params.status
- }}
- >
- {({ data, loading }) => {
- const currentTab = getTabName(params);
- return (
- <Paginator
- pageInfo={maybe(() => data.products.pageInfo)}
- paginationState={paginationState}
- queryString={params}
- >
- {({ loadNextPage, loadPreviousPage, pageInfo }) => (
+
+ const handleBulkDelete = (data: productBulkDelete) => {
+ if (data.productBulkDelete.errors.length === 0) {
+ closeModal();
+ notify({
+ text: i18n.t("Products removed")
+ });
+ refetch();
+ }
+ };
+
+ return (
+ <TypedProductBulkDeleteMutation onCompleted={handleBulkDelete}>
+ {(productBulkDelete, productBulkDeleteOpts) => {
+ const bulkDeleteMutationState = getMutationState(
+ productBulkDeleteOpts.called,
+ productBulkDeleteOpts.loading,
+ maybe(() => productBulkDeleteOpts.data.productBulkDelete.errors)
+ );
+
+ return (
+ <>
<ProductListCard
currentTab={currentTab}
filtersList={[]}
@@ -89,14 +143,83 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
status: StockAvailability.OUT_OF_STOCK
})
}
+ onBulkDelete={ids => openModal("delete", ids)}
+ onBulkPublish={ids => openModal("publish", ids)}
+ onBulkUnpublish={ids => openModal("unpublish", ids)}
/>
- )}
- </Paginator>
- );
- }}
- </TypedProductListQuery>
- );
- }}
- </Navigator>
-);
+ <ActionDialog
+ open={params.dialog === "delete"}
+ confirmButtonState={bulkDeleteMutationState}
+ onClose={closeModal}
+ onConfirm={() =>
+ productBulkDelete({ variables: { ids: params.ids } })
+ }
+ 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
+ open={params.dialog === "publish"}
+ confirmButtonState={"default"}
+ onClose={closeModal}
+ onConfirm={() => console.log(params.ids)}
+ title={i18n.t("Publish products")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to publish <strong>{{ number }}</strong> products?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ <ActionDialog
+ open={params.dialog === "unpublish"}
+ confirmButtonState={"default"}
+ onClose={closeModal}
+ onConfirm={() => console.log(params.ids)}
+ title={i18n.t("Unpublish products")}
+ >
+ <DialogContentText
+ dangerouslySetInnerHTML={{
+ __html: i18n.t(
+ "Are you sure you want to unpublish <strong>{{ number }}</strong> products?",
+ {
+ number: maybe(
+ () => params.ids.length.toString(),
+ "..."
+ )
+ }
+ )
+ }}
+ />
+ </ActionDialog>
+ </>
+ );
+ }}
+ </TypedProductBulkDeleteMutation>
+ );
+ }}
+ </TypedProductListQuery>
+ );
+};
export default ProductList;
diff --git a/saleor/static/dashboard-next/theme.ts b/saleor/static/dashboard-next/theme.ts
index c74d9bc99e..4e3a1a01f9 100644
--- a/saleor/static/dashboard-next/theme.ts
+++ b/saleor/static/dashboard-next/theme.ts
@@ -144,10 +144,15 @@ export default (colors: IThemeColors): Theme =>
fontSize: ".875rem",
fontWeight: 400
},
+ paddingCheckbox: {
+ width: "1%"
+ },
root: {
"&:first-child": {
- paddingLeft: 24 + "px",
- textAlign: "left" as "left"
+ "&:not($paddingCheckbox)": {
+ paddingLeft: 24 + "px",
+ textAlign: "left" as "left"
+ }
},
borderBottomColor: colors.paperBorder,
height: 56,
@@ -169,6 +174,11 @@ export default (colors: IThemeColors): Theme =>
"$root&:hover": {
backgroundColor: fade(colors.primary, 0.2)
}
+ },
+ root: {
+ "&$selected": {
+ backgroundColor: fade(colors.primary, 0.05)
+ }
}
}
},
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index 38e0729ac5..5ab237296f 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -15,6 +15,17 @@ export interface ListProps {
onPreviousPage: () => void;
onRowClick: (id: string) => () => void;
}
+export type ListAction = (ids: string[]) => void;
+export interface ListActions {
+ toggle: (id: string) => void;
+ isChecked: (id: string) => boolean;
+ selected: number;
+ toolbar: React.ReactNode | React.ReactNodeArray;
+}
+export type ListActionProps<TActions extends string> = Record<
+ TActions,
+ ListAction
+>;
export interface PageListProps extends ListProps {
onAdd: () => void;
}
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index 09a056ef76..b042d07ee3 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -382,6 +382,7 @@ export interface ProductTypeInput {
productAttributes?: (string | null)[] | null;
variantAttributes?: (string | null)[] | null;
isShippingRequired?: boolean | null;
+ isDigital?: boolean | null;
weight?: any | null;
taxRate?: TaxRateType | null;
}
@@ -437,6 +438,9 @@ export interface ShopSettingsInput {
chargeTaxesOnShipping?: boolean | null;
trackInventoryByDefault?: boolean | null;
defaultWeightUnit?: WeightUnitsEnum | null;
+ automaticFulfillmentDigitalProducts?: boolean | null;
+ defaultDigitalMaxDownloads?: number | null;
+ defaultDigitalUrlValidDays?: number | null;
}
export interface SiteDomainInput {