Skip to content

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

Add togglable columns to product list

dominik-zeglen73e7fc32a512
diff --git a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx
index 4bebceace7..87393f0be5 100644
--- a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx
+++ b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPicker.tsx
@@ -30,7 +30,16 @@ const useStyles = makeStyles(
);
const ColumnPicker: React.FC<ColumnPickerProps> = props => {
- const { className, columns, initial, selectedColumns } = props;
+ const {
+ className,
+ columns,
+ initial,
+ selectedColumns,
+ onCancel,
+ onColumnToggle,
+ onReset,
+ onSave
+ } = props;
const classes = useStyles(props);
const anchor = React.useRef<HTMLDivElement>();
const [isExpanded, setExpansionState] = React.useState(false);
@@ -39,6 +48,16 @@ const ColumnPicker: React.FC<ColumnPickerProps> = props => {
setTimeout(() => setExpansionState(!!initial), 100);
}, []);
+ const handleCancel = React.useCallback(() => {
+ setExpansionState(false);
+ onCancel();
+ }, []);
+
+ const handleSave = () => {
+ setExpansionState(false);
+ onSave();
+ };
+
return (
<div ref={anchor} className={className}>
<ColumnPickerButton
@@ -68,6 +87,10 @@ const ColumnPicker: React.FC<ColumnPickerProps> = props => {
<ColumnPickerContent
columns={columns}
selectedColumns={selectedColumns}
+ onCancel={handleCancel}
+ onColumnToggle={onColumnToggle}
+ onReset={onReset}
+ onSave={handleSave}
/>
</ClickAwayListener>
</Grow>
diff --git a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx
index 80461f06bd..3629d19d14 100644
--- a/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx
+++ b/saleor/static/dashboard-next/components/ColumnPicker/ColumnPickerContent.tsx
@@ -20,6 +20,10 @@ export interface ColumnPickerChoice {
export interface ColumnPickerContentProps {
columns: ColumnPickerChoice[];
selectedColumns: string[];
+ onCancel: () => void;
+ onColumnToggle: (column: string) => void;
+ onReset: () => void;
+ onSave: () => void;
}
const useStyles = makeStyles((theme: Theme) => ({
@@ -49,7 +53,14 @@ const useStyles = makeStyles((theme: Theme) => ({
}));
const ColumnPickerContent: React.FC<ColumnPickerContentProps> = props => {
- const { columns, selectedColumns } = props;
+ const {
+ columns,
+ selectedColumns,
+ onCancel,
+ onColumnToggle,
+ onReset,
+ onSave
+ } = props;
const classes = useStyles(props);
const anchor = React.useRef<HTMLDivElement>();
const scrollPosition = useElementScroll(anchor);
@@ -85,7 +96,7 @@ const ColumnPickerContent: React.FC<ColumnPickerContentProps> = props => {
)}
name={column.value}
label={column.label}
- onChange={() => undefined}
+ onChange={() => onColumnToggle(column.value)}
/>
))}
</div>
@@ -97,10 +108,14 @@ const ColumnPickerContent: React.FC<ColumnPickerContentProps> = props => {
})}
>
<div className={classes.actionBar}>
- <Button color="default">{i18n.t("Reset")}</Button>
+ <Button color="default" onClick={onReset}>
+ {i18n.t("Reset")}
+ </Button>
<div>
- <Button color="default">{i18n.t("Cancel")}</Button>
- <Button color="primary" variant="contained">
+ <Button color="default" onClick={onCancel}>
+ {i18n.t("Cancel")}
+ </Button>
+ <Button color="primary" variant="contained" onClick={onSave}>
{i18n.t("Save")}
</Button>
</div>
diff --git a/saleor/static/dashboard-next/components/ColumnPicker/index.ts b/saleor/static/dashboard-next/components/ColumnPicker/index.ts
index 8dd5ec6ac1..301ba74e4f 100644
--- a/saleor/static/dashboard-next/components/ColumnPicker/index.ts
+++ b/saleor/static/dashboard-next/components/ColumnPicker/index.ts
@@ -1,2 +1,4 @@
export { default } from "./ColumnPicker";
export * from "./ColumnPicker";
+export * from "./ColumnPickerButton";
+export * from "./ColumnPickerContent";
diff --git a/saleor/static/dashboard-next/components/PageHeader/PageHeader.tsx b/saleor/static/dashboard-next/components/PageHeader/PageHeader.tsx
index 1fc53ec1c3..843be5df38 100644
--- a/saleor/static/dashboard-next/components/PageHeader/PageHeader.tsx
+++ b/saleor/static/dashboard-next/components/PageHeader/PageHeader.tsx
@@ -12,6 +12,9 @@ import Skeleton from "../Skeleton";
const styles = (theme: Theme) =>
createStyles({
+ root: {
+ display: "flex"
+ },
title: {
flex: 1,
fontSize: 24,
@@ -35,7 +38,7 @@ const PageHeader = withStyles(styles)(
</Typography>
}
>
- {children}
+ <div className={classes.root}>{children}</div>
</ExtendedPageHeader>
)
);
diff --git a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
index 09fa0c286b..e8199242a4 100644
--- a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
@@ -9,12 +9,16 @@ import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter";
import TableRow from "@material-ui/core/TableRow";
-import React from "react";
+import React, { useCallback } from "react";
-import TableCellAvatar from "@saleor/components/TableCellAvatar";
+import TableCellAvatar, {
+ AVATAR_MARGIN
+} from "@saleor/components/TableCellAvatar";
+import { ProductListColumns } from "@saleor/config";
import i18n from "@saleor/i18n";
import { maybe, renderCollection } from "@saleor/misc";
import { ListActions, ListProps } from "@saleor/types";
+import { isSelected } from "@saleor/utils/lists";
import { CategoryDetails_category_products_edges_node } from "../../categories/types/CategoryDetails";
import Checkbox from "../Checkbox";
import Money from "../Money";
@@ -27,7 +31,7 @@ const styles = (theme: Theme) =>
createStyles({
[theme.breakpoints.up("lg")]: {
colName: {
- width: 430
+ width: "auto"
},
colPrice: {
width: 200
@@ -39,12 +43,14 @@ const styles = (theme: Theme) =>
width: 200
}
},
- avatarCell: {
- paddingLeft: theme.spacing.unit * 2,
- paddingRight: 0,
- width: theme.spacing.unit * 5
+ colFill: {
+ padding: 0,
+ width: "100%"
},
colName: {},
+ colNameHeader: {
+ marginLeft: AVATAR_MARGIN
+ },
colPrice: {
textAlign: "right"
},
@@ -53,6 +59,12 @@ const styles = (theme: Theme) =>
link: {
cursor: "pointer"
},
+ table: {
+ tableLayout: "fixed"
+ },
+ tableContainer: {
+ overflowX: "scroll"
+ },
textLeft: {
textAlign: "left"
},
@@ -62,7 +74,7 @@ const styles = (theme: Theme) =>
});
interface ProductListProps
- extends ListProps,
+ extends ListProps<ProductListColumns>,
ListActions,
WithStyles<typeof styles> {
products: CategoryDetails_category_products_edges_node[];
@@ -84,115 +96,147 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
onPreviousPage,
onUpdateListSettings,
onRowClick
- }: ProductListProps) => (
- <Table>
- <TableHead
- selected={selected}
- disabled={disabled}
- items={products}
- toggleAll={toggleAll}
- toolbar={toolbar}
- >
- <TableCell />
- <TableCell className={classes.colName}>
- {i18n.t("Name", { context: "object" })}
- </TableCell>
- <TableCell className={classes.colType}>
- {i18n.t("Type", { context: "object" })}
- </TableCell>
- <TableCell className={classes.colPublished}>
- {i18n.t("Published", { context: "object" })}
- </TableCell>
- <TableCell className={classes.colPrice}>
- {i18n.t("Price", { context: "object" })}
- </TableCell>
- </TableHead>
- <TableFooter>
- <TableRow>
- <TablePagination
- colSpan={6}
- settings={settings}
- hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
- onNextPage={onNextPage}
- onUpdateListSettings={onUpdateListSettings}
- hasPreviousPage={
- pageInfo && !disabled ? pageInfo.hasPreviousPage : false
- }
- onPreviousPage={onPreviousPage}
- />
- </TableRow>
- </TableFooter>
- <TableBody>
- {renderCollection(
- products,
- product => {
- const isSelected = product ? isChecked(product.id) : false;
+ }: ProductListProps) => {
+ const displayColumn = useCallback(
+ (column: ProductListColumns) =>
+ isSelected(column, settings.columns, (a, b) => a === b),
+ [settings.columns]
+ );
+ const columns = 2 + settings.columns.length;
- return (
- <TableRow
- selected={isSelected}
- hover={!!product}
- key={product ? product.id : "skeleton"}
- onClick={product && onRowClick(product.id)}
- className={classes.link}
- >
- <TableCell padding="checkbox">
- <Checkbox
- checked={isSelected}
- disabled={disabled}
- onChange={() => toggle(product.id)}
- />
- </TableCell>
- <TableCellAvatar
- thumbnail={maybe(() => product.thumbnail.url)}
- />
- <TableCell className={classes.colName}>
- {product ? product.name : <Skeleton />}
- </TableCell>
- <TableCell className={classes.colType}>
- {product && product.productType ? (
- product.productType.name
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.colPublished}>
- {product && maybe(() => product.isAvailable !== undefined) ? (
- <StatusLabel
- label={
- product.isAvailable
- ? i18n.t("Published", { context: "product status" })
- : i18n.t("Not published", {
- context: "product status"
- })
- }
- status={product.isAvailable ? "success" : "error"}
- />
- ) : (
- <Skeleton />
- )}
- </TableCell>
- <TableCell className={classes.colPrice}>
- {maybe(() => product.basePrice) &&
- maybe(() => product.basePrice.amount) !== undefined &&
- maybe(() => product.basePrice.currency) !== undefined ? (
- <Money money={product.basePrice} />
- ) : (
- <Skeleton />
- )}
- </TableCell>
- </TableRow>
- );
- },
- () => (
+ return (
+ <div className={classes.tableContainer}>
+ <Table className={classes.table}>
+ <TableHead
+ selected={selected}
+ disabled={disabled}
+ items={products}
+ toggleAll={toggleAll}
+ toolbar={toolbar}
+ >
+ <TableCell className={classes.colName}>
+ <span className={classes.colNameHeader}>
+ {i18n.t("Name", { context: "object" })}
+ </span>
+ </TableCell>
+ {displayColumn("productType") && (
+ <TableCell className={classes.colType}>
+ {i18n.t("Type", { context: "object" })}
+ </TableCell>
+ )}
+ {displayColumn("isPublished") && (
+ <TableCell className={classes.colPublished}>
+ {i18n.t("Published", { context: "object" })}
+ </TableCell>
+ )}
+ {displayColumn("price") && (
+ <TableCell className={classes.colPrice}>
+ {i18n.t("Price", { context: "object" })}
+ </TableCell>
+ )}
+ </TableHead>
+ <TableFooter>
<TableRow>
- <TableCell colSpan={6}>{i18n.t("No products found")}</TableCell>
+ <TablePagination
+ colSpan={columns}
+ settings={settings}
+ hasNextPage={
+ pageInfo && !disabled ? pageInfo.hasNextPage : false
+ }
+ onNextPage={onNextPage}
+ onUpdateListSettings={onUpdateListSettings}
+ hasPreviousPage={
+ pageInfo && !disabled ? pageInfo.hasPreviousPage : false
+ }
+ onPreviousPage={onPreviousPage}
+ />
</TableRow>
- )
- )}
- </TableBody>
- </Table>
- )
+ </TableFooter>
+ <TableBody>
+ {renderCollection(
+ products,
+ 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
+ checked={isSelected}
+ disabled={disabled}
+ onChange={() => toggle(product.id)}
+ />
+ </TableCell>
+ <TableCellAvatar
+ className={classes.colName}
+ thumbnail={maybe(() => product.thumbnail.url)}
+ >
+ {product ? product.name : <Skeleton />}
+ </TableCellAvatar>
+ {displayColumn("productType") && (
+ <TableCell className={classes.colType}>
+ {product && product.productType ? (
+ product.productType.name
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ )}
+ {displayColumn("isPublished") && (
+ <TableCell className={classes.colPublished}>
+ {product &&
+ maybe(() => product.isAvailable !== undefined) ? (
+ <StatusLabel
+ label={
+ product.isAvailable
+ ? i18n.t("Published", {
+ context: "product status"
+ })
+ : i18n.t("Not published", {
+ context: "product status"
+ })
+ }
+ status={product.isAvailable ? "success" : "error"}
+ />
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ )}
+ {displayColumn("price") && (
+ <TableCell className={classes.colPrice}>
+ {maybe(() => product.basePrice) &&
+ maybe(() => product.basePrice.amount) !== undefined &&
+ maybe(() => product.basePrice.currency) !==
+ undefined ? (
+ <Money money={product.basePrice} />
+ ) : (
+ <Skeleton />
+ )}
+ </TableCell>
+ )}
+ </TableRow>
+ );
+ },
+ () => (
+ <TableRow>
+ <TableCell colSpan={columns}>
+ {i18n.t("No products found")}
+ </TableCell>
+ </TableRow>
+ )
+ )}
+ </TableBody>
+ </Table>
+ </div>
+ );
+ }
);
ProductList.displayName = "ProductList";
export default ProductList;
diff --git a/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx b/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
index 7a4bdee9d8..3bfe205c59 100644
--- a/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
+++ b/saleor/static/dashboard-next/components/TableCellAvatar/TableCellAvatar.tsx
@@ -12,6 +12,8 @@ import React from "react";
import Image from "../../icons/Image";
+export const AVATAR_MARGIN = 56;
+
const styles = (theme: Theme) =>
createStyles({
avatar: {
@@ -19,8 +21,16 @@ const styles = (theme: Theme) =>
border: `1px solid ${theme.overrides.MuiCard.root.borderColor}`,
borderRadius: 2,
color: "#bdbdbd",
+ display: "inline-block",
padding: theme.spacing.unit / 2
},
+ children: {
+ alignSelf: "center",
+ marginLeft: theme.spacing.unit * 2
+ },
+ content: {
+ display: "flex"
+ },
root: {
paddingRight: theme.spacing.unit * 3,
width: "1%"
@@ -31,25 +41,35 @@ interface TableCellAvatarProps extends WithStyles<typeof styles> {
className?: string;
thumbnail?: string;
avatarProps?: string;
+ children?: React.ReactNode | React.ReactNodeArray;
}
const TableCellAvatar = withStyles(styles, { name: "TableCellAvatar" })(
- ({ classes, className, thumbnail, avatarProps }: TableCellAvatarProps) => (
+ ({
+ classes,
+ children,
+ className,
+ thumbnail,
+ avatarProps
+ }: TableCellAvatarProps) => (
<TableCell className={classNames(classes.root, className)}>
- {thumbnail === undefined ? (
- <Avatar className={classNames(classes.avatar, avatarProps)}>
- <Cached color="primary" />
- </Avatar>
- ) : thumbnail === null ? (
- <Avatar className={classNames(classes.avatar, avatarProps)}>
- <Image color="primary" />
- </Avatar>
- ) : (
- <Avatar
- className={classNames(classes.avatar, avatarProps)}
- src={thumbnail}
- />
- )}
+ <div className={classes.content}>
+ {thumbnail === undefined ? (
+ <Avatar className={classNames(classes.avatar, avatarProps)}>
+ <Cached color="primary" />
+ </Avatar>
+ ) : thumbnail === null ? (
+ <Avatar className={classNames(classes.avatar, avatarProps)}>
+ <Image color="primary" />
+ </Avatar>
+ ) : (
+ <Avatar
+ className={classNames(classes.avatar, avatarProps)}
+ src={thumbnail}
+ />
+ )}
+ <span className={classes.children}>{children}</span>
+ </div>
</TableCell>
)
);
diff --git a/saleor/static/dashboard-next/config.ts b/saleor/static/dashboard-next/config.ts
index 00ebd89500..d930c0fb79 100644
--- a/saleor/static/dashboard-next/config.ts
+++ b/saleor/static/dashboard-next/config.ts
@@ -1,4 +1,5 @@
import { SearchQueryVariables } from "./containers/BaseSearch";
+import { ListSettings, ListViews } from "./types";
export const APP_MOUNT_URI = "/dashboard/next/";
export const API_URI = "/graphql/";
@@ -11,41 +12,57 @@ export const DEFAULT_INITIAL_SEARCH_DATA: SearchQueryVariables = {
export const PAGINATE_BY = 20;
-export const defaultListSettings = {
- CATEGORY_LIST: {
+export type ProductListColumns = "productType" | "isPublished" | "price";
+export interface AppListViewSettings {
+ [ListViews.CATEGORY_LIST]: ListSettings;
+ [ListViews.COLLECTION_LIST]: ListSettings;
+ [ListViews.CUSTOMER_LIST]: ListSettings;
+ [ListViews.DRAFT_LIST]: ListSettings;
+ [ListViews.NAVIGATION_LIST]: ListSettings;
+ [ListViews.ORDER_LIST]: ListSettings;
+ [ListViews.PAGES_LIST]: ListSettings;
+ [ListViews.PRODUCT_LIST]: ListSettings<ProductListColumns>;
+ [ListViews.SALES_LIST]: ListSettings;
+ [ListViews.SHIPPING_METHODS_LIST]: ListSettings;
+ [ListViews.STAFF_MEMBERS_LIST]: ListSettings;
+ [ListViews.VOUCHER_LIST]: ListSettings;
+}
+export const defaultListSettings: AppListViewSettings = {
+ [ListViews.CATEGORY_LIST]: {
rowNumber: PAGINATE_BY
},
- COLLECTION_LIST: {
+ [ListViews.COLLECTION_LIST]: {
rowNumber: PAGINATE_BY
},
- CUSTOMER_LIST: {
+ [ListViews.CUSTOMER_LIST]: {
rowNumber: PAGINATE_BY
},
- DRAFT_LIST: {
+ [ListViews.DRAFT_LIST]: {
rowNumber: PAGINATE_BY
},
- NAVIGATION_LIST: {
+ [ListViews.NAVIGATION_LIST]: {
rowNumber: PAGINATE_BY
},
- ORDER_LIST: {
+ [ListViews.ORDER_LIST]: {
rowNumber: PAGINATE_BY
},
- PAGES_LIST: {
+ [ListViews.PAGES_LIST]: {
rowNumber: PAGINATE_BY
},
- PRODUCT_LIST: {
+ [ListViews.PRODUCT_LIST]: {
+ columns: ["isPublished", "price", "productType"],
rowNumber: PAGINATE_BY
},
- SALES_LIST: {
+ [ListViews.SALES_LIST]: {
rowNumber: PAGINATE_BY
},
- SHIPPING_METHODS_LIST: {
+ [ListViews.SHIPPING_METHODS_LIST]: {
rowNumber: PAGINATE_BY
},
- STAFF_MEMBERS_LIST: {
+ [ListViews.STAFF_MEMBERS_LIST]: {
rowNumber: PAGINATE_BY
},
- VOUCHER_LIST: {
+ [ListViews.VOUCHER_LIST]: {
rowNumber: PAGINATE_BY
}
};
diff --git a/saleor/static/dashboard-next/hooks/useListSettings.ts b/saleor/static/dashboard-next/hooks/useListSettings.ts
index cb2a93c40d..75c1afd869 100644
--- a/saleor/static/dashboard-next/hooks/useListSettings.ts
+++ b/saleor/static/dashboard-next/hooks/useListSettings.ts
@@ -1,13 +1,20 @@
import useLocalStorage from "@saleor/hooks/useLocalStorage";
-import { defaultListSettings } from "./../config";
+import { AppListViewSettings, defaultListSettings } from "./../config";
import { ListSettings, ListViews } from "./../types";
-export default function useListSettings(listName: ListViews) {
- const [settings, setListSettings] = useLocalStorage(
+export interface UseListSettings<TColumns extends string = string> {
+ settings: ListSettings<TColumns>;
+ updateListSettings: (key: keyof ListSettings<TColumns>, value: any) => void;
+}
+export default function useListSettings<TColumns extends string = string>(
+ listName: ListViews
+): UseListSettings<TColumns> {
+ const [settings, setListSettings] = useLocalStorage<AppListViewSettings>(
"listConfig",
defaultListSettings
);
- const updateListSettings = (key: keyof ListSettings, value: any) => {
+
+ const updateListSettings = (key: keyof ListSettings, value: any) =>
setListSettings(settings => ({
...settings,
[listName]: {
@@ -15,10 +22,9 @@ export default function useListSettings(listName: ListViews) {
[key]: value
}
}));
- };
return {
- settings: settings[listName],
+ settings: settings[listName] as ListSettings<TColumns>,
updateListSettings
};
}
diff --git a/saleor/static/dashboard-next/hooks/useLocalStorage.ts b/saleor/static/dashboard-next/hooks/useLocalStorage.ts
index a9ce76b706..13deab7df6 100644
--- a/saleor/static/dashboard-next/hooks/useLocalStorage.ts
+++ b/saleor/static/dashboard-next/hooks/useLocalStorage.ts
@@ -1,12 +1,17 @@
import { useState } from "react";
-export default function useLocalStorage(key, initialValue) {
- const [storedValue, setStoredValue] = useState(() => {
+export type SetLocalStorageValue<T> = T | ((prevValue: T) => T);
+export type SetLocalStorage<T> = (value: SetLocalStorageValue<T>) => void;
+export default function useLocalStorage<T>(
+ key: string,
+ initialValue: T
+): [T, SetLocalStorage<T>] {
+ const [storedValue, setStoredValue] = useState<T>(() => {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
});
- const setValue = value => {
+ const setValue = (value: SetLocalStorageValue<T>) => {
const valueToStore = value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
window.localStorage.setItem(key, JSON.stringify(valueToStore));
diff --git a/saleor/static/dashboard-next/hooks/useStateFromProps.ts b/saleor/static/dashboard-next/hooks/useStateFromProps.ts
index 6089f3ecb4..b70bbb2a59 100644
--- a/saleor/static/dashboard-next/hooks/useStateFromProps.ts
+++ b/saleor/static/dashboard-next/hooks/useStateFromProps.ts
@@ -16,8 +16,10 @@ function useStateFromProps<T>(
opts = {};
}
- if (!isEqual(prevData, data)) {
- const { mergeFunc, onRefresh } = opts;
+ const { mergeFunc, onRefresh } = opts;
+ const shouldUpdate = !isEqual(prevData, data);
+
+ if (shouldUpdate) {
const newData =
typeof mergeFunc === "function" ? mergeFunc(prevData, state, data) : data;
setState(newData);
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
deleted file mode 100644
index 687bdff9b8..0000000000
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ /dev/null
@@ -1,76 +0,0 @@
-import Button from "@material-ui/core/Button";
-import Card from "@material-ui/core/Card";
-import AddIcon from "@material-ui/icons/Add";
-import React from "react";
-
-import Container from "@saleor/components/Container";
-import PageHeader from "@saleor/components/PageHeader";
-import ProductList from "@saleor/components/ProductList";
-import i18n from "@saleor/i18n";
-import {
- FilterPageProps,
- ListActions,
- ListSettings,
- PageListProps
-} from "@saleor/types";
-import { CategoryDetails_category_products_edges_node } from "../../../categories/types/CategoryDetails";
-import { ProductListUrlFilters } from "../../urls";
-import ProductListFilter from "../ProductListFilter";
-
-export interface ProductListCardProps
- extends PageListProps,
- ListActions,
- FilterPageProps<ProductListUrlFilters> {
- currencySymbol: string;
- settings?: ListSettings;
- products: CategoryDetails_category_products_edges_node[];
-}
-
-export const ProductListCard: React.StatelessComponent<
- ProductListCardProps
-> = ({
- currencySymbol,
- currentTab,
- filtersList,
- filterTabs,
- initialSearch,
- onAdd,
- onAll,
- onSearchChange,
- onFilterAdd,
- onFilterSave,
- onTabChange,
- onFilterDelete,
- ...listProps
-}) => {
- return (
- <Container>
- <PageHeader title={i18n.t("Products")}>
- <Button onClick={onAdd} color="primary" variant="contained">
- {i18n.t("Add product")} <AddIcon />
- </Button>
- </PageHeader>
- <Card>
- <ProductListFilter
- allTabLabel={i18n.t("All Products")}
- currencySymbol={currencySymbol}
- currentTab={currentTab}
- filterLabel={i18n.t("Select all products where:")}
- filterTabs={filterTabs}
- filtersList={filtersList}
- initialSearch={initialSearch}
- searchPlaceholder={i18n.t("Search Products...")}
- onAll={onAll}
- onSearchChange={onSearchChange}
- onFilterAdd={onFilterAdd}
- onFilterSave={onFilterSave}
- onTabChange={onTabChange}
- onFilterDelete={onFilterDelete}
- />
- <ProductList {...listProps} />
- </Card>
- </Container>
- );
-};
-ProductListCard.displayName = "ProductListCard";
-export default ProductListCard;
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/index.ts b/saleor/static/dashboard-next/products/components/ProductListCard/index.ts
deleted file mode 100644
index 995a575ae1..0000000000
--- a/saleor/static/dashboard-next/products/components/ProductListCard/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export { default } from "./ProductListCard";
-export * from "./ProductListCard";
diff --git a/saleor/static/dashboard-next/products/components/ProductListPage/ProductListPage.tsx b/saleor/static/dashboard-next/products/components/ProductListPage/ProductListPage.tsx
new file mode 100644
index 0000000000..0b075177b3
--- /dev/null
+++ b/saleor/static/dashboard-next/products/components/ProductListPage/ProductListPage.tsx
@@ -0,0 +1,133 @@
+import Button from "@material-ui/core/Button";
+import Card from "@material-ui/core/Card";
+import { Theme } from "@material-ui/core/styles";
+import AddIcon from "@material-ui/icons/Add";
+import makeStyles from "@material-ui/styles/makeStyles";
+import React from "react";
+
+import { CategoryDetails_category_products_edges_node } from "@saleor/categories/types/CategoryDetails";
+import ColumnPicker, {
+ ColumnPickerChoice
+} from "@saleor/components/ColumnPicker";
+import Container from "@saleor/components/Container";
+import PageHeader from "@saleor/components/PageHeader";
+import ProductList from "@saleor/components/ProductList";
+import { ProductListColumns } from "@saleor/config";
+import useStateFromProps from "@saleor/hooks/useStateFromProps";
+import i18n from "@saleor/i18n";
+import { FilterPageProps, ListActions, PageListProps } from "@saleor/types";
+import { toggle } from "@saleor/utils/lists";
+import { ProductListUrlFilters } from "../../urls";
+import ProductListFilter from "../ProductListFilter";
+
+export interface ProductListPageProps
+ extends PageListProps<ProductListColumns>,
+ ListActions,
+ FilterPageProps<ProductListUrlFilters> {
+ currencySymbol: string;
+ products: CategoryDetails_category_products_edges_node[];
+}
+
+const useStyles = makeStyles((theme: Theme) => ({
+ columnPicker: {
+ marginRight: theme.spacing.unit * 3
+ }
+}));
+
+export const ProductListPage: React.FC<ProductListPageProps> = props => {
+ const {
+ currencySymbol,
+ currentTab,
+ defaultSettings,
+ filtersList,
+ filterTabs,
+ initialSearch,
+ settings,
+ onAdd,
+ onAll,
+ onSearchChange,
+ onFilterAdd,
+ onFilterSave,
+ onTabChange,
+ onFilterDelete,
+ onUpdateListSettings,
+ ...listProps
+ } = props;
+ const classes = useStyles(props);
+ const [selectedColumns, setSelectedColumns] = useStateFromProps(
+ settings.columns
+ );
+
+ const handleCancel = React.useCallback(
+ () => setSelectedColumns(settings.columns),
+ [settings.columns]
+ );
+
+ const handleColumnToggle = (column: ProductListColumns) =>
+ setSelectedColumns(prevSelectedColumns =>
+ toggle(column, prevSelectedColumns, (a, b) => a === b)
+ );
+
+ const handleReset = () => setSelectedColumns(defaultSettings.columns);
+
+ const handleSave = () => onUpdateListSettings("columns", selectedColumns);
+
+ const columns: ColumnPickerChoice[] = [
+ {
+ label: i18n.t("Published"),
+ value: "isPublished" as ProductListColumns
+ },
+ {
+ label: i18n.t("Price"),
+ value: "price" as ProductListColumns
+ },
+ {
+ label: i18n.t("Type"),
+ value: "productType" as ProductListColumns
+ }
+ ];
+
+ return (
+ <Container>
+ <PageHeader title={i18n.t("Products")}>
+ <ColumnPicker
+ className={classes.columnPicker}
+ columns={columns}
+ selectedColumns={selectedColumns}
+ onColumnToggle={handleColumnToggle}
+ onCancel={handleCancel}
+ onReset={handleReset}
+ onSave={handleSave}
+ />
+ <Button onClick={onAdd} color="primary" variant="contained">
+ {i18n.t("Add product")} <AddIcon />
+ </Button>
+ </PageHeader>
+ <Card>
+ <ProductListFilter
+ allTabLabel={i18n.t("All Products")}
+ currencySymbol={currencySymbol}
+ currentTab={currentTab}
+ filterLabel={i18n.t("Select all products where:")}
+ filterTabs={filterTabs}
+ filtersList={filtersList}
+ initialSearch={initialSearch}
+ searchPlaceholder={i18n.t("Search Products...")}
+ onAll={onAll}
+ onSearchChange={onSearchChange}
+ onFilterAdd={onFilterAdd}
+ onFilterSave={onFilterSave}
+ onTabChange={onTabChange}
+ onFilterDelete={onFilterDelete}
+ />
+ <ProductList
+ {...listProps}
+ settings={{ ...settings, columns: selectedColumns }}
+ onUpdateListSettings={onUpdateListSettings}
+ />
+ </Card>
+ </Container>
+ );
+};
+ProductListPage.displayName = "ProductListPage";
+export default ProductListPage;
diff --git a/saleor/static/dashboard-next/products/components/ProductListPage/index.ts b/saleor/static/dashboard-next/products/components/ProductListPage/index.ts
new file mode 100644
index 0000000000..a5e54aa30a
--- /dev/null
+++ b/saleor/static/dashboard-next/products/components/ProductListPage/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./ProductListPage";
+export * from "./ProductListPage";
diff --git a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index 85cdf7d417..17f6d7e00e 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -9,6 +9,7 @@ import DeleteFilterTabDialog from "@saleor/components/DeleteFilterTabDialog";
import SaveFilterTabDialog, {
SaveFilterTabDialogFormData
} from "@saleor/components/SaveFilterTabDialog";
+import { defaultListSettings, ProductListColumns } from "@saleor/config";
import useBulkActions from "@saleor/hooks/useBulkActions";
import useListSettings from "@saleor/hooks/useListSettings";
import useLocale from "@saleor/hooks/useLocale";
@@ -21,7 +22,7 @@ import useShop from "@saleor/hooks/useShop";
import i18n from "@saleor/i18n";
import { getMutationState, maybe } from "@saleor/misc";
import { ListViews } from "@saleor/types";
-import ProductListCard from "../../components/ProductListCard";
+import ProductListPage from "../../components/ProductListPage";
import {
TypedProductBulkDeleteMutation,
TypedProductBulkPublishMutation
@@ -63,7 +64,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
params.ids
);
- const { updateListSettings, settings } = useListSettings(
+ const { updateListSettings, settings } = useListSettings<ProductListColumns>(
ListViews.PRODUCT_LIST
);
const tabs = getFilterTabs();
@@ -196,9 +197,12 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
return (
<>
- <ProductListCard
+ <ProductListPage
currencySymbol={currencySymbol}
currentTab={currentTab}
+ defaultSettings={
+ defaultListSettings[ListViews.PRODUCT_LIST]
+ }
settings={settings}
filtersList={createFilterChips(
params,
diff --git a/saleor/static/dashboard-next/storybook/config.js b/saleor/static/dashboard-next/storybook/config.js
index 93cbb15258..d2b62b2c29 100644
--- a/saleor/static/dashboard-next/storybook/config.js
+++ b/saleor/static/dashboard-next/storybook/config.js
@@ -104,7 +104,7 @@ function loadStories() {
// Products
require("./stories/products/ProductCreatePage");
require("./stories/products/ProductImagePage");
- require("./stories/products/ProductListCard");
+ require("./stories/products/ProductListPage");
require("./stories/products/ProductUpdatePage");
require("./stories/products/ProductVariantCreatePage");
require("./stories/products/ProductVariantImageSelectDialog");
diff --git a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx b/saleor/static/dashboard-next/storybook/stories/products/ProductListPage.tsx
similarity index 65%
rename from saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
rename to saleor/static/dashboard-next/storybook/stories/products/ProductListPage.tsx
index ffddca86fb..12b6de38c0 100644
--- a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/products/ProductListPage.tsx
@@ -9,30 +9,34 @@ import {
listActionsProps,
pageListProps
} from "../../../fixtures";
-import ProductListCard, {
- ProductListCardProps
-} from "../../../products/components/ProductListCard";
+import ProductListPage, {
+ ProductListPageProps
+} from "../../../products/components/ProductListPage";
import Decorator from "../../Decorator";
const products = categoryFixture(placeholderImage).products.edges.map(
edge => edge.node
);
-const props: ProductListCardProps = {
+const props: ProductListPageProps = {
...listActionsProps,
...pageListProps.default,
...filterPageProps,
- products
+ products,
+ settings: {
+ ...pageListProps.default.settings,
+ columns: ["thumbnail"]
+ }
};
storiesOf("Views / Products / Product list", module)
.addDecorator(Decorator)
- .add("default", () => <ProductListCard {...props} />)
+ .add("default", () => <ProductListPage {...props} />)
.add("with custom filters", () => (
- <ProductListCard {...props} filtersList={filters} />
+ <ProductListPage {...props} filtersList={filters} />
))
.add("loading", () => (
- <ProductListCard
+ <ProductListPage
{...props}
products={undefined}
filtersList={undefined}
@@ -40,4 +44,4 @@ storiesOf("Views / Products / Product list", module)
disabled={true}
/>
))
- .add("no data", () => <ProductListCard {...props} products={[]} />);
+ .add("no data", () => <ProductListPage {...props} products={[]} />);
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index accab494b4..777042b5d7 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -9,7 +9,8 @@ export interface UserError {
message: string;
}
-export interface ListSettings {
+export interface ListSettings<TColumn extends string = string> {
+ columns?: TColumn[];
rowNumber: number;
}
@@ -28,17 +29,21 @@ export enum ListViews {
VOUCHER_LIST = "VOUCHER_LIST"
}
-export interface ListProps {
+export interface ListProps<TColumns extends string = string> {
disabled: boolean;
pageInfo?: {
hasNextPage: boolean;
hasPreviousPage: boolean;
};
- settings?: ListSettings;
+ settings?: ListSettings<TColumns>;
onNextPage: () => void;
onPreviousPage: () => void;
onRowClick: (id: string) => () => void;
- onUpdateListSettings?(key: keyof ListSettings, value: any): void;
+ onUpdateListSettings?: (
+ key: keyof ListSettings<TColumns>,
+ value: any
+ ) => void;
+ onListSettingsReset?: () => void;
}
export interface ListActionsWithoutToolbar {
toggle: (id: string) => void;
@@ -53,7 +58,9 @@ export type TabListActions<
export interface ListActions extends ListActionsWithoutToolbar {
toolbar: React.ReactNode | React.ReactNodeArray;
}
-export interface PageListProps extends ListProps {
+export interface PageListProps<TColumns extends string = string>
+ extends ListProps<TColumns> {
+ defaultSettings: ListSettings<TColumns>;
onAdd: () => void;
}
export interface FilterPageProps<TUrlFilters> {