Skip to content

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

Persist filter state in querystring

dominik-zeglen23a0f3b67278
diff --git a/saleor/static/dashboard-next/components/Filter/Filter.tsx b/saleor/static/dashboard-next/components/Filter/Filter.tsx
index 794ab477e4..8501137c24 100644
--- a/saleor/static/dashboard-next/components/Filter/Filter.tsx
+++ b/saleor/static/dashboard-next/components/Filter/Filter.tsx
@@ -16,12 +16,14 @@ import * as React from "react";
import { FilterContent } from ".";
import i18n from "../../i18n";
+import { FilterContentSubmitData } from "./FilterContent";
import { IFilter } from "./types";
export interface FilterProps {
currencySymbol: string;
menu: IFilter;
filterLabel: string;
+ onFilterAdd: (filter: FilterContentSubmitData) => void;
}
const styles = (theme: Theme) =>
@@ -86,7 +88,8 @@ const Filter = withStyles(styles, { name: "Filter" })(
classes,
currencySymbol,
filterLabel,
- menu
+ menu,
+ onFilterAdd
}: FilterProps & WithStyles<typeof styles>) => {
const anchor = React.useRef<HTMLDivElement>();
const [isFilterMenuOpened, setFilterMenuOpened] = React.useState(false);
@@ -132,7 +135,7 @@ const Filter = withStyles(styles, { name: "Filter" })(
<FilterContent
currencySymbol={currencySymbol}
filters={menu}
- onSubmit={() => undefined}
+ onSubmit={onFilterAdd}
/>
</Paper>
</Grow>
diff --git a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
index 645c13bc23..f1f451552a 100644
--- a/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
+++ b/saleor/static/dashboard-next/components/TableFilter/FilterChips.tsx
@@ -7,7 +7,7 @@ import ClearIcon from "@material-ui/icons/Clear";
import { createStyles, makeStyles, useTheme } from "@material-ui/styles";
import * as React from "react";
-import Filter from "../Filter";
+import Filter, { FilterContentSubmitData } from "../Filter";
import { IFilter } from "../Filter/types";
import Hr from "../Hr";
@@ -101,6 +101,9 @@ interface FilterChipProps {
filtersList: Filter[];
filterLabel: string;
placeholder: string;
+ search: string;
+ onSearchChange: (event: React.ChangeEvent<any>) => void;
+ onFilterAdd: (filter: FilterContentSubmitData) => void;
}
export const FilterChips: React.FC<FilterChipProps> = ({
@@ -108,7 +111,10 @@ export const FilterChips: React.FC<FilterChipProps> = ({
filtersList,
menu,
filterLabel,
- placeholder
+ placeholder,
+ onSearchChange,
+ search,
+ onFilterAdd
}) => {
const theme = useTheme();
const classes = useStyles({ theme });
@@ -120,8 +126,14 @@ export const FilterChips: React.FC<FilterChipProps> = ({
currencySymbol={currencySymbol}
menu={menu}
filterLabel={filterLabel}
+ onFilterAdd={onFilterAdd}
+ />
+ <Search
+ fullWidth
+ placeholder={placeholder}
+ value={search}
+ onChange={onSearchChange}
/>
- <Search fullWidth placeholder={placeholder} />
</div>
{filtersList && filtersList.length > 0 ? (
<div className={classes.filterContainer}>
diff --git a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
index 7854eaafe0..332196a024 100644
--- a/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListCard/ProductListCard.tsx
@@ -5,6 +5,7 @@ import * as React from "react";
import { CategoryDetails_category_products_edges_node } from "../../../categories/types/CategoryDetails";
import Container from "../../../components/Container";
+import { FilterContentSubmitData } from "../../../components/Filter";
import PageHeader from "../../../components/PageHeader";
import ProductList from "../../../components/ProductList";
import { Filter } from "../../../components/TableFilter";
@@ -12,7 +13,7 @@ import i18n from "../../../i18n";
import { ListActions, PageListProps } from "../../../types";
import ProductListFilter, { ProductListFilterTabs } from "../ProductListFilter";
-interface ProductListCardProps extends PageListProps, ListActions {
+export interface ProductListCardProps extends PageListProps, ListActions {
currencySymbol: string;
currentTab: ProductListFilterTabs;
filtersList: Filter[];
@@ -21,6 +22,8 @@ interface ProductListCardProps extends PageListProps, ListActions {
onAvailable: () => void;
onCustomFilter: () => void;
onOfStock: () => void;
+ onSearchChange: (value: string) => void;
+ onFilterAdd: (filter: FilterContentSubmitData) => void;
}
export const ProductListCard: React.StatelessComponent<
@@ -44,7 +47,9 @@ export const ProductListCard: React.StatelessComponent<
selected,
toggle,
toggleAll,
- toolbar
+ toolbar,
+ onSearchChange,
+ onFilterAdd
}) => (
<Container>
<PageHeader title={i18n.t("Products")}>
@@ -61,6 +66,8 @@ export const ProductListCard: React.StatelessComponent<
onAllProducts={onAllProducts}
onOfStock={onOfStock}
onCustomFilter={onCustomFilter}
+ onSearchChange={onSearchChange}
+ onFilterAdd={onFilterAdd}
/>
<ProductList
products={products}
diff --git a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
index 1477988ff5..635eb95666 100644
--- a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
@@ -1,5 +1,7 @@
import * as React from "react";
+import Debounce from "../../../components/Debounce";
+import { FilterContentSubmitData } from "../../../components/Filter";
import { FieldType, IFilter } from "../../../components/Filter/types";
import {
Filter,
@@ -23,6 +25,8 @@ interface ProductListFilterProps {
onAvailable: () => void;
onOfStock: () => void;
onCustomFilter: () => void;
+ onSearchChange: (value: string) => void;
+ onFilterAdd: (filter: FilterContentSubmitData) => void;
}
export enum ProductFilterKeys {
@@ -98,33 +102,54 @@ const ProductListFilter: React.StatelessComponent<ProductListFilterProps> = ({
onAllProducts,
onAvailable,
onOfStock,
- onCustomFilter
-}) => (
- <>
- <FilterTabs
- currentTab={["all", "available", "outOfStock", "custom"].indexOf(
- currentTab
- )}
- >
- <FilterTab label={i18n.t("All Products")} onClick={onAllProducts} />
- <FilterTab label={i18n.t("Available")} onClick={onAvailable} />
- <FilterTab label={i18n.t("Out Of Stock")} onClick={onOfStock} />
- {currentTab === "custom" && filtersList && filtersList.length > 0 && (
- <FilterTab
- onClick={onCustomFilter}
- value={0}
- label={i18n.t("Custom Filter")}
- />
- )}
- </FilterTabs>
- <FilterChips
- currencySymbol={currencySymbol}
- menu={filterMenu}
- filtersList={filtersList}
- filterLabel={i18n.t("Select all products where:")}
- placeholder={i18n.t("Search Products...")}
- />
- </>
-);
+ onCustomFilter,
+ onSearchChange,
+ onFilterAdd
+}) => {
+ const [search, setSearch] = React.useState("");
+
+ return (
+ <>
+ <FilterTabs
+ currentTab={["all", "available", "outOfStock", "custom"].indexOf(
+ currentTab
+ )}
+ >
+ <FilterTab label={i18n.t("All Products")} onClick={onAllProducts} />
+ <FilterTab label={i18n.t("Available")} onClick={onAvailable} />
+ <FilterTab label={i18n.t("Out Of Stock")} onClick={onOfStock} />
+ {currentTab === "custom" && filtersList && filtersList.length > 0 && (
+ <FilterTab
+ onClick={onCustomFilter}
+ value={0}
+ label={i18n.t("Custom Filter")}
+ />
+ )}
+ </FilterTabs>
+ <Debounce debounceFn={onSearchChange}>
+ {debounceSearchChange => {
+ const handleSearchChange = (event: React.ChangeEvent<any>) => {
+ const value = event.target.value;
+ setSearch(value);
+ debounceSearchChange(value);
+ };
+
+ return (
+ <FilterChips
+ currencySymbol={currencySymbol}
+ menu={filterMenu}
+ filtersList={filtersList}
+ filterLabel={i18n.t("Select all products where:")}
+ placeholder={i18n.t("Search Products...")}
+ search={search}
+ onSearchChange={handleSearchChange}
+ onFilterAdd={onFilterAdd}
+ />
+ );
+ }}
+ </Debounce>
+ </>
+ );
+};
ProductListFilter.displayName = "ProductListFilter";
export default ProductListFilter;
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index dc493de09e..2e1f048173 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -217,14 +217,14 @@ const productListQuery = gql`
$after: String
$last: Int
$before: String
- $stockAvailability: StockAvailability
+ $filter: ProductFilterInput
) {
products(
before: $before
after: $after
first: $first
last: $last
- stockAvailability: $stockAvailability
+ filter: $filter
) {
edges {
node {
diff --git a/saleor/static/dashboard-next/products/types/ProductList.ts b/saleor/static/dashboard-next/products/types/ProductList.ts
index ddeebc551e..0758898759 100644
--- a/saleor/static/dashboard-next/products/types/ProductList.ts
+++ b/saleor/static/dashboard-next/products/types/ProductList.ts
@@ -2,7 +2,7 @@
/* eslint-disable */
// This file was automatically generated and should not be edited.
-import { StockAvailability } from "./../../types/globalTypes";
+import { ProductFilterInput } from "./../../types/globalTypes";
// ====================================================
// GraphQL query operation: ProductList
@@ -63,5 +63,5 @@ export interface ProductListVariables {
after?: string | null;
last?: number | null;
before?: string | null;
- stockAvailability?: StockAvailability | null;
+ filter?: ProductFilterInput | null;
}
diff --git a/saleor/static/dashboard-next/products/urls.ts b/saleor/static/dashboard-next/products/urls.ts
index 9c1bdebc26..dccf63fb6e 100644
--- a/saleor/static/dashboard-next/products/urls.ts
+++ b/saleor/static/dashboard-next/products/urls.ts
@@ -12,7 +12,11 @@ export const productAddUrl = productAddPath;
export const productListPath = productSection;
export type ProductListUrlDialog = "publish" | "unpublish" | "delete";
export type ProductListUrlFilters = Partial<{
+ isPublished: boolean;
+ priceFrom: string;
+ priceTo: string;
status: StockAvailability;
+ query: string;
}>;
export type ProductListUrlQueryParams = BulkAction &
Dialog<ProductListUrlDialog> &
diff --git a/saleor/static/dashboard-next/products/views/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
similarity index 87%
rename from saleor/static/dashboard-next/products/views/ProductList.tsx
rename to saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index 31750391dd..195ff9b943 100644
--- a/saleor/static/dashboard-next/products/views/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -4,24 +4,26 @@ import IconButton from "@material-ui/core/IconButton";
import DeleteIcon from "@material-ui/icons/Delete";
import * as React from "react";
-import ActionDialog from "../../components/ActionDialog";
-import useBulkActions from "../../hooks/useBulkActions";
-import useNavigator from "../../hooks/useNavigator";
-import useNotifier from "../../hooks/useNotifier";
-import usePaginator, { createPaginationState } from "../../hooks/usePaginator";
-import useShop from "../../hooks/useShop";
-import i18n from "../../i18n";
-import { getMutationState, maybe } from "../../misc";
-import { StockAvailability } from "../../types/globalTypes";
-import ProductListCard from "../components/ProductListCard";
-import { getTabName } from "../misc";
+import ActionDialog from "../../../components/ActionDialog";
+import useBulkActions from "../../../hooks/useBulkActions";
+import useNavigator from "../../../hooks/useNavigator";
+import useNotifier from "../../../hooks/useNotifier";
+import usePaginator, {
+ createPaginationState
+} from "../../../hooks/usePaginator";
+import useShop from "../../../hooks/useShop";
+import i18n from "../../../i18n";
+import { getMutationState, maybe } from "../../../misc";
+import { StockAvailability } from "../../../types/globalTypes";
+import ProductListCard from "../../components/ProductListCard";
+import { getTabName } from "../../misc";
import {
TypedProductBulkDeleteMutation,
TypedProductBulkPublishMutation
-} from "../mutations";
-import { TypedProductListQuery } from "../queries";
-import { productBulkDelete } from "../types/productBulkDelete";
-import { productBulkPublish } from "../types/productBulkPublish";
+} from "../../mutations";
+import { TypedProductListQuery } from "../../queries";
+import { productBulkDelete } from "../../types/productBulkDelete";
+import { productBulkPublish } from "../../types/productBulkPublish";
import {
productAddUrl,
productListUrl,
@@ -29,7 +31,8 @@ import {
ProductListUrlFilters,
ProductListUrlQueryParams,
productUrl
-} from "../urls";
+} from "../../urls";
+import { getActiveFilters, createFilter } from "./filters";
interface ProductListProps {
params: ProductListUrlQueryParams;
@@ -63,6 +66,16 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
navigate(productListUrl(filters));
};
+ const addFilterField = (filter: ProductListUrlFilters) => {
+ reset();
+ navigate(
+ productListUrl({
+ ...getActiveFilters(params),
+ ...filter
+ })
+ );
+ };
+
const openModal = (action: ProductListUrlDialog, ids: string[]) =>
navigate(
productListUrl({
@@ -79,6 +92,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
displayLoader
variables={{
...paginationState,
+ query: params.query,
stockAvailability: params.status
}}
>
@@ -197,6 +211,14 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
selected={listElements.length}
toggle={toggle}
toggleAll={toggleAll}
+ onSearchChange={query =>
+ addFilterField({
+ query
+ })
+ }
+ onFilterAdd={filter =>
+ addFilterField(createFilter(filter))
+ }
/>
<ActionDialog
open={params.action === "delete"}
diff --git a/saleor/static/dashboard-next/products/views/ProductList/filters.ts b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
new file mode 100644
index 0000000000..e760774b57
--- /dev/null
+++ b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
@@ -0,0 +1,56 @@
+import { FilterContentSubmitData } from "../../../components/Filter";
+import { ProductFilterInput } from "../../../types/globalTypes";
+import { ProductFilterKeys } from "../../components/ProductListFilter";
+import { ProductListUrlFilters, ProductListUrlQueryParams } from "../../urls";
+
+export function getFilterVariables(
+ params: ProductListUrlFilters
+): ProductFilterInput {
+ return {
+ isPublished: params.isPublished,
+ price: {
+ gte: parseFloat(params.priceFrom),
+ lte: parseFloat(params.priceTo)
+ },
+ search: params.query,
+ stockAvailability: params.status
+ };
+}
+
+export function getActiveFilters(
+ params: ProductListUrlQueryParams
+): ProductListUrlFilters {
+ return Object.keys(params)
+ .filter(key =>
+ ((["query", "status", "priceFrom", "priceTo", "isPublished"] as Array<
+ keyof ProductListUrlFilters
+ >) as string[]).includes(key)
+ )
+ .reduce((acc, key) => {
+ acc[key] = params[key];
+ return acc;
+ }, {});
+}
+
+export function createFilter(
+ filter: FilterContentSubmitData
+): ProductListUrlFilters {
+ const filterName = filter.name;
+ if (filterName === ProductFilterKeys.priceEqual.toString()) {
+ const value = filter.value as string;
+ return {
+ priceFrom: value,
+ priceTo: value
+ };
+ } else if (filterName === ProductFilterKeys.priceRange.toString()) {
+ const { value } = filter;
+ return {
+ priceFrom: value[0],
+ priceTo: value[1]
+ };
+ } else if (filterName === ProductFilterKeys.published.toString()) {
+ return {
+ isPublished: filter.value === "true"
+ };
+ }
+}
diff --git a/saleor/static/dashboard-next/products/views/ProductList/index.ts b/saleor/static/dashboard-next/products/views/ProductList/index.ts
new file mode 100644
index 0000000000..3308d7c0f5
--- /dev/null
+++ b/saleor/static/dashboard-next/products/views/ProductList/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./ProductList";
+export * from "./ProductList";
diff --git a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx b/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
index 6c9a6fae14..07db7aefe5 100644
--- a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
@@ -5,7 +5,9 @@ import * as placeholderImage from "../../../../images/placeholder255x255.png";
import { category as categoryFixture } from "../../../categories/fixtures";
import { Filter } from "../../../components/TableFilter";
import { listActionsProps, pageListProps } from "../../../fixtures";
-import ProductListCard from "../../../products/components/ProductListCard";
+import ProductListCard, {
+ ProductListCardProps
+} from "../../../products/components/ProductListCard";
import Decorator from "../../Decorator";
const products = categoryFixture(placeholderImage).products.edges.map(
@@ -14,122 +16,97 @@ const products = categoryFixture(placeholderImage).products.edges.map(
const filtersList: Filter[] = [
{
- label: "Gardner-Schultz",
+ label: "Property X is ",
onClick: () => undefined
},
{
- label: "Davis, Brown and Ray",
+ label: "Property Y is ",
onClick: () => undefined
},
{
- label: "Franklin Inc",
+ label: "Property Z is ",
onClick: () => undefined
},
{
- label: "Gardner-Schultz",
+ label: "Property X is ",
onClick: () => undefined
},
{
- label: "Davis, Brown and Ray",
+ label: "Property Y is ",
onClick: () => undefined
},
{
- label: "Franklin Inc",
+ label: "Property Z is ",
onClick: () => undefined
},
{
- label: "Gardner-Schultz",
+ label: "Property X is ",
onClick: () => undefined
},
{
- label: "Davis, Brown and Ray",
+ label: "Property Y is ",
onClick: () => undefined
},
{
- label: "Franklin Inc",
+ label: "Property Z is ",
onClick: () => undefined
},
{
- label: "Gardner-Schultz",
+ label: "Property X is ",
onClick: () => undefined
},
{
- label: "Davis, Brown and Ray",
+ label: "Property Y is ",
onClick: () => undefined
},
{
- label: "Franklin Inc",
+ label: "Property Z is ",
onClick: () => undefined
},
{
- label: "Gardner-Schultz",
+ label: "Property X is ",
onClick: () => undefined
},
{
- label: "Davis, Brown and Ray",
+ label: "Property Y is ",
onClick: () => undefined
},
{
- label: "Franklin Inc",
+ label: "Property Z is ",
onClick: () => undefined
}
-];
+].map((filter, filterIndex) => ({
+ ...filter,
+ label: filter.label + filterIndex
+}));
+
+const props: ProductListCardProps = {
+ ...listActionsProps,
+ ...pageListProps.default,
+ currencySymbol: "USD",
+ currentTab: "all",
+ filtersList: [],
+ onAllProducts: () => undefined,
+ onAvailable: () => undefined,
+ onCustomFilter: () => undefined,
+ onOfStock: () => undefined,
+ onSearchChange: val => console.log(val),
+ products
+};
storiesOf("Views / Products / Product list", module)
.addDecorator(Decorator)
- .add("default", () => (
- <ProductListCard
- filtersList={[]}
- currentTab="all"
- products={products}
- {...listActionsProps}
- {...pageListProps.default}
- onAllProducts={() => undefined}
- onAvailable={() => undefined}
- onOfStock={() => undefined}
- onCustomFilter={() => undefined}
- currencySymbol="USD"
- />
- ))
+ .add("default", () => <ProductListCard {...props} />)
.add("with custom filters", () => (
- <ProductListCard
- products={products}
- {...listActionsProps}
- {...pageListProps.default}
- filtersList={filtersList}
- currentTab="custom"
- onAllProducts={() => undefined}
- onAvailable={() => undefined}
- onOfStock={() => undefined}
- onCustomFilter={() => undefined}
- currencySymbol="USD"
- />
+ <ProductListCard {...props} filtersList={filtersList} currentTab="custom" />
))
.add("loading", () => (
<ProductListCard
- {...listActionsProps}
- {...pageListProps.loading}
+ {...props}
products={undefined}
filtersList={undefined}
currentTab={undefined}
- onAllProducts={() => undefined}
- onAvailable={() => undefined}
- onOfStock={() => undefined}
- onCustomFilter={() => undefined}
- currencySymbol="USD"
+ disabled={true}
/>
))
- .add("no data", () => (
- <ProductListCard
- products={[]}
- {...listActionsProps}
- {...pageListProps.default}
- filtersList={[]}
- currentTab="all"
- onAllProducts={() => undefined}
- onAvailable={() => undefined}
- onOfStock={() => undefined}
- onCustomFilter={() => undefined}
- currencySymbol="USD"
- />
- ));
+ .add("no data", () => <ProductListCard {...props} products={[]} />);
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index 5b5e3cc562..9fc74fc905 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -244,6 +244,11 @@ export interface AttributeCreateInput {
values?: (AttributeValueCreateInput | null)[] | null;
}
+export interface AttributeInput {
+ slug: string;
+ value: string;
+}
+
export interface AttributeUpdateInput {
name?: string | null;
removeValues?: (string | null)[] | null;
@@ -432,6 +437,22 @@ export interface PageTranslationInput {
contentJson?: any | null;
}
+export interface PriceRangeInput {
+ gte?: number | null;
+ lte?: number | null;
+}
+
+export interface ProductFilterInput {
+ isPublished?: boolean | null;
+ collections?: (string | null)[] | null;
+ categories?: (string | null)[] | null;
+ price?: PriceRangeInput | null;
+ attributes?: (AttributeInput | null)[] | null;
+ stockAvailability?: StockAvailability | null;
+ productType?: string | null;
+ search?: string | null;
+}
+
export interface ProductTypeInput {
name?: string | null;
hasVariants?: boolean | null;