@alphakits/ui 2.4.2 → 2.4.3
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.
|
@@ -1,119 +1,123 @@
|
|
|
1
|
-
import { useState as u, useCallback as
|
|
2
|
-
import { addQueryParam as
|
|
3
|
-
import { getQueryFromUrl as
|
|
1
|
+
import { useState as u, useRef as b, useCallback as l, useEffect as E } from "react";
|
|
2
|
+
import { addQueryParam as h } from "../utils/add-query-param.js";
|
|
3
|
+
import { getQueryFromUrl as j } from "../utils/get-query-from-url.js";
|
|
4
4
|
import "query-string";
|
|
5
|
-
import { removeQueryParam as
|
|
6
|
-
import { resetQueryParam as
|
|
7
|
-
import { serializeData as
|
|
8
|
-
const
|
|
5
|
+
import { removeQueryParam as B } from "../utils/remove-query-param.js";
|
|
6
|
+
import { resetQueryParam as G } from "../utils/reset-query-param.js";
|
|
7
|
+
import { serializeData as C } from "../utils/serialize-data.js";
|
|
8
|
+
const p = {
|
|
9
9
|
page: 1,
|
|
10
10
|
limit: 10,
|
|
11
11
|
where: {}
|
|
12
12
|
// остальное добавится по мере нужды (searchText, sort, order и т.д.)
|
|
13
13
|
};
|
|
14
|
-
function
|
|
15
|
-
const r =
|
|
14
|
+
function L(w, a) {
|
|
15
|
+
const r = w ?? p, n = {
|
|
16
16
|
...r,
|
|
17
|
-
...
|
|
17
|
+
...a,
|
|
18
18
|
where: {
|
|
19
19
|
...r.where ?? {},
|
|
20
|
-
...
|
|
20
|
+
...a.where ?? {}
|
|
21
21
|
}
|
|
22
22
|
};
|
|
23
|
-
return
|
|
23
|
+
return n.page && (n.page = +n.page), n.limit && (n.limit = +n.limit), n;
|
|
24
24
|
}
|
|
25
|
-
function
|
|
26
|
-
const [
|
|
27
|
-
|
|
25
|
+
function X({ promise: w, records: a, fromPath: r = !0 }) {
|
|
26
|
+
const [n, A] = u([]), [S, d] = u(!0), [T, Q] = u(!1), [U, q] = u(0), [F, k] = u(1), [c, f] = u(void 0), x = b(w);
|
|
27
|
+
x.current = w;
|
|
28
|
+
const v = b(a);
|
|
29
|
+
v.current = a;
|
|
30
|
+
const m = l(
|
|
31
|
+
(t) => r ? j() : t ?? c ?? p,
|
|
28
32
|
// params не включаем при fromPath: иначе setParams на каждом fetch
|
|
29
33
|
// даёт новую ссылку → computeQuery/updateData меняются → бесконечный цикл.
|
|
30
|
-
r ? [r] : [r,
|
|
31
|
-
), s =
|
|
34
|
+
r ? [r] : [r, c]
|
|
35
|
+
), s = l(
|
|
32
36
|
async (t) => {
|
|
33
|
-
const e =
|
|
34
|
-
if (Array.isArray(
|
|
35
|
-
|
|
37
|
+
const e = m(t), i = v.current, y = x.current;
|
|
38
|
+
if (Array.isArray(i) && (d(!1), A(C(i, e))), r || f(e), y) {
|
|
39
|
+
Q(!1), d(!0);
|
|
36
40
|
try {
|
|
37
41
|
const {
|
|
38
|
-
data: { items:
|
|
39
|
-
} = await
|
|
40
|
-
|
|
42
|
+
data: { items: o, meta: g }
|
|
43
|
+
} = await y(e);
|
|
44
|
+
q(g.totalItems), k(g.totalPages), A(o || []);
|
|
41
45
|
} catch {
|
|
42
|
-
|
|
46
|
+
Q(!0);
|
|
43
47
|
} finally {
|
|
44
|
-
|
|
48
|
+
d(!1);
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
},
|
|
48
|
-
[
|
|
49
|
-
),
|
|
52
|
+
[m, r]
|
|
53
|
+
), z = l(
|
|
50
54
|
(t) => {
|
|
51
55
|
if (r) {
|
|
52
|
-
|
|
56
|
+
h(t), s();
|
|
53
57
|
return;
|
|
54
58
|
}
|
|
55
|
-
const e =
|
|
56
|
-
|
|
59
|
+
const e = L(c, t);
|
|
60
|
+
f(e), s(e);
|
|
57
61
|
},
|
|
58
|
-
[r,
|
|
59
|
-
),
|
|
62
|
+
[r, c, s]
|
|
63
|
+
), D = l(
|
|
60
64
|
(t, e, i) => {
|
|
61
|
-
const
|
|
62
|
-
...
|
|
63
|
-
where: { ...
|
|
64
|
-
},
|
|
65
|
-
if (Array.isArray(
|
|
66
|
-
const
|
|
67
|
-
|
|
65
|
+
const y = t ?? p, o = {
|
|
66
|
+
...y,
|
|
67
|
+
where: { ...y.where ?? {} }
|
|
68
|
+
}, g = o.where?.[e];
|
|
69
|
+
if (Array.isArray(g) && i != null) {
|
|
70
|
+
const R = g.filter((_) => String(_) !== String(i));
|
|
71
|
+
R.length ? o.where[e] = R : delete o.where[e];
|
|
68
72
|
} else
|
|
69
|
-
|
|
70
|
-
return
|
|
73
|
+
o.where && e in o.where && delete o.where[e];
|
|
74
|
+
return o;
|
|
71
75
|
},
|
|
72
76
|
[]
|
|
73
|
-
),
|
|
77
|
+
), I = l(
|
|
74
78
|
(t, e) => {
|
|
75
79
|
if (r) {
|
|
76
|
-
|
|
80
|
+
B(t, e), s();
|
|
77
81
|
return;
|
|
78
82
|
}
|
|
79
|
-
const i =
|
|
80
|
-
|
|
83
|
+
const i = D(c, t, e);
|
|
84
|
+
f(i), s(i);
|
|
81
85
|
},
|
|
82
|
-
[r,
|
|
83
|
-
),
|
|
86
|
+
[r, c, s, D]
|
|
87
|
+
), V = l(
|
|
84
88
|
(t) => {
|
|
85
89
|
if (r) {
|
|
86
|
-
|
|
90
|
+
G(t), s();
|
|
87
91
|
return;
|
|
88
92
|
}
|
|
89
|
-
const e =
|
|
90
|
-
|
|
93
|
+
const e = L(p, t);
|
|
94
|
+
f(e), s(e);
|
|
91
95
|
},
|
|
92
96
|
[r, s]
|
|
93
97
|
);
|
|
94
|
-
|
|
98
|
+
E(() => {
|
|
95
99
|
s();
|
|
96
|
-
}, [s]),
|
|
97
|
-
if (Array.isArray(
|
|
98
|
-
const t =
|
|
99
|
-
|
|
100
|
+
}, [s]), E(() => {
|
|
101
|
+
if (Array.isArray(a)) {
|
|
102
|
+
const t = m();
|
|
103
|
+
r || f(t), d(!1), A(C(a, t));
|
|
100
104
|
}
|
|
101
|
-
}, [
|
|
102
|
-
const
|
|
105
|
+
}, [a, m]);
|
|
106
|
+
const Y = c ?? m();
|
|
103
107
|
return {
|
|
104
|
-
loading:
|
|
105
|
-
error:
|
|
106
|
-
data:
|
|
107
|
-
total:
|
|
108
|
-
params:
|
|
109
|
-
pagesCount:
|
|
108
|
+
loading: S,
|
|
109
|
+
error: T,
|
|
110
|
+
data: n,
|
|
111
|
+
total: U,
|
|
112
|
+
params: Y,
|
|
113
|
+
pagesCount: F,
|
|
110
114
|
updateData: s,
|
|
111
115
|
// НОВОЕ: наружу отдаём универсальные методы для управления параметрами
|
|
112
|
-
addParam:
|
|
113
|
-
removeParam:
|
|
114
|
-
resetParams:
|
|
116
|
+
addParam: z,
|
|
117
|
+
removeParam: I,
|
|
118
|
+
resetParams: V
|
|
115
119
|
};
|
|
116
120
|
}
|
|
117
121
|
export {
|
|
118
|
-
|
|
122
|
+
X as useTableData
|
|
119
123
|
};
|