Skip to content

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

Render chips

dominik-zeglen2fb00fdce774
diff --git a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
index 8db2f331a8..eed7cf1c6a 100644
--- a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
+++ b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
@@ -23,7 +23,7 @@ function checkFilterValue(value: string | string[]): boolean {
if (typeof value === "string") {
return value !== "";
}
- return value.every(v => !!v);
+ return value.some(v => !!v);
}
const useStyles = makeStyles({
@@ -73,7 +73,9 @@ const FilterContent: React.FC<FilterContentProps> = ({
menus.map(
(filterItem, filterItemIndex) =>
!isLeaf(filterItem) && (
- <React.Fragment key={filterItem.label.toString() + filterItem.value}>
+ <React.Fragment
+ key={filterItem.label.toString() + filterItem.value}
+ >
<FormSpacer />
<SingleSelectField
choices={filterItem.children.map(filterItem => ({
diff --git a/saleor/static/dashboard-next/hooks/useLocale.ts b/saleor/static/dashboard-next/hooks/useLocale.ts
new file mode 100644
index 0000000000..60499f582c
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useLocale.ts
@@ -0,0 +1,9 @@
+import { useContext } from "react";
+
+import { LocaleContext } from "../components/Locale";
+
+function useLocale() {
+ const themeInfo = useContext(LocaleContext);
+ return themeInfo;
+}
+export default useLocale;
diff --git a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
index 635eb95666..a0057ea450 100644
--- a/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
+++ b/saleor/static/dashboard-next/products/components/ProductListFilter/ProductListFilter.tsx
@@ -10,6 +10,7 @@ import {
FilterTabs
} from "../../../components/TableFilter";
import i18n from "../../../i18n";
+import { StockAvailability } from "../../../types/globalTypes";
export type ProductListFilterTabs =
| "all"
@@ -60,7 +61,17 @@ const filterMenu: IFilter = [
children: [],
data: {
fieldLabel: i18n.t("Stock quantity"),
- type: FieldType.range
+ options: [
+ {
+ label: i18n.t("Available"),
+ value: StockAvailability.IN_STOCK
+ },
+ {
+ label: i18n.t("Out Of Stock"),
+ value: StockAvailability.OUT_OF_STOCK
+ }
+ ],
+ type: FieldType.select
},
label: i18n.t("Stock"),
value: ProductFilterKeys.stock.toString()
diff --git a/saleor/static/dashboard-next/products/urls.ts b/saleor/static/dashboard-next/products/urls.ts
index dccf63fb6e..dd2894ff80 100644
--- a/saleor/static/dashboard-next/products/urls.ts
+++ b/saleor/static/dashboard-next/products/urls.ts
@@ -12,7 +12,7 @@ export const productAddUrl = productAddPath;
export const productListPath = productSection;
export type ProductListUrlDialog = "publish" | "unpublish" | "delete";
export type ProductListUrlFilters = Partial<{
- isPublished: boolean;
+ isPublished: string;
priceFrom: string;
priceTo: string;
status: StockAvailability;
diff --git a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
index 09273ce93f..73503260c3 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
+++ b/saleor/static/dashboard-next/products/views/ProductList/ProductList.tsx
@@ -6,6 +6,7 @@ import * as React from "react";
import ActionDialog from "../../../components/ActionDialog";
import useBulkActions from "../../../hooks/useBulkActions";
+import useLocale from "../../../hooks/useLocale";
import useNavigator from "../../../hooks/useNavigator";
import useNotifier from "../../../hooks/useNotifier";
import usePaginator, {
@@ -32,7 +33,12 @@ import {
ProductListUrlQueryParams,
productUrl
} from "../../urls";
-import { getActiveFilters, createFilter, getFilterVariables } from "./filters";
+import {
+ createFilter,
+ createFilterChips,
+ getActiveFilters,
+ getFilterVariables
+} from "./filters";
interface ProductListProps {
params: ProductListUrlQueryParams;
@@ -43,6 +49,7 @@ const PAGINATE_BY = 20;
export const ProductList: React.StatelessComponent<ProductListProps> = ({
params
}) => {
+ const locale = useLocale();
const navigate = useNavigator();
const notify = useNotifier();
const paginate = usePaginator();
@@ -66,7 +73,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
navigate(productListUrl(filters));
};
- const addFilterField = (filter: ProductListUrlFilters) => {
+ const changeFilterField = (filter: ProductListUrlFilters) => {
reset();
navigate(
productListUrl({
@@ -86,6 +93,7 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
);
const paginationState = createPaginationState(PAGINATE_BY, params);
+ const currencySymbol = maybe(() => shop.defaultCurrency, "USD");
return (
<TypedProductListQuery
@@ -150,9 +158,16 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
return (
<>
<ProductListCard
- currencySymbol={maybe(() => shop.defaultCurrency)}
+ currencySymbol={currencySymbol}
currentTab={currentTab}
- filtersList={[]}
+ filtersList={createFilterChips(
+ params,
+ {
+ currencySymbol,
+ locale
+ },
+ changeFilterField
+ )}
onAdd={() => navigate(productAddUrl)}
disabled={loading}
products={
@@ -211,12 +226,12 @@ export const ProductList: React.StatelessComponent<ProductListProps> = ({
toggle={toggle}
toggleAll={toggleAll}
onSearchChange={query =>
- addFilterField({
+ changeFilterField({
query
})
}
onFilterAdd={filter =>
- addFilterField(createFilter(filter))
+ changeFilterField(createFilter(filter))
}
/>
<ActionDialog
diff --git a/saleor/static/dashboard-next/products/views/ProductList/filters.ts b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
index e760774b57..46002b85aa 100644
--- a/saleor/static/dashboard-next/products/views/ProductList/filters.ts
+++ b/saleor/static/dashboard-next/products/views/ProductList/filters.ts
@@ -1,5 +1,10 @@
import { FilterContentSubmitData } from "../../../components/Filter";
-import { ProductFilterInput } from "../../../types/globalTypes";
+import { Filter } from "../../../components/TableFilter";
+import i18n from "../../../i18n";
+import {
+ ProductFilterInput,
+ StockAvailability
+} from "../../../types/globalTypes";
import { ProductFilterKeys } from "../../components/ProductListFilter";
import { ProductListUrlFilters, ProductListUrlQueryParams } from "../../urls";
@@ -7,7 +12,8 @@ export function getFilterVariables(
params: ProductListUrlFilters
): ProductFilterInput {
return {
- isPublished: params.isPublished,
+ isPublished:
+ params.isPublished !== undefined ? params.isPublished === "true" : null,
price: {
gte: parseFloat(params.priceFrom),
lte: parseFloat(params.priceTo)
@@ -50,7 +56,133 @@ export function createFilter(
};
} else if (filterName === ProductFilterKeys.published.toString()) {
return {
- isPublished: filter.value === "true"
+ isPublished: filter.value as string
};
+ } else if (filterName === ProductFilterKeys.stock.toString()) {
+ const value = filter.value as string;
+ return {
+ status: StockAvailability[value]
+ };
+ }
+}
+
+function exists(param: any): boolean {
+ return param !== undefined && param !== null;
+}
+
+interface ProductListChipFormatData {
+ currencySymbol: string;
+ locale: string;
+}
+export function createFilterChips(
+ filters: ProductListUrlFilters,
+ formatData: ProductListChipFormatData,
+ onClose: (filters: ProductListUrlFilters) => void
+): Filter[] {
+ let filterChips: Filter[] = [];
+
+ if (exists(filters.priceFrom) || exists(filters.priceTo)) {
+ if (filters.priceFrom === filters.priceTo) {
+ filterChips = [
+ ...filterChips,
+ {
+ label: i18n.t("Price is {{ price }}", {
+ price: parseFloat(filters.priceFrom).toLocaleString(
+ formatData.locale,
+ {
+ currency: formatData.currencySymbol,
+ style: "currency"
+ }
+ )
+ }),
+ onClick: () =>
+ onClose({
+ ...filters,
+ priceFrom: undefined,
+ priceTo: undefined
+ })
+ }
+ ];
+ } else {
+ if (exists(filters.priceFrom)) {
+ filterChips = [
+ ...filterChips,
+ {
+ label: i18n.t("Price from {{ price }}", {
+ price: parseFloat(filters.priceFrom).toLocaleString(
+ formatData.locale,
+ {
+ currency: formatData.currencySymbol,
+ style: "currency"
+ }
+ )
+ }),
+ onClick: () =>
+ onClose({
+ ...filters,
+ priceFrom: undefined
+ })
+ }
+ ];
+ }
+
+ if (exists(filters.priceTo)) {
+ filterChips = [
+ ...filterChips,
+ {
+ label: i18n.t("Price to {{ price }}", {
+ price: parseFloat(filters.priceTo).toLocaleString(
+ formatData.locale,
+ {
+ currency: formatData.currencySymbol,
+ style: "currency"
+ }
+ )
+ }),
+ onClick: () =>
+ onClose({
+ ...filters,
+ priceTo: undefined
+ })
+ }
+ ];
+ }
+ }
+ }
+
+ if (exists(filters.status)) {
+ filterChips = [
+ ...filterChips,
+ {
+ label:
+ filters.status === StockAvailability.IN_STOCK.toString()
+ ? i18n.t("Available")
+ : i18n.t("Out Of Stock"),
+ onClick: () =>
+ onClose({
+ ...filters,
+ status: undefined
+ })
+ }
+ ];
+ }
+
+ if (exists(filters.isPublished)) {
+ filterChips = [
+ ...filterChips,
+ {
+ label:
+ filters.isPublished === StockAvailability.IN_STOCK.toString()
+ ? i18n.t("Published")
+ : i18n.t("Hidden"),
+ onClick: () =>
+ onClose({
+ ...filters,
+ isPublished: undefined
+ })
+ }
+ ];
}
+
+ return filterChips;
}