Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extract menu walk to own module
dominik-zeglencce4fbf8a2d3
diff --git a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx b/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsxindex e7a9687c4d..5024c47019 100644--- a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx+++ b/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.tsx@@ -13,14 +13,9 @@ import Downshift from "downshift";import * as React from "react";import i18n from "../../i18n";+import { getMenu, IMenu, validateMenuOptions } from "../../utils/menu";import Debounce, { DebounceProps } from "../Debounce";-export interface SelectMenuItem {- children?: SelectMenuItem[];- label: React.ReactNode;- value?: string;-}-export interface AutocompleteSelectMenuProps {disabled: boolean;displayValue: string;@@ -29,41 +24,16 @@ export interface AutocompleteSelectMenuProps {label: string;loading: boolean;name: string;- options: SelectMenuItem[];+ options: IMenu[];placeholder: string;onChange: (event: React.ChangeEvent<any>) => void;onInputChange?: (value: string) => void;}-function getOptionValues(option: SelectMenuItem): string[] {- return option.value- ? [option.value]- : option.children.reduce(- (acc, option) => [...acc, ...getOptionValues(option)],- []- );-}--export function validateOptions(options: SelectMenuItem[]): boolean {- const values: string[] = options.reduce(- (acc, option) => [...acc, ...getOptionValues(option)],- []- );- const uniqueValues = Array.from(new Set(values));- return uniqueValues.length === values.length;-}-const validationError: Error = new Error("Values supplied to AutocompleteSelectMenu should be unique");-function getMenu(options: SelectMenuItem[], path: number[]): SelectMenuItem[] {- if (path.length === 0) {- return options;- }- return getMenu(options[path[0]].children, path.slice(1));-}-const DebounceAutocomplete: React.ComponentType<DebounceProps<string>> = Debounce;@@ -119,7 +89,7 @@ const AutocompleteSelectMenu = withStyles(styles, {// Validate if option values are duplicatedReact.useEffect(() => {- if (!validateOptions(options)) {+ if (!validateMenuOptions(options)) {throw validationError;}}, []);diff --git a/saleor/static/dashboard-next/utils/menu/index.ts b/saleor/static/dashboard-next/utils/menu/index.tsnew file mode 100644index 0000000000..4fbc74d76e--- /dev/null+++ b/saleor/static/dashboard-next/utils/menu/index.ts@@ -0,0 +1 @@+export * from "./menu";diff --git a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.test.ts b/saleor/static/dashboard-next/utils/menu/menu.test.tssimilarity index 74%rename from saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.test.tsrename to saleor/static/dashboard-next/utils/menu/menu.test.tsindex 7c78d08eb9..68a69dc346 100644--- a/saleor/static/dashboard-next/components/AutocompleteSelectMenu/AutocompleteSelectMenu.test.ts+++ b/saleor/static/dashboard-next/utils/menu/menu.test.ts@@ -1,8 +1,8 @@-import { SelectMenuItem, validateOptions } from "./AutocompleteSelectMenu";+import { IMenu, validateMenuOptions } from "./menu";-describe("Validate Autocomplete Menu data structure", () => {+describe("Validate menu data structure", () => {it("Properly catches same level duplicated value", () => {- const menu: SelectMenuItem[] = [+ const menu: IMenu[] = [{label: "1",value: "1"@@ -17,11 +17,11 @@ describe("Validate Autocomplete Menu data structure", () => {}];- expect(validateOptions(menu)).toBeFalsy();+ expect(validateMenuOptions(menu)).toBeFalsy();});it("Properly catches multi level duplicated value", () => {- const menu: SelectMenuItem[] = [+ const menu: IMenu[] = [{label: "1",value: "1"@@ -49,11 +49,11 @@ describe("Validate Autocomplete Menu data structure", () => {}];- expect(validateOptions(menu)).toBeFalsy();+ expect(validateMenuOptions(menu)).toBeFalsy();});it("Properly passes valid structure", () => {- const menu: SelectMenuItem[] = [+ const menu: IMenu[] = [{label: "1",value: "1"@@ -81,6 +81,6 @@ describe("Validate Autocomplete Menu data structure", () => {}];- expect(validateOptions(menu)).toBeTruthy();+ expect(validateMenuOptions(menu)).toBeTruthy();});});diff --git a/saleor/static/dashboard-next/utils/menu/menu.ts b/saleor/static/dashboard-next/utils/menu/menu.tsnew file mode 100644index 0000000000..3973626e68--- /dev/null+++ b/saleor/static/dashboard-next/utils/menu/menu.ts@@ -0,0 +1,31 @@+export interface IMenu<TData = {}> {+ children?: Array<IMenu<TData>>;+ data?: TData;+ label: React.ReactNode;+ value?: string;+}++function getMenuOptionValues(menuOption: IMenu): string[] {+ return menuOption.value+ ? [menuOption.value]+ : menuOption.children.reduce(+ (acc, menuOption) => [...acc, ...getMenuOptionValues(menuOption)],+ []+ );+}++export function validateMenuOptions(menuOptions: IMenu[]): boolean {+ const values: string[] = menuOptions.reduce(+ (acc, menuOption) => [...acc, ...getMenuOptionValues(menuOption)],+ []+ );+ const uniqueValues = Array.from(new Set(values));+ return uniqueValues.length === values.length;+}++export function getMenu(menuOptions: IMenu[], path: number[]): IMenu[] {+ if (path.length === 0) {+ return menuOptions;+ }+ return getMenu(menuOptions[path[0]].children, path.slice(1));+}