Skip to content

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

Add listSettings hook

Krzysztof Bialoglowiczadad8552dbb1
diff --git a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
index 4f55b4db4d..d05f320702 100644
--- a/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/components/ProductList/ProductList.tsx
@@ -71,7 +71,7 @@ interface ProductListProps
export const ProductList = withStyles(styles, { name: "ProductList" })(
({
classes,
- currentRowNum,
+ listSettings,
disabled,
isChecked,
pageInfo,
@@ -82,7 +82,7 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
toolbar,
onNextPage,
onPreviousPage,
- onRowNumChange,
+ onUpdateListSettings,
onRowClick
}: ProductListProps) => {
return (
@@ -112,10 +112,10 @@ export const ProductList = withStyles(styles, { name: "ProductList" })(
<TableRow>
<TablePagination
colSpan={6}
- currentRowNum={currentRowNum}
+ listSettings={listSettings}
hasNextPage={pageInfo && !disabled ? pageInfo.hasNextPage : false}
onNextPage={onNextPage}
- onRowNumChange={onRowNumChange}
+ onUpdateListSettings={onUpdateListSettings}
hasPreviousPage={
pageInfo && !disabled ? pageInfo.hasPreviousPage : false
}
diff --git a/saleor/static/dashboard-next/components/RowNumberSelect/RowNumberSelect.tsx b/saleor/static/dashboard-next/components/RowNumberSelect/RowNumberSelect.tsx
index 429d924576..2ca84d4689 100644
--- a/saleor/static/dashboard-next/components/RowNumberSelect/RowNumberSelect.tsx
+++ b/saleor/static/dashboard-next/components/RowNumberSelect/RowNumberSelect.tsx
@@ -10,6 +10,7 @@ import classNames from "classnames";
import React from "react";
import i18n from "../../i18n";
+import { ListSettings } from "../../types";
const styles = (theme: Theme) =>
createStyles({
@@ -37,8 +38,8 @@ const styles = (theme: Theme) =>
interface RowNumberSelectProps extends WithStyles<typeof styles> {
choices: number[];
className?: string;
- currentRowNum: number;
- onChange(event: any);
+ listSettings: ListSettings;
+ onChange(key: keyof ListSettings, value: any);
}
const RowNumberSelect = withStyles(styles, { name: "RowNumberSelect" })(
@@ -46,7 +47,7 @@ const RowNumberSelect = withStyles(styles, { name: "RowNumberSelect" })(
classes,
className,
choices,
- currentRowNum,
+ listSettings,
onChange
}: RowNumberSelectProps) => {
return (
@@ -54,8 +55,8 @@ const RowNumberSelect = withStyles(styles, { name: "RowNumberSelect" })(
<span className={classes.label}>{i18n.t("No of Rows:")}</span>
<Select
className={classes.select}
- value={currentRowNum}
- onChange={onChange}
+ value={listSettings.rowNumber}
+ onChange={event => onChange("rowNumber", event.target.value)}
>
{choices.length > 0 &&
choices.map(choice => (
diff --git a/saleor/static/dashboard-next/components/TablePagination/TablePagination.tsx b/saleor/static/dashboard-next/components/TablePagination/TablePagination.tsx
index 3629413d94..bb8100a4d1 100644
--- a/saleor/static/dashboard-next/components/TablePagination/TablePagination.tsx
+++ b/saleor/static/dashboard-next/components/TablePagination/TablePagination.tsx
@@ -12,6 +12,7 @@ import Toolbar from "@material-ui/core/Toolbar";
import React from "react";
import RowNumberSelect from "@saleor/components/RowNumberSelect";
+import { ListSettings } from "../../types";
import TablePaginationActions from "./TablePaginationActions";
const styles = (theme: Theme) =>
@@ -61,13 +62,13 @@ interface TablePaginationProps extends WithStyles<typeof styles> {
backIconButtonProps?: Partial<IconButtonProps>;
colSpan: number;
component?: string | typeof TableCell;
- currentRowNum?: number;
+ listSettings?: ListSettings;
hasNextPage: boolean;
hasPreviousPage: boolean;
nextIconButtonProps?: Partial<IconButtonProps>;
onNextPage(event);
onPreviousPage(event);
- onRowNumChange?(event);
+ onUpdateListSettings?(key: keyof ListSettings, value: any): void;
}
const TablePagination = withStyles(styles, { name: "TablePagination" })(
@@ -77,13 +78,13 @@ const TablePagination = withStyles(styles, { name: "TablePagination" })(
classes,
colSpan: colSpanProp,
component: Component,
- currentRowNum,
+ listSettings,
hasNextPage,
hasPreviousPage,
nextIconButtonProps,
onNextPage,
onPreviousPage,
- onRowNumChange,
+ onUpdateListSettings,
...other
}: TablePaginationProps) => {
let colSpan;
@@ -96,11 +97,11 @@ const TablePagination = withStyles(styles, { name: "TablePagination" })(
<Component className={classes.root} colSpan={colSpan} {...other}>
<Toolbar className={classes.toolbar}>
<div className={classes.spacer}>
- {currentRowNum && (
+ {listSettings && (
<RowNumberSelect
choices={[20, 30, 50, 100]}
- currentRowNum={currentRowNum}
- onChange={onRowNumChange}
+ listSettings={listSettings}
+ onChange={onUpdateListSettings}
/>
)}
</div>
diff --git a/saleor/static/dashboard-next/config.ts b/saleor/static/dashboard-next/config.ts
index e0da6c4503..e6debe176e 100644
--- a/saleor/static/dashboard-next/config.ts
+++ b/saleor/static/dashboard-next/config.ts
@@ -10,3 +10,18 @@ export const DEFAULT_INITIAL_SEARCH_DATA: SearchQueryVariables = {
};
export const PAGINATE_BY = 20;
+
+export const defaultListSettings = {
+ CATEGORY_LIST: {
+ name: "CATEGORY_LIST",
+ rowNumber: PAGINATE_BY
+ },
+ COLLECTION_LIST: {
+ name: "COLLECTION_LIST",
+ rowNumber: PAGINATE_BY
+ },
+ PRODUCT_LIST: {
+ name: "PRODUCT_LIST",
+ rowNumber: PAGINATE_BY
+ }
+};
diff --git a/saleor/static/dashboard-next/fixtures.ts b/saleor/static/dashboard-next/fixtures.ts
index 18e1e301a5..0f467bbda8 100644
--- a/saleor/static/dashboard-next/fixtures.ts
+++ b/saleor/static/dashboard-next/fixtures.ts
@@ -7,8 +7,8 @@ const pageInfo = {
};
export const pageListProps: { [key: string]: PageListProps } = {
default: {
- currentRowNum: 20,
disabled: false,
+ listSettings: { name: undefined, rowNumber: 20 },
onAdd: undefined,
onNextPage: undefined,
onPreviousPage: undefined,
@@ -16,8 +16,8 @@ export const pageListProps: { [key: string]: PageListProps } = {
pageInfo
},
loading: {
- currentRowNum: undefined,
disabled: true,
+ listSettings: undefined,
onAdd: undefined,
onNextPage: undefined,
onPreviousPage: undefined,
diff --git a/saleor/static/dashboard-next/hooks/useListSettings.ts b/saleor/static/dashboard-next/hooks/useListSettings.ts
new file mode 100644
index 0000000000..916d46a1c6
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useListSettings.ts
@@ -0,0 +1,21 @@
+import React from "react";
+import { defaultListSettings } from "./../config";
+import { Lists, ListSettings } from "./../types";
+
+export default function useListSettings(listName: Lists) {
+ const [listSettings, setListSettings] = React.useState(defaultListSettings);
+ const updateListSettings = (key: keyof ListSettings, value: any) => {
+ setListSettings(settings => ({
+ ...settings,
+ [listName]: {
+ ...settings[listName],
+ [key]: value
+ }
+ }));
+ };
+
+ return {
+ listSettings,
+ updateListSettings
+ };
+}
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index d381ae343f..4e8501f992 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -8,7 +8,12 @@ import PageHeader from "@saleor/components/PageHeader";
import ProductList from "@saleor/components/ProductList";
import { CategoryDetails_category_products_edges_node } from "../../../categories/types/CategoryDetails";
import i18n from "../../../i18n";
-import { FilterPageProps, ListActions, PageListProps } from "../../../types";
+import {
+ FilterPageProps,
+ ListActions,
+ ListSettings,
+ PageListProps
+} from "../../../types";
import { ProductListUrlFilters } from "../../urls";
import ProductListFilter from "../ProductListFilter";
@@ -16,8 +21,8 @@ export interface ProductListCardProps
extends PageListProps,
ListActions,
FilterPageProps<ProductListUrlFilters> {
- currentRowNum?: number;
currencySymbol: string;
+ listSettings?: ListSettings;
products: CategoryDetails_category_products_edges_node[];
}
diff --git a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index 294cd547a9..cf44a1042a 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -10,16 +10,17 @@ import SaveFilterTabDialog, {
SaveFilterTabDialogFormData
} from "@saleor/components/SaveFilterTabDialog";
import useBulkActions from "@saleor/hooks/useBulkActions";
+import useListSettings from "@saleor/hooks/useListSettings";
import useLocale from "@saleor/hooks/useLocale";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import usePaginator, {
createPaginationState
} from "@saleor/hooks/usePaginator";
-import useRowChange from "@saleor/hooks/useRowChange";
import useShop from "@saleor/hooks/useShop";
import i18n from "../../../i18n";
import { getMutationState, maybe } from "../../../misc";
+import { Lists } from "../../../types";
import ProductListCard from "../../components/ProductListCard";
import {
TypedProductBulkDeleteMutation,
@@ -62,8 +63,10 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
const { isSelected, listElements, reset, toggle, toggleAll } = useBulkActions(
params.ids
);
- const { rowNumber, onRowChange } = useRowChange();
+ const { updateListSettings, listSettings } = useListSettings(
+ Lists.PRODUCT_LIST
+ );
const tabs = getFilterTabs();
const currentTab =
@@ -129,14 +132,17 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
handleTabChange(tabs.length + 1);
};
- const paginationState = createPaginationState(rowNumber, params);
+ const paginationState = createPaginationState(
+ listSettings.PRODUCT_LIST.rowNumber,
+ params
+ );
const currencySymbol = maybe(() => shop.defaultCurrency, "USD");
const queryVariables = React.useMemo(
() => ({
...paginationState,
filter: getFilterVariables(params)
}),
- [params, rowNumber]
+ [params, listSettings.PRODUCT_LIST.rowNumber]
);
return (
@@ -197,7 +203,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
<ProductListCard
currencySymbol={currencySymbol}
currentTab={currentTab}
- currentRowNum={rowNumber}
+ listSettings={listSettings.PRODUCT_LIST}
filtersList={createFilterChips(
params,
{
@@ -213,7 +219,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
)}
onNextPage={loadNextPage}
onPreviousPage={loadPreviousPage}
- onRowNumChange={onRowChange}
+ onUpdateListSettings={updateListSettings}
pageInfo={pageInfo}
onRowClick={id => () => navigate(productUrl(id))}
onAll={() =>
@@ -264,7 +270,9 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
confirmButtonState={bulkDeleteMutationState}
onClose={closeModal}
onConfirm={() =>
- productBulkDelete({ variables: { ids: params.ids } })
+ productBulkDelete({
+ variables: { ids: params.ids }
+ })
}
title={i18n.t("Remove products")}
variant="delete"
diff --git a/saleor/static/dashboard-next/types.ts b/saleor/static/dashboard-next/types.ts
index 4740e5dd35..b50109d7a4 100644
--- a/saleor/static/dashboard-next/types.ts
+++ b/saleor/static/dashboard-next/types.ts
@@ -9,17 +9,28 @@ export interface UserError {
message: string;
}
+export interface ListSettings {
+ name: string;
+ rowNumber: number;
+}
+
+export enum Lists {
+ CATEGORY_LIST = "CATEGORY_LIST",
+ COLLECTION_PAGE = "COLLECTION_PAGE",
+ PRODUCT_LIST = "PRODUCT_LIST"
+}
+
export interface ListProps {
- currentRowNum?: number;
disabled: boolean;
pageInfo?: {
hasNextPage: boolean;
hasPreviousPage: boolean;
};
+ listSettings?: ListSettings;
onNextPage: () => void;
onPreviousPage: () => void;
onRowClick: (id: string) => () => void;
- onRowNumChange?: (event: React.ChangeEvent<any>) => void;
+ onUpdateListSettings?(key: keyof ListSettings, value: any): void;
}
export interface ListActionsWithoutToolbar {
toggle: (id: string) => void;