Skip to content

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

Add early filter render prototype

dominik-zeglen8015ebaafafd
diff --git a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
index b4b8ee5730..962761c112 100644
--- a/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
+++ b/saleor/static/dashboard-next/components/Filter/FilterContent.tsx
@@ -1,3 +1,4 @@
+import Button from "@material-ui/core/Button";
import {
createStyles,
Theme,
@@ -6,15 +7,104 @@ import {
} from "@material-ui/core/styles";
import * as React from "react";
-import { Filter } from "./Filter";
+import i18n from "../../i18n";
+import { getMenuItemByValue, isLeaf, walkToRoot } from "../../utils/menu";
+import FormSpacer from "../FormSpacer";
+import SingleSelectField from "../SingleSelectField";
+import FilterElement from "./FilterElement";
+import { IFilter } from "./types";
+export interface FilterContentSubmitData {
+ name: string;
+ value: string | string[];
+}
export interface FilterContentProps {
- filters: Filter[];
+ filters: IFilter;
+ onSubmit: (data: FilterContentSubmitData) => void;
}
const styles = (theme: Theme) => createStyles({});
const FilterContent = withStyles(styles, { name: "FilterContent" })(
- ({ classes }: FilterContentProps & WithStyles<typeof styles>) => <div />
+ ({
+ classes,
+ filters,
+ onSubmit
+ }: FilterContentProps & WithStyles<typeof styles>) => {
+ const [menuValue, setMenuValue] = React.useState<string>("");
+ const [filterValue, setFilterValue] = React.useState<string | string[]>("");
+
+ const activeMenu = menuValue
+ ? getMenuItemByValue(filters, menuValue)
+ : undefined;
+ const menus = menuValue
+ ? walkToRoot(filters, menuValue).slice(-1)
+ : undefined;
+
+ const onMenuChange = (event: React.ChangeEvent<any>) => {
+ setMenuValue(event.target.value);
+ setFilterValue("");
+ };
+
+ return (
+ <>
+ <SingleSelectField
+ choices={filters.map(filterItem => ({
+ label: filterItem.label,
+ value: filterItem.value
+ }))}
+ onChange={onMenuChange}
+ selectProps={{
+ placeholder: i18n.t("Select Filter...")
+ }}
+ value={menus ? menus[0].value : menuValue}
+ />
+ <FormSpacer />
+ {menus &&
+ menus.map((filterItem, filterItemIndex) => (
+ <>
+ <SingleSelectField
+ choices={filterItem.children.map(filterItem => ({
+ label: filterItem.label,
+ value: filterItem.value
+ }))}
+ onChange={onMenuChange}
+ selectProps={{
+ placeholder: i18n.t("Select Filter...")
+ }}
+ value={
+ filterItemIndex === menus.length - 1
+ ? menuValue
+ : menus[filterItemIndex - 1].label.toString()
+ }
+ />
+ <FormSpacer />
+ </>
+ ))}
+ {activeMenu && isLeaf(activeMenu) && (
+ <>
+ <FilterElement
+ filter={activeMenu}
+ value={filterValue}
+ onChange={value => setFilterValue(value)}
+ />
+ {filterValue && (
+ <Button
+ color="primary"
+ onClick={() =>
+ onSubmit({
+ name: activeMenu.value,
+ value: filterValue
+ })
+ }
+ >
+ {i18n.t("Add filter")}
+ </Button>
+ )}
+ </>
+ )}
+ </>
+ );
+ }
);
FilterContent.displayName = "FilterContent";
export default FilterContent;
diff --git a/saleor/static/dashboard-next/components/Filter/FilterElement.tsx b/saleor/static/dashboard-next/components/Filter/FilterElement.tsx
new file mode 100644
index 0000000000..ac011fb126
--- /dev/null
+++ b/saleor/static/dashboard-next/components/Filter/FilterElement.tsx
@@ -0,0 +1,69 @@
+import TextField from "@material-ui/core/TextField";
+import * as React from "react";
+
+import i18n from "../../i18n";
+import FormSpacer from "../FormSpacer";
+import SingleSelectField from "../SingleSelectField";
+import { FieldType, IFilterItem } from "./types";
+
+export interface FilterElementProps {
+ className?: string;
+ filter: IFilterItem;
+ value: string | string[];
+ onChange: (value: string | string[]) => void;
+}
+
+const FilterElement: React.FC<FilterElementProps> = ({
+ className,
+ filter,
+ onChange,
+ value
+}) => {
+ if (filter.data.type === FieldType.date) {
+ return (
+ <TextField
+ className={className}
+ fullWidth
+ label={filter.label}
+ type="date"
+ onChange={event => onChange(event.target.value)}
+ value={value}
+ />
+ );
+ } else if (filter.data.type === FieldType.rangeDate) {
+ return (
+ <>
+ <TextField
+ className={className}
+ fullWidth
+ label={i18n.t("From")}
+ type="date"
+ value={value[0]}
+ onChange={event => onChange([event.target.value, value[1]])}
+ />
+ <FormSpacer />
+ <TextField
+ className={className}
+ fullWidth
+ label={i18n.t("To")}
+ type="date"
+ value={value[1]}
+ onChange={event => onChange([value[0], event.target.value])}
+ />
+ </>
+ );
+ } else if (filter.data.type === FieldType.select) {
+ return (
+ <SingleSelectField
+ choices={filter.children.map(filterItem => ({
+ label: filterItem.label,
+ value: filterItem.value
+ }))}
+ onChange={event => onChange(event.target.value)}
+ />
+ );
+ }
+ return <TextField className={className} fullWidth label={filter.label} />;
+};
+FilterElement.displayName = "FilterElement";
+export default FilterElement;
diff --git a/saleor/static/dashboard-next/components/Filter/filter.ts b/saleor/static/dashboard-next/components/Filter/filter.ts
deleted file mode 100644
index 2d8748476f..0000000000
--- a/saleor/static/dashboard-next/components/Filter/filter.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-export enum FieldType {
- date,
- number,
- range,
- select,
- text
-}
-
-export interface FilterData {
- label: React.ReactNode;
- value: string;
- type: FieldType;
-}
-
-export interface FilterChildren {
- children?: Filter[];
-}
-export type Filter = FilterData & FilterChildren;
-
-function validateOptions();
diff --git a/saleor/static/dashboard-next/components/Filter/types.ts b/saleor/static/dashboard-next/components/Filter/types.ts
new file mode 100644
index 0000000000..449a9606b3
--- /dev/null
+++ b/saleor/static/dashboard-next/components/Filter/types.ts
@@ -0,0 +1,19 @@
+import { IMenu, IMenuItem } from "../../utils/menu";
+
+export enum FieldType {
+ date,
+ number,
+ range,
+ rangeDate,
+ rangePrice,
+ select,
+ text
+}
+
+export interface FilterData {
+ type: FieldType;
+}
+
+export type IFilterItem = IMenuItem<FilterData>;
+
+export type IFilter = IMenu<FilterData>;
diff --git a/saleor/static/dashboard-next/components/SingleSelectField/SingleSelectField.tsx b/saleor/static/dashboard-next/components/SingleSelectField/SingleSelectField.tsx
index f9be8bc561..f269b3a208 100644
--- a/saleor/static/dashboard-next/components/SingleSelectField/SingleSelectField.tsx
+++ b/saleor/static/dashboard-next/components/SingleSelectField/SingleSelectField.tsx
@@ -5,6 +5,7 @@ import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import Select, { SelectProps } from "@material-ui/core/Select";
import { createStyles, withStyles, WithStyles } from "@material-ui/core/styles";
+import classNames from "classnames";
import * as React from "react";
import i18n from "../../i18n";
@@ -20,6 +21,7 @@ interface SingleSelectFieldProps extends WithStyles<typeof styles> {
value: string;
label: string | React.ReactNode;
}>;
+ className?: string;
disabled?: boolean;
error?: boolean;
hint?: string;
@@ -34,6 +36,7 @@ export const SingleSelectField = withStyles(styles, {
name: "SingleSelectField"
})(
({
+ className,
classes,
disabled,
error,
@@ -55,7 +58,7 @@ export const SingleSelectField = withStyles(styles, {
return (
<FormControl
- className={classes.formControl}
+ className={classNames(classes.formControl, className)}
error={error}
disabled={disabled}
>
diff --git a/saleor/static/dashboard-next/storybook/stories/components/Filter.tsx b/saleor/static/dashboard-next/storybook/stories/components/Filter.tsx
index 0c9b917409..58143d5820 100644
--- a/saleor/static/dashboard-next/storybook/stories/components/Filter.tsx
+++ b/saleor/static/dashboard-next/storybook/stories/components/Filter.tsx
@@ -2,10 +2,65 @@ import { storiesOf } from "@storybook/react";
import * as React from "react";
import { FilterContent, FilterContentProps } from "../../../components/Filter";
+import { FieldType } from "../../../components/Filter/types";
import CardDecorator from "../../CardDecorator";
import Decorator from "../../Decorator";
-const props: FilterContentProps = {};
+const props: FilterContentProps = {
+ filters: [
+ {
+ children: [
+ {
+ children: [],
+ data: {
+ type: FieldType.date
+ },
+ label: "Exactly",
+ value: "date-exactly"
+ },
+ {
+ children: [],
+ data: {
+ type: FieldType.rangeDate
+ },
+ label: "Range",
+ value: "date-range"
+ }
+ ],
+ data: {
+ type: FieldType.select
+ },
+ label: "Date",
+ value: "date"
+ },
+ {
+ children: [
+ {
+ children: [],
+ data: {
+ type: FieldType.number
+ },
+ label: "Exactly",
+ value: "price-exactly"
+ },
+ {
+ children: [],
+ data: {
+ type: FieldType.rangePrice
+ },
+ label: "Range",
+ value: "price-range"
+ }
+ ],
+ data: {
+ type: FieldType.select
+ },
+ label: "Price",
+ value: "price"
+ }
+ ],
+ onSubmit: data => console.log(data)
+};
storiesOf("Generics / Filter", module)
.addDecorator(CardDecorator)
diff --git a/saleor/static/dashboard-next/utils/menu/__snapshots__/menu.test.ts.snap b/saleor/static/dashboard-next/utils/menu/__snapshots__/menu.test.ts.snap
index 39d9892479..68f546b094 100644
--- a/saleor/static/dashboard-next/utils/menu/__snapshots__/menu.test.ts.snap
+++ b/saleor/static/dashboard-next/utils/menu/__snapshots__/menu.test.ts.snap
@@ -74,7 +74,28 @@ Array [
"label": "4",
"value": "4",
},
- undefined,
- undefined,
+ Object {
+ "children": Array [],
+ "data": null,
+ "label": "4.1",
+ "value": "4.1",
+ },
+]
+`;
+
+exports[`Walk through menu Properly returns all nodes on the path to root 1`] = `
+Array [
+ Object {
+ "children": Array [],
+ "data": null,
+ "label": "4.1",
+ "value": "4.1",
+ },
+ Object {
+ "children": Array [],
+ "data": null,
+ "label": "3",
+ "value": "3",
+ },
]
`;
diff --git a/saleor/static/dashboard-next/utils/menu/menu.test.ts b/saleor/static/dashboard-next/utils/menu/menu.test.ts
index 205fe25194..9b6d6b65b2 100644
--- a/saleor/static/dashboard-next/utils/menu/menu.test.ts
+++ b/saleor/static/dashboard-next/utils/menu/menu.test.ts
@@ -5,7 +5,8 @@ import {
IMenu,
toFlat,
validateMenuOptions,
- walkToMenuItem
+ walkToMenuItem,
+ walkToRoot
} from "./menu";
const validMenu: IMenu = [
@@ -161,4 +162,10 @@ describe("Walk through menu", () => {
it("Properly returns all nodes on the path", () => {
expect(walkToMenuItem(validMenu, [3, 0])).toMatchSnapshot();
});
+
+ it("Properly returns all nodes on the path to root", () => {
+ expect(
+ walkToRoot(validMenu, validMenu[3].children[0].value)
+ ).toMatchSnapshot();
+ });
});
diff --git a/saleor/static/dashboard-next/utils/menu/menu.ts b/saleor/static/dashboard-next/utils/menu/menu.ts
index 406c68498e..4abae4363a 100644
--- a/saleor/static/dashboard-next/utils/menu/menu.ts
+++ b/saleor/static/dashboard-next/utils/menu/menu.ts
@@ -50,7 +50,7 @@ export function getMenuItemByValue<TMenuData = {}>(
menuItem => menuItem.value === value
);
- return _fromFlat(flatMenu, flatMenuItem.id);
+ return _fromFlat(flatMenu, flatMenuItem);
}
function _walkToMenuItem<TMenuData = {}>(
@@ -74,6 +74,31 @@ export function walkToMenuItem<TMenuData = {}>(
return [walkByNode, ..._walkToMenuItem(walkByNode, path.slice(1))];
}
+function _walkToRoot<TMenuData = {}>(
+ flatMenu: IFlatMenu<TMenuData>,
+ parent: string
+): IFlatMenu<TMenuData> {
+ const menuItem = flatMenu.find(menuItem => menuItem.id === parent);
+
+ if (menuItem.parent === null) {
+ return [menuItem];
+ }
+
+ return [menuItem, ..._walkToRoot(flatMenu, menuItem.parent)];
+}
+export function walkToRoot<TMenuData = {}>(
+ menu: IMenu<TMenuData>,
+ value: string
+): IMenu<TMenuData> {
+ const flatMenu = toFlat(menu);
+ const menuItem = flatMenu.find(menuItem => menuItem.value === value);
+
+ return (menuItem.parent === null
+ ? [menuItem]
+ : [menuItem, ..._walkToRoot(flatMenu, menuItem.parent)]
+ ).map(flatMenuItem => _fromFlat(flatMenu, flatMenuItem));
+}
+
function _toFlat<TMenuData = {}>(
menuItem: IMenuItem<TMenuData>,
sort: number,
@@ -105,12 +130,11 @@ export function toFlat<TMenuData = {}>(
function _fromFlat<TMenuData = {}>(
menu: IFlatMenu<TMenuData>,
- id: string
+ flatMenuItem: IFlatMenuItem<TMenuData>
): IMenuItem<TMenuData> {
- const flatMenuItem = menu.find(menuItem => menuItem.id === id);
const children: Array<IMenuItem<TMenuData>> = menu
.filter(menuItem => menuItem.parent === flatMenuItem.id)
- .map(menuItem => _fromFlat(menu, menuItem.id));
+ .map(menuItem => _fromFlat(menu, menuItem));
return {
children,
@@ -124,5 +148,11 @@ export function fromFlat<TMenuData = {}>(
): IMenu<TMenuData> {
return menu
.filter(menuItem => menuItem.parent === null)
- .map(menuItem => _fromFlat(menu, menuItem.id));
+ .map(menuItem => _fromFlat(menu, menuItem));
+}
+
+export function isLeaf<TMenuData = {}>(
+ menuItem: IMenuItem<TMenuData>
+): boolean {
+ return menuItem.children.length === 0;
}