aio-entity 1.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +113 -48
  2. package/index.js +238 -216
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,34 +1,36 @@
1
- import { type ReactNode } from "react";
2
1
  type I_sort<T> = {
3
- active?: boolean;
4
- dir?: 'dec' | 'inc';
5
- title?: ReactNode;
6
- sortId?: string;
7
- getValue?: (row: T, dir: 'inc' | 'dec') => any;
8
- isDate?: boolean;
9
- columnId?: string;
10
- order?: number;
2
+ dir: 'dec' | 'inc';
3
+ getValue: (row: T, dir: 'inc' | 'dec') => any;
11
4
  };
12
5
  type I_detail = {
13
- _parentIds?: (string | number)[];
6
+ _parentIds: (string | number)[];
14
7
  _childsLength?: number;
15
8
  _isLeaf: boolean;
16
9
  };
17
10
  export type I_useEntity<T> = {
18
- getId: (row: T) => string | number;
19
- fetchData: () => Promise<T[] | false>;
20
- saveData: (data: T[]) => Promise<boolean>;
21
- initData?: T[];
11
+ data: {
12
+ get: () => Promise<T[] | false>;
13
+ set: (data: T[]) => Promise<boolean>;
14
+ };
22
15
  childs?: {
23
16
  get: (row: T) => (T[] | void);
24
17
  set: (row: T, childs: T[] | undefined) => T;
25
18
  };
26
- setId: (row: T) => T;
19
+ id: {
20
+ get: (row: T) => string | number;
21
+ set: (row: T) => T;
22
+ };
23
+ sorts?: I_sort<T>[];
24
+ onChange?: (data: T[]) => void;
27
25
  };
28
26
  export declare const useEntity: <T>(p: I_useEntity<T>) => {
27
+ methods: EntityClass<T>;
29
28
  data: T[];
30
29
  changeData: (data: T[]) => Promise<boolean>;
31
- getId: (row: T) => string | number;
30
+ id: {
31
+ get: (row: T) => string | number;
32
+ set: (row: T) => T;
33
+ };
32
34
  add: (row: T, parentId?: string | number) => Promise<number | string | false>;
33
35
  edit: (row: T) => Promise<boolean>;
34
36
  remove: (rowId: string | number) => Promise<boolean>;
@@ -38,46 +40,109 @@ export declare const useEntity: <T>(p: I_useEntity<T>) => {
38
40
  moveTo: (fromIds: (string | number)[], toId: string | number) => Promise<boolean>;
39
41
  editChilds: (rows: T[], callback: (row: T) => T) => Promise<boolean>;
40
42
  duplicate: (row: T) => Promise<string | number | false>;
41
- getLeafs: (rows?: T[]) => T[];
42
- sort: (sorts: I_sort<T>[], parentRow?: T) => Promise<boolean>;
43
- getNodes: <R>(p: {
43
+ filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
44
+ search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
45
+ getLeafs: (rows: T[]) => T[];
46
+ getSortedData: (rows: T[], sorts: I_sort<T>[]) => T[];
47
+ sort: (params: {
48
+ sorts: I_sort<T>[];
49
+ row?: T;
50
+ }) => Promise<false | T[]>;
51
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => false | R) => R[];
52
+ getRowAndParentsById: (rowId: string | number) => {
44
53
  row?: T;
45
- getValue: (row: T, detail: I_detail) => R | false | undefined;
46
- }) => R[];
47
- getRowAndParentsById: (p: {
48
- rowId: string | number;
49
- }) => {
50
- row?: T | undefined;
51
54
  parents: T[];
52
55
  };
53
- getChilds: (row: T) => void | T[];
54
- getReport: (p: {
55
- rows?: T[];
56
- getValue: (row: T, detail: I_detail) => {
57
- [key: string]: any[] | number;
58
- } | false | undefined;
59
- getGroup?: (row: T) => string[];
60
- }) => any;
61
- getParentByRowId: (p: {
62
- rowId: any;
63
- rows: T[];
64
- }) => T;
56
+ childs: {
57
+ get: (row: T) => void | T[];
58
+ set: (row: T, childs: T[]) => T;
59
+ } | {
60
+ get: () => any;
61
+ set: () => any;
62
+ };
63
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => {
64
+ [key: string]: any[] | number;
65
+ } | false | undefined, getGroup?: (row: T) => string[]) => any;
66
+ getParentByRowId: (data: T[], rowId: string | number) => T;
65
67
  };
66
- type I_useFilter<T, F> = {
67
- getFilteredRows: (rows: T[], filter: F) => {
68
- rows: T[];
69
- data: any;
68
+ type I_EntityClass<T> = {
69
+ childs?: {
70
+ get: (row: T) => (T[] | void);
71
+ set: (row: T, childs: T[] | undefined) => T;
72
+ };
73
+ id: {
74
+ get: (row: T) => string | number;
75
+ set: (row: T) => T;
70
76
  };
71
- getDefaultFilter: () => F;
72
- getRows: () => T[];
73
77
  };
74
- export declare const useFilter: <T, F>(p: I_useFilter<T, F>) => {
78
+ declare class EntityClass<T> {
79
+ params: I_EntityClass<T>;
80
+ constructor(p: I_EntityClass<T>);
81
+ private getRowAndParentsById_req;
82
+ private addToParent;
83
+ private editByParent;
84
+ private removeChild;
85
+ private walk;
86
+ private editChilds_req;
87
+ filterRows: (rows: T[], getResult: (row: T, detail: I_detail) => boolean, parentIds?: (string | number)[]) => T[];
88
+ updateReport: (values: any, groups: string[], dic: any) => void;
89
+ getReport: (rows: T[], getValue: (row: T, detail: I_detail) => {
90
+ [key: string]: any[] | number;
91
+ } | false | undefined, getGroup?: (row: T) => string[]) => any;
92
+ add: (data: T[], row: T, parentId?: string | number) => {
93
+ data: T[];
94
+ addedId?: string | number;
95
+ };
96
+ editRoot: (rows: T[], row: T) => T[];
97
+ edit: (data: T[], row: T) => T[];
98
+ remove: (data: T[], rowId: string | number) => T[];
99
+ removeByIds: (data: T[], rowIds: (string | number)[]) => T[];
100
+ mergeToExistRow: (data: T[], rowIds: (string | number)[], parentId: string | number) => T[];
101
+ mergeToNewRow: (data: T[], rowIds: (string | number)[], newRow: T, newRowParentId?: string | number) => T[];
102
+ getNodes: <R = any>(data: T[], getValue: (row: T, detail: I_detail) => R | false | undefined) => R[];
103
+ getChilds: (row: T) => void | T[];
104
+ getRowAndParentsById: (data: T[], rowId: string | number) => {
105
+ row?: T;
106
+ parents: T[];
107
+ };
108
+ getRowsByIds: (data: T[], rowIds: (string | number)[]) => T[];
109
+ getParentByRowId: (data: T[], rowId: string | number) => T | undefined;
110
+ private moveToUnit;
111
+ moveTo: (data: T[], fromIds: (string | number)[], toId: string | number) => T[];
112
+ editChilds: (rows: T[], callback: (row: T) => T) => T[];
113
+ search: (rows: T[], getResult: (row: T, detail: I_detail) => boolean) => T[];
114
+ duplicate: (data: T[], row: T) => {
115
+ data: T[];
116
+ addedId?: string | number;
117
+ };
118
+ getLeafs: (rows: T[]) => T[];
119
+ getSortedData: (rows: T[], sorts: I_sort<T>[]) => T[];
120
+ }
121
+ export declare const useFilter: <T, F>(p: {
122
+ getFilteredRows: (rows: T[], filter: F) => T[];
123
+ getDefaultFilter: () => F;
124
+ rows: T[];
125
+ getReport?: (data: T[], filter: F) => any;
126
+ defaultReport?: any;
127
+ }) => {
75
128
  filter: F;
76
129
  changeFilter: (p: Partial<F>) => void;
77
- filteredData: {
78
- rows: T[];
79
- data: any;
80
- };
130
+ data: T[];
81
131
  updateFilteredRows: (rows: T[], pFilter?: F) => void;
132
+ report: any;
133
+ };
134
+ export declare const useEntityFilter: <T, F>(p: {
135
+ entity: ReturnType<typeof useEntity<T>>;
136
+ defaultFilter: F;
137
+ getResult: (row: T, detail: I_detail, filter: F) => boolean;
138
+ getReport?: (row: T, detail: I_detail, filter: F) => false | undefined | {
139
+ [key: string]: any[] | number;
140
+ };
141
+ defaultReport?: any;
142
+ }) => {
143
+ filter: F;
144
+ changeFilter: (p: Partial<F>) => void;
145
+ data: T[];
146
+ report: any;
82
147
  };
83
148
  export {};
package/index.js CHANGED
@@ -8,77 +8,103 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { useEffect, useRef, useState } from "react";
11
- import { Debounce, SortArray } from '../../aio-utils';
11
+ import { Debounce, SortArray } from 'aio-utils';
12
12
  export const useEntity = (p) => {
13
- const entity = new EntityClass({
14
- getId: p.getId, setId: p.setId, childs: p.childs
15
- });
16
- const [data, setData] = useState(p.initData || []), dataRef = useRef(data);
13
+ const entity = new EntityClass({ id: p.id, childs: p.childs });
14
+ const [data, setData] = useState([]), dataRef = useRef(data);
17
15
  dataRef.current = data;
18
16
  const fetchData = () => __awaiter(void 0, void 0, void 0, function* () {
19
- const res = yield p.fetchData();
17
+ const res = yield p.data.get();
20
18
  if (res !== false) {
21
19
  setData(res);
22
20
  }
23
21
  });
24
22
  useEffect(() => { fetchData(); }, []);
25
23
  const changeData = (data) => __awaiter(void 0, void 0, void 0, function* () {
26
- const res = yield p.saveData(data);
24
+ const sortedData = p.sorts && p.sorts.length ? entity.getSortedData(data, p.sorts) : data;
25
+ const res = yield p.data.set(sortedData);
27
26
  if (res !== false) {
28
- dataRef.current = data;
29
- setData(data);
27
+ dataRef.current = sortedData;
28
+ if (p.onChange) {
29
+ p.onChange(sortedData);
30
+ }
31
+ setData(sortedData);
30
32
  }
31
33
  return res;
32
34
  });
33
35
  return {
34
- data, changeData, getId: p.getId,
36
+ methods: entity,
37
+ data, changeData, id: p.id,
35
38
  add: (row, parentId) => __awaiter(void 0, void 0, void 0, function* () {
36
- const { rows, addedId } = entity.add({ row, parentId, rows: dataRef.current });
39
+ const { data, addedId } = entity.add(dataRef.current, row, parentId);
37
40
  if (addedId === undefined) {
38
41
  return false;
39
42
  }
40
- const res = yield changeData(rows);
43
+ const res = yield changeData(data);
41
44
  if (!res) {
42
45
  return false;
43
46
  }
44
47
  return addedId;
45
48
  }),
46
- edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit({ row, rows: dataRef.current })); }),
47
- remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove({ rowId, rows: dataRef.current })); }),
48
- removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds({ rowIds, rows: dataRef.current })); }),
49
- mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow({ rowIds, rows: dataRef.current, parentId })); }),
50
- mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow({ rowIds, rows: dataRef.current, newRow, newRowParentId })); }),
51
- moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo({ fromIds, toId, rows: dataRef.current })); }),
52
- editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds({ rows, callback })); }),
49
+ edit: (row) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.edit(dataRef.current, row)); }),
50
+ remove: (rowId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.remove(dataRef.current, rowId)); }),
51
+ removeByIds: (rowIds) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.removeByIds(dataRef.current, rowIds)); }),
52
+ mergeToExistRow: (rowIds, parentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToExistRow(dataRef.current, rowIds, parentId)); }),
53
+ mergeToNewRow: (rowIds, newRow, newRowParentId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.mergeToNewRow(dataRef.current, rowIds, newRow, newRowParentId)); }),
54
+ moveTo: (fromIds, toId) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.moveTo(dataRef.current, fromIds, toId)); }),
55
+ editChilds: (rows, callback) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.editChilds(rows, callback)); }),
53
56
  duplicate: (row) => __awaiter(void 0, void 0, void 0, function* () {
54
- const { rows, addedId } = entity.duplicate({ rows: dataRef.current, row });
57
+ const { data, addedId } = entity.duplicate(dataRef.current, row);
55
58
  if (addedId === undefined) {
56
59
  return false;
57
60
  }
58
- const res = yield changeData(rows);
61
+ const res = yield changeData(data);
59
62
  if (!res) {
60
63
  return false;
61
64
  }
62
65
  return addedId;
63
66
  }),
64
- getLeafs: (rows) => entity.getLeafs(dataRef.current, rows),
65
- sort: (sorts, parentRow) => __awaiter(void 0, void 0, void 0, function* () { return yield changeData(entity.sort({ rows: dataRef.current, parentRow, sorts })); }),
66
- getNodes: (p) => entity.getNodes(Object.assign(Object.assign({}, p), { data: dataRef.current })),
67
- getRowAndParentsById: (p) => entity.getRowAndParentsById(Object.assign(Object.assign({}, p), { rows: dataRef.current })),
68
- getChilds: entity.getChilds,
69
- getReport: (p) => entity.getReport({ data: dataRef.current, rows: p.rows, getValue: p.getValue, getGroup: p.getGroup }),
67
+ filterRows: entity.filterRows,
68
+ search: entity.search,
69
+ getLeafs: (rows) => entity.getLeafs(rows),
70
+ getSortedData: entity.getSortedData,
71
+ sort: (params) => __awaiter(void 0, void 0, void 0, function* () {
72
+ const { childs: c } = p, { row, sorts } = params;
73
+ let newData = dataRef.current;
74
+ if (row) {
75
+ if (!c) {
76
+ return false;
77
+ }
78
+ const childs = c.get(row);
79
+ if (!childs || !childs.length) {
80
+ return false;
81
+ }
82
+ const newChilds = entity.getSortedData(childs, sorts);
83
+ const newRow = c.set(row, newChilds);
84
+ newData = entity.edit(dataRef.current, newRow);
85
+ }
86
+ else {
87
+ newData = entity.getSortedData(dataRef.current, sorts);
88
+ }
89
+ const res = yield changeData(newData);
90
+ return res ? newData : false;
91
+ }),
92
+ getNodes: entity.getNodes,
93
+ getRowAndParentsById: (rowId) => entity.getRowAndParentsById(dataRef.current, rowId),
94
+ childs: p.childs || { get: () => undefined, set: () => { return undefined; } },
95
+ getReport: (rows, getValue, getGroup) => entity.getReport(rows, getValue, getGroup),
70
96
  getParentByRowId: entity.getParentByRowId
71
97
  };
72
98
  };
73
99
  class EntityClass {
74
100
  constructor(p) {
75
- this.getRowAndParentsById_req = (rows, id, parents, result) => {
101
+ this.getRowAndParentsById_req = (data, id, parents, result) => {
76
102
  if (result.row) {
77
103
  return;
78
104
  }
79
- for (let i = 0; i < rows.length; i++) {
80
- const row = rows[i];
81
- if (this.params.getId(row) === id) {
105
+ for (let i = 0; i < data.length; i++) {
106
+ const row = data[i];
107
+ if (this.params.id.get(row) === id) {
82
108
  result.row = row;
83
109
  result.parents = parents;
84
110
  return;
@@ -89,74 +115,56 @@ class EntityClass {
89
115
  }
90
116
  }
91
117
  };
92
- this.addToParent = (p) => {
118
+ this.addToParent = (parentRow, row) => {
93
119
  const { childs: c } = this.params;
94
- const { parentRow, row } = p;
95
120
  if (!c) {
96
121
  return row;
97
122
  }
98
- const childs = c.get(parentRow) || [];
123
+ const childs = c.get(parentRow);
99
124
  if (!childs) {
100
125
  return row;
101
126
  }
102
127
  return c.set(parentRow, [...childs, row]) || row;
103
128
  };
104
- this.editByParent = (p) => {
105
- const { parentRow, row } = p;
106
- const { childs: c, getId } = this.params;
129
+ this.editByParent = (parentRow, row) => {
130
+ const { childs: c, id } = this.params;
107
131
  if (!c) {
108
132
  return parentRow;
109
133
  }
110
- const rowId = getId(row);
111
- const childs = c.get(parentRow);
134
+ const rowId = id.get(row), childs = c.get(parentRow);
112
135
  if (!childs) {
113
136
  return parentRow;
114
137
  }
115
- const newChilds = childs.map((o) => getId(o) === rowId ? row : o);
138
+ const newChilds = childs.map((o) => id.get(o) === rowId ? row : o);
116
139
  return c.set(parentRow, newChilds) || parentRow;
117
140
  };
118
- this.removeChild = (p) => {
119
- const { parentRow, rowId, rows } = p;
120
- const { childs: c, getId } = this.params;
141
+ this.removeChild = (data, parentRow, rowId) => {
142
+ const { childs: c, id } = this.params;
121
143
  if (!c) {
122
- return rows;
144
+ return data;
123
145
  }
124
146
  let parentChilds = c.get(parentRow);
125
147
  if (!parentChilds || !parentChilds.length) {
126
- return rows;
148
+ return data;
127
149
  }
128
- parentChilds = parentChilds.filter((o) => getId(o) !== rowId);
150
+ parentChilds = parentChilds.filter((o) => id.get(o) !== rowId);
129
151
  const res = c.set(parentRow, parentChilds);
130
152
  if (!res) {
131
- return rows;
153
+ return data;
132
154
  }
133
- return this.edit({ row: res, rows });
134
- };
135
- this.getLeafs = (data, rows) => {
136
- const res = this.getReport({
137
- rows, data,
138
- getValue: (o, detail) => detail._isLeaf ? { rows: [o] } : undefined,
139
- });
140
- return res.rows;
141
- };
142
- this.getDetail = (row) => {
143
- const { childs: c } = this.params;
144
- if (!c) {
145
- return { _parentIds: [], _childsLength: undefined, _isLeaf: false };
146
- }
147
- const childs = c.get(row);
148
- const childsLength = childs ? childs.length : undefined;
149
- return { _parentIds: [], _childsLength: childsLength, _isLeaf: childsLength === undefined };
155
+ return this.edit(data, res);
150
156
  };
151
157
  this.walk = (p) => {
152
158
  const { childs: c } = this.params;
153
- if (!p.childs || !c) {
159
+ if (!c) {
154
160
  return;
155
161
  }
156
162
  for (let i = 0; i < p.childs.length; i++) {
157
163
  const row = p.childs[i];
158
- const rowId = this.params.getId(row);
159
- const detail = this.getDetail(row);
164
+ const rowId = this.params.id.get(row);
165
+ const childs = c.get(row);
166
+ const childsLength = childs ? childs.length : undefined;
167
+ const detail = { _childsLength: childsLength, _isLeaf: childsLength === undefined, _parentIds: p.parentIds };
160
168
  const mapped = p.getValue(row, detail);
161
169
  if (mapped === false) {
162
170
  continue;
@@ -170,37 +178,14 @@ class EntityClass {
170
178
  }
171
179
  }
172
180
  };
173
- this.sort_req = (p) => {
174
- const { childs: c } = this.params;
175
- const { rows, sorts, result, parentRow } = p;
176
- let sortedChilds = [];
177
- if (!parentRow) {
178
- sortedChilds = SortArray(rows, sorts);
179
- result.rows = sortedChilds;
180
- }
181
- else {
182
- if (!c) {
183
- return;
184
- }
185
- const childs = this.getChilds(parentRow);
186
- if (childs && childs.length) {
187
- sortedChilds = SortArray(childs, p.sorts);
188
- const fixedParent = c.set(parentRow, sortedChilds);
189
- p.result.rows = this.edit({ row: fixedParent, rows: p.result.rows });
190
- }
191
- }
192
- for (let i = 0; i < sortedChilds.length; i++) {
193
- this.sort_req({ parentRow: sortedChilds[i], sorts, result, rows });
194
- }
195
- };
196
- this.editChilds_req = (rows, callback, result) => {
181
+ this.editChilds_req = (data, callback, result) => {
197
182
  const { childs: c } = this.params;
198
183
  if (!c) {
199
184
  return [];
200
185
  }
201
- for (let i = 0; i < rows.length; i++) {
202
- const row = rows[i];
203
- result.rows = this.edit({ row: callback(row), rows: result.rows });
186
+ for (let i = 0; i < data.length; i++) {
187
+ const row = data[i];
188
+ result.rows = this.edit(result.rows, callback(row));
204
189
  const childs = c.get(row);
205
190
  if (!childs || !childs.length) {
206
191
  continue;
@@ -208,13 +193,21 @@ class EntityClass {
208
193
  this.editChilds_req(childs, callback, result);
209
194
  }
210
195
  };
211
- this.filterRows = (rows, getResult) => {
212
- const { childs: c } = this.params;
213
- const res = rows.filter((o) => getResult(o));
196
+ this.filterRows = (rows, getResult, parentIds) => {
197
+ const { childs: c, id } = this.params;
198
+ if (!c) {
199
+ return rows.filter((o) => getResult(o, { _parentIds: [], _childsLength: undefined, _isLeaf: false }));
200
+ }
201
+ const res = rows.filter((o) => {
202
+ const childs = c.get(o);
203
+ const childsLength = childs ? childs.length : undefined;
204
+ return getResult(o, { _childsLength: childsLength, _isLeaf: childsLength === undefined, _parentIds: parentIds || [] });
205
+ });
214
206
  return res.map((row) => {
215
207
  const childs = c ? c.get(row) : undefined;
216
208
  if (childs && c) {
217
- const newChilds = this.filterRows(childs, getResult);
209
+ const rowId = id.get(row);
210
+ const newChilds = this.filterRows(childs, getResult, (parentIds || []).concat(rowId));
218
211
  return c.set(row, newChilds);
219
212
  }
220
213
  else {
@@ -247,96 +240,73 @@ class EntityClass {
247
240
  }
248
241
  }
249
242
  };
250
- this.getReport = (p) => {
243
+ this.getReport = (rows, getValue, getGroup) => {
251
244
  const dic = {};
252
- const rows = p.rows || p.data;
253
- for (let i = 0; i < rows.length; i++) {
254
- this.getNodes({
255
- data: p.data, row: rows[i], getValue: (o) => {
256
- const detail = this.getDetail(o);
257
- const values = p.getValue(o, detail);
258
- if (values === false || values === undefined) {
259
- return values;
260
- }
261
- const groups = p.getGroup ? p.getGroup(o) : [];
262
- this.updateReport(values, groups, dic);
263
- }
264
- });
265
- }
245
+ this.getNodes(rows, (o, detail) => {
246
+ const values = getValue(o, detail);
247
+ if (values === false || values === undefined) {
248
+ return values;
249
+ }
250
+ const groups = getGroup ? getGroup(o) : [];
251
+ this.updateReport(values, groups, dic);
252
+ });
266
253
  return dic;
267
254
  };
268
- this.add = (p) => {
269
- const { rows, row, parentId } = p;
270
- const newRow = this.params.setId(row);
271
- const addedId = this.params.getId(newRow);
255
+ this.add = (data, row, parentId) => {
256
+ const newRow = this.params.id.set(row);
257
+ const addedId = this.params.id.get(newRow);
272
258
  if (parentId !== undefined) {
273
- const res = this.getRowAndParentsById({ rowId: parentId, rows });
259
+ const res = this.getRowAndParentsById(data, parentId);
274
260
  if (!res.row) {
275
- return { rows };
261
+ return { data };
276
262
  }
277
- const res1 = this.addToParent({ parentRow: res.row, row: newRow });
278
- const newRows = this.edit({ row: res1, rows });
279
- return { rows: newRows, addedId };
263
+ const res1 = this.addToParent(res.row, newRow);
264
+ const newData = this.edit(data, res1);
265
+ return { data: newData, addedId };
280
266
  }
281
267
  else {
282
- const newRows = [...rows, newRow];
283
- return { rows: newRows, addedId };
268
+ return { data: [...data, newRow], addedId };
284
269
  }
285
270
  };
286
- this.editRoot = (rows, row) => {
287
- const { getId } = this.params;
288
- return rows.map((o) => getId(o) === getId(row) ? row : o);
289
- };
290
- this.edit = (p) => {
271
+ this.editRoot = (rows, row) => rows.map((o) => this.params.id.get(o) === this.params.id.get(row) ? row : o);
272
+ this.edit = (data, row) => {
291
273
  const { childs: c } = this.params;
292
- const { row, rows } = p;
293
- const rowId = this.params.getId(row);
274
+ const rowId = this.params.id.get(row);
294
275
  if (!!c) {
295
- let parentRow = this.getParentByRowId({ rowId, rows });
296
- if (parentRow) {
297
- parentRow = this.editByParent({ parentRow, row });
298
- return this.edit({ row: parentRow, rows: p.rows });
299
- }
300
- else {
301
- const res = this.editRoot(rows, row);
302
- console.log(res);
303
- return res;
304
- }
276
+ let parentRow = this.getParentByRowId(data, rowId);
277
+ return parentRow ? this.edit(data, this.editByParent(parentRow, row)) : this.editRoot(data, row);
305
278
  }
306
279
  else {
307
- return this.editRoot(rows, row);
280
+ return this.editRoot(data, row);
308
281
  }
309
282
  };
310
- this.remove = (p) => {
311
- const { getId } = this.params;
312
- const { rowId, rows } = p;
313
- const parentResult = this.getParentByRowId({ rowId, rows });
283
+ this.remove = (data, rowId) => {
284
+ const { id } = this.params;
285
+ const parentResult = this.getParentByRowId(data, rowId);
314
286
  if (parentResult) {
315
- return this.removeChild({ parentRow: parentResult, rowId, rows });
287
+ return this.removeChild(data, parentResult, rowId);
316
288
  }
317
289
  else {
318
- return rows.filter((o) => getId(o) !== rowId);
290
+ return data.filter((o) => id.get(o) !== rowId);
319
291
  }
320
292
  };
321
- this.removeByIds = (p) => {
322
- const { rowIds, rows } = p;
323
- let tempRows = rows;
293
+ this.removeByIds = (data, rowIds) => {
294
+ let tempRows = [...data];
324
295
  for (let i = 0; i < rowIds.length; i++) {
325
- const rowId = rowIds[i];
326
- tempRows = this.remove({ rowId, rows: tempRows });
296
+ tempRows = this.remove(tempRows, rowIds[i]);
327
297
  }
328
298
  return tempRows;
329
299
  };
330
- this.mergeToExistRow = (p) => {
331
- let rows = p.rows;
300
+ this.mergeToExistRow = (data, rowIds, parentId) => {
301
+ let rows = data;
332
302
  if (!this.params.childs) {
333
- return rows;
303
+ return data;
334
304
  }
335
- const { row: parentRow } = this.getRowAndParentsById({ rows: p.rows, rowId: p.parentId });
305
+ const { row: parentRow } = this.getRowAndParentsById(data, parentId);
336
306
  if (!parentRow) {
337
307
  return rows;
338
308
  }
339
- const newChilds = this.getRowsByIds({ rows: p.rows, rowIds: p.rowIds });
309
+ const newChilds = this.getRowsByIds(data, rowIds);
340
310
  if (!newChilds.length) {
341
311
  return rows;
342
312
  }
@@ -344,46 +314,43 @@ class EntityClass {
344
314
  if (!parentChilds) {
345
315
  return rows;
346
316
  }
347
- rows = this.removeByIds({ rows, rowIds: p.rowIds });
317
+ rows = this.removeByIds(rows, rowIds);
348
318
  const newParentChilds = [...parentChilds, ...newChilds];
349
319
  const newParentRow = this.params.childs.set(parentRow, newParentChilds);
350
- return this.edit({ rows, row: newParentRow });
320
+ return this.edit(rows, newParentRow);
351
321
  };
352
- this.mergeToNewRow = (p) => {
353
- const { rows, addedId } = this.add({ rows: p.rows, row: p.newRow, parentId: p.newRowParentId });
354
- if (addedId === undefined) {
355
- return rows;
356
- }
357
- return this.mergeToExistRow({ rows, rowIds: p.rowIds, parentId: addedId });
322
+ this.mergeToNewRow = (data, rowIds, newRow, newRowParentId) => {
323
+ const addRes = this.add(data, newRow, newRowParentId);
324
+ return addRes.addedId === undefined ? data : this.mergeToExistRow(data, rowIds, addRes.addedId);
358
325
  };
359
- this.getNodes = (params) => {
326
+ this.getNodes = (data, getValue) => {
360
327
  const result = [];
361
- this.walk({ childs: params.row ? [params.row] : params.data, getValue: params.getValue, result, parentIds: [] });
328
+ this.walk({ childs: data, getValue, result, parentIds: [] });
362
329
  return result;
363
330
  };
364
331
  this.getChilds = (row) => !this.params.childs ? undefined : this.params.childs.get(row);
365
- this.getRowAndParentsById = (p) => {
332
+ this.getRowAndParentsById = (data, rowId) => {
366
333
  let result = { parents: [] };
367
- this.getRowAndParentsById_req(p.rows, p.rowId, [], result);
334
+ this.getRowAndParentsById_req(data, rowId, [], result);
368
335
  return result;
369
336
  };
370
- this.getRowsByIds = (p) => {
337
+ this.getRowsByIds = (data, rowIds) => {
371
338
  const res = [];
372
339
  const usedIds = [];
373
- for (let i = 0; i < p.rowIds.length; i++) {
374
- const id = p.rowIds[i];
340
+ for (let i = 0; i < rowIds.length; i++) {
341
+ const id = rowIds[i];
375
342
  if (usedIds.indexOf(id) !== -1) {
376
343
  continue;
377
344
  }
378
- const { row } = this.getRowAndParentsById({ rowId: id, rows: p.rows });
345
+ const { row } = this.getRowAndParentsById(data, id);
379
346
  if (row) {
380
347
  res.push(row);
381
348
  }
382
349
  }
383
350
  return res;
384
351
  };
385
- this.getParentByRowId = (p) => {
386
- const res = this.getRowAndParentsById(p);
352
+ this.getParentByRowId = (data, rowId) => {
353
+ const res = this.getRowAndParentsById(data, rowId);
387
354
  if (!res.row) {
388
355
  return;
389
356
  }
@@ -393,56 +360,78 @@ class EntityClass {
393
360
  }
394
361
  return parents[parents.length - 1];
395
362
  };
396
- this.moveToUnit = (p) => {
397
- const { fromId, toId, rows } = p;
398
- let { row: toRow } = this.getRowAndParentsById({ rowId: toId, rows });
363
+ this.moveToUnit = (data, fromId, toId) => {
364
+ let { row: toRow } = this.getRowAndParentsById(data, toId);
399
365
  if (!toRow) {
400
- return p.rows;
366
+ return data;
401
367
  }
402
- let { row: fromRow } = this.getRowAndParentsById({ rowId: fromId, rows });
368
+ let { row: fromRow } = this.getRowAndParentsById(data, fromId);
403
369
  if (!fromRow) {
404
- return p.rows;
370
+ return data;
405
371
  }
406
- const rowsAfterRemove = this.remove({ rowId: fromId, rows });
372
+ const rowsAfterRemove = this.remove(data, fromId);
407
373
  //اگر بروز نشه مشکل بوجود میاد
408
- toRow = this.getRowAndParentsById({ rowId: toId, rows: rowsAfterRemove }).row;
374
+ toRow = this.getRowAndParentsById(rowsAfterRemove, toId).row;
409
375
  if (!toRow) {
410
- return p.rows;
376
+ return data;
411
377
  }
412
- const newToRow = this.addToParent({ parentRow: toRow, row: fromRow });
413
- const res = this.edit({ row: newToRow, rows: rowsAfterRemove });
378
+ const newToRow = this.addToParent(toRow, fromRow);
379
+ const res = this.edit(rowsAfterRemove, newToRow);
414
380
  return res;
415
381
  };
416
- this.moveTo = (p) => {
417
- const { fromIds, toId } = p;
418
- let rows = p.rows;
419
- for (let i = 0; i < p.fromIds.length; i++) {
420
- const fromId = fromIds[i];
421
- rows = this.moveToUnit({ fromId, toId, rows });
382
+ this.moveTo = (data, fromIds, toId) => {
383
+ let rows = data;
384
+ for (let i = 0; i < fromIds.length; i++) {
385
+ rows = this.moveToUnit(rows, fromIds[i], toId);
422
386
  }
423
387
  return rows;
424
388
  };
425
- this.editChilds = (p) => {
426
- const { rows, callback } = p;
389
+ this.editChilds = (rows, callback) => {
427
390
  const result = { rows };
428
- this.editChilds_req(rows ? [...rows] : rows, callback, result);
391
+ this.editChilds_req(rows, callback, result);
429
392
  return result.rows;
430
393
  };
431
- this.duplicate = (p) => {
432
- const { getId, setId } = this.params;
433
- const { rows, row } = p;
434
- const rowId = getId(row);
435
- const parent = this.getParentByRowId({ rowId, rows });
436
- const parentId = parent ? getId(parent) : undefined;
437
- const newRow = setId(row);
438
- return this.add({ row: newRow, parentId, rows });
394
+ this.search = (rows, getResult) => {
395
+ const { id } = this.params;
396
+ const dic = {};
397
+ this.getNodes(rows, (row, detail) => {
398
+ const result = getResult(row, detail);
399
+ if (!result) {
400
+ return;
401
+ }
402
+ const ids = [...detail._parentIds, id.get(row)];
403
+ for (let i = 0; i < ids.length; i++) {
404
+ dic['a' + ids[i]] = true;
405
+ }
406
+ });
407
+ return this.filterRows(rows, (row) => !!dic['a' + id.get(row)]);
439
408
  };
440
- this.sort = (p) => {
441
- const fixedSorts = p.sorts.map((sort) => ({ getValue: (row) => row[sort.columnId], dir: sort.dir }));
442
- const result = { rows: p.rows };
443
- const { rows, parentRow } = p;
444
- this.sort_req({ sorts: fixedSorts, parentRow, result, rows });
445
- return result.rows;
409
+ this.duplicate = (data, row) => {
410
+ const { id } = this.params;
411
+ const rowId = id.get(row);
412
+ const parent = this.getParentByRowId(data, rowId);
413
+ const parentId = parent ? id.get(parent) : undefined;
414
+ const newRow = id.set(row);
415
+ return this.add(data, newRow, parentId);
416
+ };
417
+ this.getLeafs = (rows) => {
418
+ const res = this.getReport(rows, (o, detail) => detail._isLeaf ? { rows: [o] } : undefined);
419
+ return res.rows || [];
420
+ };
421
+ this.getSortedData = (rows, sorts) => {
422
+ const { childs: c } = this.params;
423
+ let sortedRows = SortArray(rows, sorts);
424
+ return sortedRows.map((o) => {
425
+ if (!c) {
426
+ return o;
427
+ }
428
+ const childs = c.get(o);
429
+ if (!childs || !childs.length) {
430
+ return o;
431
+ }
432
+ const newChilds = this.getSortedData(childs, sorts);
433
+ return c.set(Object.assign({}, o), newChilds);
434
+ });
446
435
  };
447
436
  this.params = p;
448
437
  }
@@ -451,17 +440,50 @@ export const useFilter = (p) => {
451
440
  const [filter, setFilter] = useState(p.getDefaultFilter);
452
441
  const filterRef = useRef(filter);
453
442
  filterRef.current = filter;
454
- const [filteredData, setFilteredData] = useState({ rows: [], data: {} });
455
- const filteredDataRef = useRef(filteredData);
456
- filteredDataRef.current = filteredData;
443
+ const [data, setData] = useState([]);
444
+ const [report, setReport] = useState(p.defaultReport);
445
+ const filteredDataRef = useRef([]);
446
+ filteredDataRef.current = data;
457
447
  const changeFilter = (p) => setFilter(Object.assign(Object.assign({}, filter), p));
458
448
  const updateFilteredRows = (rows, pFilter) => {
459
449
  Debounce(() => {
460
450
  const filter = pFilter || filterRef.current;
461
451
  const filteredData = p.getFilteredRows(rows, filter);
462
- setFilteredData(filteredData);
452
+ setData(filteredData);
453
+ if (p.getReport) {
454
+ const rep = p.getReport(rows, filter);
455
+ setReport(rep);
456
+ }
457
+ filteredDataRef.current = filteredData;
463
458
  }, 50);
464
459
  };
465
- useEffect(() => { updateFilteredRows(p.getRows()); }, [filter]);
466
- return { filter, changeFilter, filteredData, updateFilteredRows };
460
+ useEffect(() => { updateFilteredRows(p.rows); }, [filter, p.rows]);
461
+ return { filter, changeFilter, data: filteredDataRef.current, updateFilteredRows, report };
462
+ };
463
+ export const useEntityFilter = (p) => {
464
+ const [filter, setFilter] = useState(p.defaultFilter);
465
+ const filterRef = useRef(filter);
466
+ filterRef.current = filter;
467
+ const [data, setData] = useState([]);
468
+ const [report, setReport] = useState(p.defaultReport);
469
+ const filteredDataRef = useRef([]);
470
+ filteredDataRef.current = data;
471
+ const changeFilter = (p) => setFilter(Object.assign(Object.assign({}, filter), p));
472
+ const updateFilteredRows = (rows) => {
473
+ Debounce(() => {
474
+ const res = p.entity.search(rows, (row, detail) => p.getResult(row, detail, filter));
475
+ setData(res);
476
+ updateReport(res);
477
+ filteredDataRef.current = res;
478
+ }, 50);
479
+ };
480
+ const updateReport = (data) => {
481
+ if (!p.getReport) {
482
+ return;
483
+ }
484
+ const res = p.entity.getReport(data, (row, detail) => p.getReport(row, detail, filter));
485
+ setReport(res);
486
+ };
487
+ useEffect(() => { updateFilteredRows(p.entity.data); }, [filter, p.entity.data]);
488
+ return { filter, changeFilter, data, report };
467
489
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-entity",
3
- "version": "1.0.4",
3
+ "version": "2.0.0",
4
4
  "description": "kit",
5
5
  "main": "index.js",
6
6
  "scripts": {