source: src/layouts/_common/searchbar/utils.ts@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 2.5 KB
Line 
1// utils
2import { flattenArray } from 'src/utils/flatten-array';
3// components
4import { NavListProps, NavSectionProps } from 'src/components/nav-section';
5
6// ----------------------------------------------------------------------
7
8type ItemProps = {
9 group: string;
10 title: string;
11 path: string;
12};
13
14export function getAllItems({ data }: NavSectionProps) {
15 const reduceItems = data.map((list) => handleLoop(list.items, list.subheader)).flat();
16
17 const items = flattenArray(reduceItems).map((option) => {
18 const group = splitPath(reduceItems, option.path);
19
20 return {
21 group: group && group.length > 1 ? group[0] : option.subheader,
22 title: option.title,
23 path: option.path,
24 };
25 });
26
27 return items;
28}
29
30// ----------------------------------------------------------------------
31
32type FilterProps = {
33 inputData: ItemProps[];
34 query: string;
35};
36
37export function applyFilter({ inputData, query }: FilterProps) {
38 if (query) {
39 inputData = inputData.filter(
40 (item) =>
41 item.title.toLowerCase().indexOf(query.toLowerCase()) !== -1 ||
42 item.path.toLowerCase().indexOf(query.toLowerCase()) !== -1
43 );
44 }
45
46 return inputData;
47}
48
49// ----------------------------------------------------------------------
50
51export function splitPath(array: NavListProps[], key: string) {
52 let stack = array.map((item) => ({
53 path: [item.title],
54 currItem: item,
55 }));
56
57 while (stack.length) {
58 const { path, currItem } = stack.pop() as {
59 path: string[];
60 currItem: NavListProps;
61 };
62
63 if (currItem.path === key) {
64 return path;
65 }
66
67 if (currItem.children?.length) {
68 stack = stack.concat(
69 currItem.children.map((item: NavListProps) => ({
70 path: path.concat(item.title),
71 currItem: item,
72 }))
73 );
74 }
75 }
76 return null;
77}
78
79// ----------------------------------------------------------------------
80
81export function handleLoop(array: any, subheader?: string) {
82 return array?.map((list: any) => ({
83 subheader,
84 ...list,
85 ...(list.children && {
86 children: handleLoop(list.children, subheader),
87 }),
88 }));
89}
90
91// ----------------------------------------------------------------------
92
93type GroupsProps = {
94 [key: string]: ItemProps[];
95};
96
97export function groupedData(array: ItemProps[]) {
98 const group = array.reduce((groups: GroupsProps, item) => {
99 groups[item.group] = groups[item.group] || [];
100
101 groups[item.group].push(item);
102
103 return groups;
104 }, {});
105
106 return group;
107}
Note: See TracBrowser for help on using the repository browser.