Skip to content

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

Add product filter tabs

dominik-zeglen5245c535ebfe
diff --git a/saleor/static/dashboard-next/misc.ts b/saleor/static/dashboard-next/misc.ts
index aa2469d3f6..ce78be1193 100644
--- a/saleor/static/dashboard-next/misc.ts
+++ b/saleor/static/dashboard-next/misc.ts
@@ -122,3 +122,13 @@ export function maybe<T>(exp: () => T, d?: T) {
return d;
}
}
+
+export function only<T>(obj: T, key: keyof T): boolean {
+ return Object.keys(obj).every(objKey =>
+ objKey === key ? obj[key] !== undefined : obj[key] === undefined
+ );
+}
+
+export function empty(obj: object): boolean {
+ return Object.keys(obj).every(key => obj[key] === undefined);
+}
diff --git a/saleor/static/dashboard-next/products/index.tsx b/saleor/static/dashboard-next/products/index.tsx
index 6e1b2d8d06..fa976a5609 100644
--- a/saleor/static/dashboard-next/products/index.tsx
+++ b/saleor/static/dashboard-next/products/index.tsx
@@ -19,7 +19,8 @@ const ProductList: React.StatelessComponent<RouteComponentProps<any>> = ({
const qs = parseQs(location.search.substr(1));
const params: ProductListQueryParams = {
after: qs.after,
- before: qs.before
+ before: qs.before,
+ status: qs.status
};
return <ProductListComponent params={params} />;
};
diff --git a/saleor/static/dashboard-next/products/misc.ts b/saleor/static/dashboard-next/products/misc.ts
new file mode 100644
index 0000000000..8229a172a0
--- /dev/null
+++ b/saleor/static/dashboard-next/products/misc.ts
@@ -0,0 +1,21 @@
+import { empty, only } from "../misc";
+import { StockAvailability } from "../types/globalTypes";
+import { ProductListFilterTabs } from "./components/ProductListFilter";
+import { ProductListQueryParams } from "./views/ProductList";
+
+export const getTabName = (
+ qs: ProductListQueryParams
+): ProductListFilterTabs => {
+ const filters = {
+ status: qs.status
+ };
+ if (empty(filters)) {
+ return "all";
+ }
+ if (only(filters, "status")) {
+ return filters.status === StockAvailability.IN_STOCK
+ ? "available"
+ : "outOfStock";
+ }
+ return "custom";
+};
diff --git a/saleor/static/dashboard-next/products/queries.ts b/saleor/static/dashboard-next/products/queries.ts
index c2a5a56a27..3b56a32f53 100644
--- a/saleor/static/dashboard-next/products/queries.ts
+++ b/saleor/static/dashboard-next/products/queries.ts
@@ -214,8 +214,20 @@ export const TypedProductListQuery = Query as React.ComponentType<
export const productListQuery = gql`
${fragmentMoney}
- query ProductList($first: Int, $after: String, $last: Int, $before: String) {
- products(before: $before, after: $after, first: $first, last: $last) {
+ query ProductList(
+ $first: Int
+ $after: String
+ $last: Int
+ $before: String
+ $stockAvailability: StockAvailability
+ ) {
+ products(
+ before: $before
+ after: $after
+ first: $first
+ last: $last
+ stockAvailability: $stockAvailability
+ ) {
edges {
node {
id
diff --git a/saleor/static/dashboard-next/products/types/ProductList.ts b/saleor/static/dashboard-next/products/types/ProductList.ts
index 97b953bd88..b93881efc0 100644
--- a/saleor/static/dashboard-next/products/types/ProductList.ts
+++ b/saleor/static/dashboard-next/products/types/ProductList.ts
@@ -1,6 +1,8 @@
/* tslint:disable */
// This file was automatically generated and should not be edited.
+import { StockAvailability } from "./../../types/globalTypes";
+
// ====================================================
// GraphQL query operation: ProductList
// ====================================================
@@ -60,4 +62,5 @@ export interface ProductListVariables {
after?: string | null;
last?: number | null;
before?: string | null;
+ stockAvailability?: StockAvailability | null;
}
diff --git a/saleor/static/dashboard-next/products/views/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList.tsx
index 9494e8bbff..b4d62b4827 100644
--- a/saleor/static/dashboard-next/products/views/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList.tsx
@@ -1,3 +1,4 @@
+import { stringify as stringifyQs } from "qs";
import * as React from "react";
import { productAddUrl, productUrl } from "..";
@@ -5,13 +6,20 @@ import ErrorMessageCard from "../../components/ErrorMessageCard";
import Navigator from "../../components/Navigator";
import { createPaginationState, Paginator } from "../../components/Paginator";
import { maybe } from "../../misc";
+import { StockAvailability } from "../../types/globalTypes";
import ProductListCard from "../components/ProductListCard";
+import { getTabName } from "../misc";
import { productListQuery, TypedProductListQuery } from "../queries";
-export type ProductListQueryParams = Partial<{
- after: string;
- before: string;
-}>;
+export interface ProductListFilters {
+ status: StockAvailability;
+}
+export type ProductListQueryParams = Partial<
+ {
+ after: string;
+ before: string;
+ } & ProductListFilters
+>;
interface ProductListProps {
params: ProductListQueryParams;
@@ -24,11 +32,19 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
}) => (
<Navigator>
{navigate => {
+ const changeFilters = (newParams: ProductListQueryParams) =>
+ navigate(
+ "?" +
+ stringifyQs({
+ ...params,
+ ...newParams
+ })
+ );
const paginationState = createPaginationState(PAGINATE_BY, params);
return (
<TypedProductListQuery
query={productListQuery}
- variables={paginationState}
+ variables={{ ...paginationState, stockAvailability: params.status }}
fetchPolicy="network-only"
>
{({ data, loading, error }) => {
@@ -36,7 +52,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
return <ErrorMessageCard message="Something went wrong" />;
}
- const currentTab = "all";
+ const currentTab = getTabName(params);
return (
<Paginator
pageInfo={maybe(() => data.products.pageInfo)}
@@ -61,10 +77,22 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
pageInfo={pageInfo}
onRowClick={id => () =>
navigate(productUrl(encodeURIComponent(id)))}
- onAllProducts={() => undefined}
+ onAllProducts={() =>
+ changeFilters({
+ status: undefined
+ })
+ }
onCustomFilter={() => undefined}
- onAvailable={() => undefined}
- onOfStock={() => undefined}
+ onAvailable={() =>
+ changeFilters({
+ status: StockAvailability.IN_STOCK
+ })
+ }
+ onOfStock={() =>
+ changeFilters({
+ status: StockAvailability.OUT_OF_STOCK
+ })
+ }
/>
)}
</Paginator>
diff --git a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx b/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
index dd2b8caad4..1e715a8a93 100644
--- a/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/products/ProductListCard.tsx
@@ -32,7 +32,7 @@ storiesOf("Views / Products / Product list", module)
.add("default", () => (
<ProductListCard
filtersList={[]}
- currentTab={0}
+ currentTab="all"
products={products}
{...pageListProps.default}
onAllProducts={() => undefined}
@@ -46,7 +46,7 @@ storiesOf("Views / Products / Product list", module)
products={products}
{...pageListProps.default}
filtersList={filtersList}
- currentTab={0}
+ currentTab="custom"
onAllProducts={() => undefined}
onAvailable={() => undefined}
onOfStock={() => undefined}
@@ -70,7 +70,7 @@ storiesOf("Views / Products / Product list", module)
products={[]}
{...pageListProps.default}
filtersList={[]}
- currentTab={0}
+ currentTab="all"
onAllProducts={() => undefined}
onAvailable={() => undefined}
onOfStock={() => undefined}
diff --git a/saleor/static/dashboard-next/types/globalTypes.ts b/saleor/static/dashboard-next/types/globalTypes.ts
index 12ec78e17d..b78f8eec9b 100644
--- a/saleor/static/dashboard-next/types/globalTypes.ts
+++ b/saleor/static/dashboard-next/types/globalTypes.ts
@@ -81,6 +81,11 @@ export enum PermissionEnum {
MANAGE_USERS = "MANAGE_USERS",
}
+export enum StockAvailability {
+ IN_STOCK = "IN_STOCK",
+ OUT_OF_STOCK = "OUT_OF_STOCK",
+}
+
export enum TaxRateType {
ACCOMMODATION = "ACCOMMODATION",
ADMISSION_TO_CULTURAL_EVENTS = "ADMISSION_TO_CULTURAL_EVENTS",