antd-crud-table 0.0.11 → 0.0.13

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.
@@ -0,0 +1,195 @@
1
+ import { useRef as D, useState as U, useCallback as C } from "react";
2
+ import { s as y } from "./index-DNXRWKhU.js";
3
+ const $ = (m) => {
4
+ const { baseUrl: e = "", endpoints: i = {}, headers: S = {}, transform: t } = m.api, a = {
5
+ list: "/list",
6
+ create: "/create",
7
+ update: "/update",
8
+ delete: "/delete",
9
+ ...i
10
+ }, n = async (u, c = {}) => {
11
+ const h = await fetch(`${e}${u}`, {
12
+ headers: {
13
+ "Content-Type": "application/json",
14
+ ...S
15
+ },
16
+ ...c
17
+ });
18
+ if (!h.ok)
19
+ throw new Error(`HTTP error! status: ${h.status}`);
20
+ return h.json();
21
+ };
22
+ return {
23
+ getList: async (u) => {
24
+ const c = new URL(`${e}${a.list}`, window.location.origin);
25
+ Object.entries(u).forEach(([w, E]) => {
26
+ E != null && c.searchParams.append(w, String(E));
27
+ });
28
+ const h = await n(c.pathname + c.search);
29
+ return t != null && t.response ? t.response(h) : h;
30
+ },
31
+ create: async (u) => {
32
+ const c = t != null && t.request ? t.request(u) : u;
33
+ return n(a.create, {
34
+ method: "POST",
35
+ body: JSON.stringify(c)
36
+ });
37
+ },
38
+ update: async (u, c) => {
39
+ const h = t != null && t.request ? t.request(c) : c;
40
+ return n(`${a.update}/${u}`, {
41
+ method: "PUT",
42
+ body: JSON.stringify(h)
43
+ });
44
+ },
45
+ delete: async (u) => {
46
+ await n(`${a.delete}/${u}`, {
47
+ method: "DELETE"
48
+ });
49
+ }
50
+ };
51
+ }, f = (m, e) => {
52
+ let i = [...m], S = Math.max(...i.map((t) => Number(t[e]) || 0)) + 1;
53
+ return {
54
+ getList: async (t) => {
55
+ const { current: a = 1, pageSize: n = 10, sortBy: u, sortOrder: c, ...h } = t;
56
+ let w = [...i];
57
+ Object.entries(h).forEach(([s, l]) => {
58
+ l != null && l !== "" && (w = w.filter(
59
+ (r) => String(r[s]).toLowerCase().includes(String(l).toLowerCase())
60
+ ));
61
+ }), u && c && w.sort((s, l) => {
62
+ const r = s[u], o = l[u], p = r > o ? 1 : r < o ? -1 : 0;
63
+ return c === "ascend" ? p : -p;
64
+ });
65
+ const E = (a - 1) * n;
66
+ return {
67
+ data: w.slice(E, E + n),
68
+ total: w.length,
69
+ success: !0
70
+ };
71
+ },
72
+ create: async (t) => {
73
+ const a = {
74
+ [e]: S++,
75
+ ...t
76
+ };
77
+ return i.push(a), a;
78
+ },
79
+ update: async (t, a) => {
80
+ const n = i.findIndex((u) => u[e] === t);
81
+ if (n === -1) throw new Error("Item not found");
82
+ return i[n] = { ...i[n], ...a }, i[n];
83
+ },
84
+ delete: async (t) => {
85
+ const a = i.findIndex((n) => n[e] === t);
86
+ if (a === -1) throw new Error("Item not found");
87
+ i.splice(a, 1);
88
+ }
89
+ };
90
+ }, T = (m, e) => {
91
+ const i = D(null), [S, t] = U({
92
+ loading: !1,
93
+ error: null,
94
+ data: [],
95
+ total: 0,
96
+ current: 1,
97
+ pageSize: e.defaultPageSize || 10
98
+ }), a = (() => {
99
+ if ("operations" in e)
100
+ return e.operations;
101
+ if ("staticData" in e)
102
+ return f(e.staticData, m);
103
+ if ("api" in e)
104
+ return $(e);
105
+ throw new Error("useCrudTable: Must provide either staticData, api config, or custom operations");
106
+ })(), n = C(async () => {
107
+ var d;
108
+ t((s) => ({ ...s, loading: !0, error: null }));
109
+ try {
110
+ const s = {
111
+ current: S.current,
112
+ pageSize: S.pageSize
113
+ }, l = await a.getList(s);
114
+ t((r) => ({
115
+ ...r,
116
+ data: l.data,
117
+ total: l.total,
118
+ loading: !1
119
+ }));
120
+ } catch (s) {
121
+ const l = s instanceof Error ? s.message : "Failed to fetch data";
122
+ t((r) => ({ ...r, loading: !1, error: l })), (d = e.onError) == null || d.call(e, "fetch", s), y.error(l);
123
+ }
124
+ }, [S.current, S.pageSize, a, e]), u = C(async (d) => {
125
+ var s, l;
126
+ if (!a.create)
127
+ return y.error("Create operation not supported"), null;
128
+ try {
129
+ t((o) => ({ ...o, loading: !0 }));
130
+ const r = await a.create(d);
131
+ return e.optimisticUpdates ? t((o) => ({
132
+ ...o,
133
+ data: [...o.data, r],
134
+ total: o.total + 1,
135
+ loading: !1
136
+ })) : await n(), (s = e.onSuccess) == null || s.call(e, "create", r), y.success("Created successfully"), r;
137
+ } catch (r) {
138
+ t((p) => ({ ...p, loading: !1 }));
139
+ const o = r instanceof Error ? r.message : "Create failed";
140
+ return (l = e.onError) == null || l.call(e, "create", r), y.error(o), null;
141
+ }
142
+ }, [a, e, n]), c = C(async (d, s) => {
143
+ var l, r;
144
+ if (!a.update)
145
+ return y.error("Update operation not supported"), null;
146
+ try {
147
+ t((p) => ({ ...p, loading: !0 }));
148
+ const o = await a.update(d, s);
149
+ return e.optimisticUpdates ? t((p) => ({
150
+ ...p,
151
+ data: p.data.map(
152
+ (b) => b[m] === d ? { ...b, ...o } : b
153
+ ),
154
+ loading: !1
155
+ })) : await n(), (l = e.onSuccess) == null || l.call(e, "update", o), y.success("Updated successfully"), o;
156
+ } catch (o) {
157
+ t((b) => ({ ...b, loading: !1 }));
158
+ const p = o instanceof Error ? o.message : "Update failed";
159
+ return (r = e.onError) == null || r.call(e, "update", o), y.error(p), null;
160
+ }
161
+ }, [a, e, m, n]), h = C(async (d) => {
162
+ var s, l;
163
+ if (!a.delete)
164
+ return y.error("Delete operation not supported"), !1;
165
+ try {
166
+ return t((r) => ({ ...r, loading: !0 })), await a.delete(d), e.optimisticUpdates ? t((r) => ({
167
+ ...r,
168
+ data: r.data.filter((o) => o[m] !== d),
169
+ total: r.total - 1,
170
+ loading: !1
171
+ })) : await n(), (s = e.onSuccess) == null || s.call(e, "delete", d), y.success("Deleted successfully"), !0;
172
+ } catch (r) {
173
+ t((p) => ({ ...p, loading: !1 }));
174
+ const o = r instanceof Error ? r.message : "Delete failed";
175
+ return (l = e.onError) == null || l.call(e, "delete", r), y.error(o), !1;
176
+ }
177
+ }, [a, e, m, n]), w = C((d) => {
178
+ t((s) => ({ ...s, pageSize: d, current: 1 }));
179
+ }, []), E = C((d) => {
180
+ t((s) => ({ ...s, current: d }));
181
+ }, []);
182
+ return {
183
+ refresh: n,
184
+ create: u,
185
+ update: c,
186
+ delete: h,
187
+ setPageSize: w,
188
+ setCurrentPage: E,
189
+ state: S,
190
+ actionRef: i
191
+ };
192
+ };
193
+ export {
194
+ T as useCrudTable
195
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "antd-crud-table",
3
3
  "private": false,
4
- "version": "0.0.11",
4
+ "version": "0.0.13",
5
5
  "type": "module",
6
6
  "homepage": ".",
7
7
  "keywords": [