@kingteza/crud-component 1.22.0 → 1.22.2
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,2 +1,2 @@
|
|
|
1
|
-
"use strict";const
|
|
2
|
-
`,t=new Blob([e],{type:"text/csv;charset=utf-8;"});
|
|
1
|
+
"use strict";const s=require("react/jsx-runtime"),T=require("@ant-design/icons"),O=require("papaparse"),f=require("antd"),L=require("file-saver"),n=require("react"),R=require("../../locale/index.cjs.js"),z=require("../view/CrudViewer.cjs.js"),$=require("../../util/DateUtil.cjs.js"),H=require("../../common/button/Button.cjs.js"),U=require("../../common/button/ImportButton.cjs.js");function Y({onCloseMethod:w,open:y,fields:q,importProps:a}){const[c,g]=n.useState([]),[C,k]=n.useState(!1),[D,S]=n.useState(0);n.useEffect(()=>{y||(g([]),k(!1))},[y]);const p=n.useMemo(()=>new Map(q.filter(e=>{var t;return!e.hidden&&!((t=e.importProps)!=null&&t.hidden)&&!e.readonly}).map(e=>[e.name,e])),[q]),A=n.useMemo(()=>Array.from(p.values()).flatMap(e=>{var t;return[e.name,...((t=e.importProps)==null?void 0:t.extraFields)??[]]}),[p]),M=n.useCallback(async function(){const e=`${A.join(",")}
|
|
2
|
+
`,t=new Blob([e],{type:"text/csv;charset=utf-8;"});L.saveAs(t,(a==null?void 0:a.name)+" - "+$.formatDateTimeWithSecond(new Date).replaceAll(":","-")+".csv")},[A,a==null?void 0:a.name]),{t:i}=R.useTranslationLib(),_=n.useCallback(async e=>{const t=new FileReader;t.onerror=console.error,t.onload=async r=>{var o;const d=(o=r==null?void 0:r.target)==null?void 0:o.result,l=O.parse(d,{header:!0});g(l.data.map((u,m)=>({...u,id_private_private:"crud-import-"+m})))},t.readAsText(e)},[]),B=n.useCallback(async e=>{console.log("Deleting item from import component",e,c),g(t=>t.filter(r=>r.id_private_private!==e))},[c]),j=n.useMemo(()=>{var t,r,d;let e=!1;for(const l of c)for(const[o,u]of p.entries()){const m=l[o],h=m||!isNaN(m);if(u.required&&!h)if((r=(t=u.importProps)==null?void 0:t.extraFields)!=null&&r.length)for(const v of((d=u.importProps)==null?void 0:d.extraFields)??[])if(l[v]||!isNaN(l[v])){e=!1;break}else return!0;else return!0}return e},[c,p]),x=n.useCallback(e=>(t,r)=>{var l,o,u;const d=t||!isNaN(t);if(e.required&&!d){let m=i("err.validation.required");if((o=(l=e.importProps)==null?void 0:l.extraFields)!=null&&o.length){const h=(u=e.importProps)==null?void 0:u.extraFields;(h==null?void 0:h.find(E=>!r[E]))&&(m="Either one of these fields is required: "+[e.name,...h].join(", "))}return s.jsxs(f.Tooltip,{className:"d-flex",title:m,children:[s.jsx(T.WarningTwoTone,{twoToneColor:"#ee9702"}),t]})}return t},[i]),I=n.useCallback(async e=>{S(e)},[]),b=n.useCallback(async()=>{try{k(!0),await a.onClickImport(c,I),w(!1)}finally{k(!1),S(0)}},[c,a,w,I]),{modal:F}=f.App.useApp(),V=n.useCallback(async()=>{j?F.warning?F.warning({title:i("str.warning"),content:i("qus.importWithIssues"),okText:i("str.import"),onOk:b,okCancel:!0,closable:!0}):(console.error("You must wrap your react app with App component. https://ant.design/components/app"),window.confirm(i("qus.importWithIssues"))&&b()):b()},[j,b,i]),N=n.useMemo(()=>Array.from(p.values()).flatMap(e=>{var t,r,d,l;return e.type==="text"?[{...e,label:e.name,render:x(e)},...((r=(t=e.importProps)==null?void 0:t.extraFields)==null?void 0:r.map((o,u)=>({...e,name:o,label:o,render:x(e)})))??[]]:e.type==="select"?[{...e,label:e.name,type:"text",render:x(e)},...((l=(d=e.importProps)==null?void 0:d.extraFields)==null?void 0:l.map(o=>({...e,name:o,label:o,type:"text",render:x(e)})))??[]]:[{...e,label:e.name,render:x(e)}]}),[p,x]),W=n.useMemo(()=>N.map(e=>({...e,hideInTable:!1})),[N]);return s.jsxs(f.Modal,{title:[i("str.import"),a==null?void 0:a.name].filter(Boolean).join(" "),width:"100%",open:y,onOk:V,destroyOnHidden:!0,onCancel:()=>w(!1),okText:i("str.import"),confirmLoading:C,okButtonProps:{disabled:!(c!=null&&c.length)},footer:(e,{OkBtn:t,CancelBtn:r})=>s.jsxs(f.Space,{children:[s.jsx(r,{}),s.jsx(t,{}),j&&s.jsx(f.Tooltip,{className:"d-flex",title:"Some fields have issue. Please review before submit.",children:s.jsx(T.WarningTwoTone,{twoToneColor:"#ee9702"})})]}),children:[s.jsxs(f.Space,{children:[s.jsx(H,{onClick:M,icon:s.jsx(T.DownloadOutlined,{}),children:i("str.downloadCsvTemplate")}),s.jsx(U.ImportButton,{disabled:C,type:"default",onClick:_,accept:".csv",children:i("str.importCsvFile")})]}),s.jsx(f.Spin,{spinning:C,indicator:D?s.jsx(f.Progress,{type:"circle",percent:D*100,size:50,format:e=>`${parseInt(e)}%`}):void 0,children:s.jsx(z,{data:c,bordered:!0,size:"small",idField:"id_private_private",onDelete:B,confirmDeleting:!1,fields:W})})]})}module.exports=Y;
|
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { jsxs as
|
|
2
|
-
import { WarningTwoTone as
|
|
3
|
-
import
|
|
4
|
-
import { Tooltip as
|
|
5
|
-
import { saveAs as
|
|
6
|
-
import { useState as
|
|
7
|
-
import { useTranslationLib as
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import
|
|
11
|
-
import { ImportButton as
|
|
12
|
-
function
|
|
13
|
-
onCloseMethod:
|
|
1
|
+
import { jsxs as b, jsx as i } from "react/jsx-runtime";
|
|
2
|
+
import { WarningTwoTone as B, DownloadOutlined as H } from "@ant-design/icons";
|
|
3
|
+
import R from "papaparse";
|
|
4
|
+
import { Tooltip as V, App as U, Modal as Y, Space as O, Spin as G, Progress as J } from "antd";
|
|
5
|
+
import { saveAs as K } from "file-saver";
|
|
6
|
+
import { useState as A, useEffect as Q, useMemo as w, useCallback as f } from "react";
|
|
7
|
+
import { useTranslationLib as X } from "../../locale/index.es.js";
|
|
8
|
+
import Z from "../view/CrudViewer.es.js";
|
|
9
|
+
import P from "../../util/DateUtil.es.js";
|
|
10
|
+
import ee from "../../common/button/Button.es.js";
|
|
11
|
+
import { ImportButton as te } from "../../common/button/ImportButton.es.js";
|
|
12
|
+
function pe({
|
|
13
|
+
onCloseMethod: g,
|
|
14
14
|
open: x,
|
|
15
|
-
fields:
|
|
15
|
+
fields: F,
|
|
16
16
|
importProps: o
|
|
17
17
|
}) {
|
|
18
|
-
const [l,
|
|
19
|
-
|
|
20
|
-
x || (
|
|
18
|
+
const [l, v] = A([]), [C, k] = A(!1), [I, N] = A(0);
|
|
19
|
+
Q(() => {
|
|
20
|
+
x || (v([]), k(!1));
|
|
21
21
|
}, [x]);
|
|
22
|
-
const p =
|
|
22
|
+
const p = w(
|
|
23
23
|
() => new Map(
|
|
24
|
-
|
|
24
|
+
F.filter((e) => {
|
|
25
25
|
var t;
|
|
26
26
|
return !e.hidden && !((t = e.importProps) != null && t.hidden) && !e.readonly;
|
|
27
27
|
}).map((e) => [e.name, e])
|
|
28
28
|
),
|
|
29
|
-
[
|
|
30
|
-
),
|
|
29
|
+
[F]
|
|
30
|
+
), S = w(
|
|
31
31
|
() => Array.from(p.values()).flatMap((e) => {
|
|
32
32
|
var t;
|
|
33
33
|
return [
|
|
@@ -36,189 +36,195 @@ function fe({
|
|
|
36
36
|
];
|
|
37
37
|
}),
|
|
38
38
|
[p]
|
|
39
|
-
),
|
|
39
|
+
), W = f(
|
|
40
40
|
async function() {
|
|
41
|
-
const e = `${
|
|
41
|
+
const e = `${S.join(",")}
|
|
42
42
|
`, t = new Blob([e], { type: "text/csv;charset=utf-8;" });
|
|
43
|
-
|
|
43
|
+
K(
|
|
44
44
|
t,
|
|
45
|
-
(o == null ? void 0 : o.name) + " - " +
|
|
45
|
+
(o == null ? void 0 : o.name) + " - " + P.formatDateTimeWithSecond(/* @__PURE__ */ new Date()).replaceAll(":", "-") + ".csv"
|
|
46
46
|
);
|
|
47
47
|
},
|
|
48
|
-
[
|
|
49
|
-
), { t: a } =
|
|
48
|
+
[S, o == null ? void 0 : o.name]
|
|
49
|
+
), { t: a } = X(), L = f(async (e) => {
|
|
50
50
|
const t = new FileReader();
|
|
51
51
|
t.onerror = console.error, t.onload = async (r) => {
|
|
52
52
|
var n;
|
|
53
|
-
const c = (n = r == null ? void 0 : r.target) == null ? void 0 : n.result, s =
|
|
54
|
-
|
|
53
|
+
const c = (n = r == null ? void 0 : r.target) == null ? void 0 : n.result, s = R.parse(c, { header: !0 });
|
|
54
|
+
v(
|
|
55
|
+
s.data.map((d, m) => ({
|
|
56
|
+
...d,
|
|
57
|
+
id_private_private: "crud-import-" + m
|
|
58
|
+
}))
|
|
59
|
+
);
|
|
55
60
|
}, t.readAsText(e);
|
|
56
|
-
}, []),
|
|
57
|
-
|
|
58
|
-
|
|
61
|
+
}, []), M = f(
|
|
62
|
+
async (e) => {
|
|
63
|
+
console.log("Deleting item from import component", e, l), v((t) => t.filter((r) => r.id_private_private !== e));
|
|
64
|
+
},
|
|
65
|
+
[l]
|
|
66
|
+
), T = w(() => {
|
|
59
67
|
var t, r, c;
|
|
60
68
|
let e = !1;
|
|
61
69
|
for (const s of l)
|
|
62
70
|
for (const [n, d] of p.entries()) {
|
|
63
|
-
const
|
|
64
|
-
if (d.required && !
|
|
71
|
+
const m = s[n], h = m || !isNaN(m);
|
|
72
|
+
if (d.required && !h)
|
|
65
73
|
if ((r = (t = d.importProps) == null ? void 0 : t.extraFields) != null && r.length)
|
|
66
|
-
for (const
|
|
67
|
-
if (s[
|
|
74
|
+
for (const D of ((c = d.importProps) == null ? void 0 : c.extraFields) ?? [])
|
|
75
|
+
if (s[D] || !isNaN(s[D])) {
|
|
68
76
|
e = !1;
|
|
69
77
|
break;
|
|
70
78
|
} else return !0;
|
|
71
79
|
else return !0;
|
|
72
80
|
}
|
|
73
81
|
return e;
|
|
74
|
-
}, [l, p]),
|
|
82
|
+
}, [l, p]), u = f(
|
|
75
83
|
(e) => (t, r) => {
|
|
76
84
|
var s, n, d;
|
|
77
85
|
const c = t || !isNaN(t);
|
|
78
86
|
if (e.required && !c) {
|
|
79
|
-
let
|
|
87
|
+
let m = a("err.validation.required");
|
|
80
88
|
if ((n = (s = e.importProps) == null ? void 0 : s.extraFields) != null && n.length) {
|
|
81
|
-
const
|
|
82
|
-
(
|
|
89
|
+
const h = (d = e.importProps) == null ? void 0 : d.extraFields;
|
|
90
|
+
(h == null ? void 0 : h.find((q) => !r[q])) && (m = "Either one of these fields is required: " + [e.name, ...h].join(", "));
|
|
83
91
|
}
|
|
84
|
-
return /* @__PURE__ */
|
|
85
|
-
/* @__PURE__ */ i(
|
|
92
|
+
return /* @__PURE__ */ b(V, { className: "d-flex", title: m, children: [
|
|
93
|
+
/* @__PURE__ */ i(B, { twoToneColor: "#ee9702" }),
|
|
86
94
|
t
|
|
87
95
|
] });
|
|
88
96
|
}
|
|
89
97
|
return t;
|
|
90
98
|
},
|
|
91
99
|
[a]
|
|
92
|
-
),
|
|
100
|
+
), E = f(async (e) => {
|
|
93
101
|
N(e);
|
|
94
|
-
}, []),
|
|
102
|
+
}, []), y = f(async () => {
|
|
95
103
|
try {
|
|
96
|
-
|
|
104
|
+
k(!0), await o.onClickImport(l, E), g(!1);
|
|
97
105
|
} finally {
|
|
98
|
-
|
|
106
|
+
k(!1), N(0);
|
|
99
107
|
}
|
|
100
|
-
}, [l, o,
|
|
101
|
-
T ?
|
|
108
|
+
}, [l, o, g, E]), { modal: _ } = U.useApp(), z = f(async () => {
|
|
109
|
+
T ? _.warning ? _.warning({
|
|
102
110
|
title: a("str.warning"),
|
|
103
111
|
content: a("qus.importWithIssues"),
|
|
104
112
|
okText: a("str.import"),
|
|
105
|
-
onOk:
|
|
113
|
+
onOk: y,
|
|
106
114
|
okCancel: !0,
|
|
107
115
|
closable: !0
|
|
108
116
|
}) : (console.error(
|
|
109
117
|
"You must wrap your react app with App component. https://ant.design/components/app"
|
|
110
|
-
), window.confirm(a("qus.importWithIssues")) &&
|
|
111
|
-
}, [T,
|
|
112
|
-
|
|
113
|
-
|
|
118
|
+
), window.confirm(a("qus.importWithIssues")) && y()) : y();
|
|
119
|
+
}, [T, y, a]), j = w(
|
|
120
|
+
() => Array.from(p.values()).flatMap((e) => {
|
|
121
|
+
var t, r, c, s;
|
|
122
|
+
return e.type === "text" ? [
|
|
123
|
+
{
|
|
124
|
+
...e,
|
|
125
|
+
label: e.name,
|
|
126
|
+
render: u(e)
|
|
127
|
+
},
|
|
128
|
+
...((r = (t = e.importProps) == null ? void 0 : t.extraFields) == null ? void 0 : r.map((n, d) => ({
|
|
129
|
+
...e,
|
|
130
|
+
name: n,
|
|
131
|
+
label: n,
|
|
132
|
+
render: u(e)
|
|
133
|
+
}))) ?? []
|
|
134
|
+
] : e.type === "select" ? [
|
|
135
|
+
{
|
|
136
|
+
...e,
|
|
137
|
+
label: e.name,
|
|
138
|
+
type: "text",
|
|
139
|
+
render: u(e)
|
|
140
|
+
},
|
|
141
|
+
...((s = (c = e.importProps) == null ? void 0 : c.extraFields) == null ? void 0 : s.map((n) => ({
|
|
142
|
+
...e,
|
|
143
|
+
name: n,
|
|
144
|
+
label: n,
|
|
145
|
+
type: "text",
|
|
146
|
+
render: u(e)
|
|
147
|
+
}))) ?? []
|
|
148
|
+
] : [
|
|
149
|
+
{
|
|
150
|
+
...e,
|
|
151
|
+
label: e.name,
|
|
152
|
+
render: u(e)
|
|
153
|
+
}
|
|
154
|
+
];
|
|
155
|
+
}),
|
|
156
|
+
[p, u]
|
|
157
|
+
), $ = w(() => j.map((e) => ({ ...e, hideInTable: !1 })), [j]);
|
|
158
|
+
return /* @__PURE__ */ b(
|
|
159
|
+
Y,
|
|
114
160
|
{
|
|
115
161
|
title: [a("str.import"), o == null ? void 0 : o.name].filter(Boolean).join(" "),
|
|
116
162
|
width: "100%",
|
|
117
163
|
open: x,
|
|
118
|
-
onOk:
|
|
164
|
+
onOk: z,
|
|
119
165
|
destroyOnHidden: !0,
|
|
120
|
-
onCancel: () =>
|
|
166
|
+
onCancel: () => g(!1),
|
|
121
167
|
okText: a("str.import"),
|
|
122
|
-
confirmLoading:
|
|
168
|
+
confirmLoading: C,
|
|
123
169
|
okButtonProps: {
|
|
124
170
|
disabled: !(l != null && l.length)
|
|
125
171
|
},
|
|
126
|
-
footer: (e, { OkBtn: t, CancelBtn: r }) => /* @__PURE__ */
|
|
172
|
+
footer: (e, { OkBtn: t, CancelBtn: r }) => /* @__PURE__ */ b(O, { children: [
|
|
127
173
|
/* @__PURE__ */ i(r, {}),
|
|
128
174
|
/* @__PURE__ */ i(t, {}),
|
|
129
175
|
T && /* @__PURE__ */ i(
|
|
130
|
-
|
|
176
|
+
V,
|
|
131
177
|
{
|
|
132
178
|
className: "d-flex",
|
|
133
179
|
title: "Some fields have issue. Please review before submit.",
|
|
134
|
-
children: /* @__PURE__ */ i(
|
|
180
|
+
children: /* @__PURE__ */ i(B, { twoToneColor: "#ee9702" })
|
|
135
181
|
}
|
|
136
182
|
)
|
|
137
183
|
] }),
|
|
138
184
|
children: [
|
|
139
|
-
/* @__PURE__ */
|
|
185
|
+
/* @__PURE__ */ b(O, { children: [
|
|
140
186
|
/* @__PURE__ */ i(
|
|
141
|
-
|
|
187
|
+
ee,
|
|
142
188
|
{
|
|
143
|
-
onClick:
|
|
144
|
-
icon: /* @__PURE__ */ i(
|
|
189
|
+
onClick: W,
|
|
190
|
+
icon: /* @__PURE__ */ i(H, {}),
|
|
145
191
|
children: a("str.downloadCsvTemplate")
|
|
146
192
|
}
|
|
147
193
|
),
|
|
148
194
|
/* @__PURE__ */ i(
|
|
149
|
-
|
|
195
|
+
te,
|
|
150
196
|
{
|
|
151
|
-
disabled:
|
|
197
|
+
disabled: C,
|
|
152
198
|
type: "default",
|
|
153
|
-
onClick:
|
|
199
|
+
onClick: L,
|
|
154
200
|
accept: ".csv",
|
|
155
201
|
children: a("str.importCsvFile")
|
|
156
202
|
}
|
|
157
203
|
)
|
|
158
204
|
] }),
|
|
159
205
|
/* @__PURE__ */ i(
|
|
160
|
-
|
|
206
|
+
G,
|
|
161
207
|
{
|
|
162
|
-
spinning:
|
|
163
|
-
indicator:
|
|
164
|
-
|
|
208
|
+
spinning: C,
|
|
209
|
+
indicator: I ? /* @__PURE__ */ i(
|
|
210
|
+
J,
|
|
165
211
|
{
|
|
166
212
|
type: "circle",
|
|
167
|
-
percent:
|
|
213
|
+
percent: I * 100,
|
|
168
214
|
size: 50,
|
|
169
215
|
format: (e) => `${parseInt(e)}%`
|
|
170
216
|
}
|
|
171
217
|
) : void 0,
|
|
172
218
|
children: /* @__PURE__ */ i(
|
|
173
|
-
|
|
219
|
+
Z,
|
|
174
220
|
{
|
|
175
221
|
data: l,
|
|
176
222
|
bordered: !0,
|
|
177
223
|
size: "small",
|
|
178
224
|
idField: "id_private_private",
|
|
179
|
-
onDelete:
|
|
225
|
+
onDelete: M,
|
|
180
226
|
confirmDeleting: !1,
|
|
181
|
-
fields:
|
|
182
|
-
var t, r, c, s;
|
|
183
|
-
return e.type === "text" ? [
|
|
184
|
-
{
|
|
185
|
-
...e,
|
|
186
|
-
label: e.name,
|
|
187
|
-
hideInTable: !1,
|
|
188
|
-
render: h(e)
|
|
189
|
-
},
|
|
190
|
-
...((r = (t = e.importProps) == null ? void 0 : t.extraFields) == null ? void 0 : r.map((n, d) => ({
|
|
191
|
-
...e,
|
|
192
|
-
name: n,
|
|
193
|
-
label: n,
|
|
194
|
-
hideInTable: !1,
|
|
195
|
-
render: h(e)
|
|
196
|
-
}))) ?? []
|
|
197
|
-
] : e.type === "select" ? [
|
|
198
|
-
{
|
|
199
|
-
...e,
|
|
200
|
-
label: e.name,
|
|
201
|
-
hideInTable: !1,
|
|
202
|
-
type: "text",
|
|
203
|
-
render: h(e)
|
|
204
|
-
},
|
|
205
|
-
...((s = (c = e.importProps) == null ? void 0 : c.extraFields) == null ? void 0 : s.map((n) => ({
|
|
206
|
-
...e,
|
|
207
|
-
name: n,
|
|
208
|
-
label: n,
|
|
209
|
-
hideInTable: !1,
|
|
210
|
-
type: "text",
|
|
211
|
-
render: h(e)
|
|
212
|
-
}))) ?? []
|
|
213
|
-
] : [
|
|
214
|
-
{
|
|
215
|
-
...e,
|
|
216
|
-
label: e.name,
|
|
217
|
-
hideInTable: !1,
|
|
218
|
-
render: h(e)
|
|
219
|
-
}
|
|
220
|
-
];
|
|
221
|
-
})
|
|
227
|
+
fields: $
|
|
222
228
|
}
|
|
223
229
|
)
|
|
224
230
|
}
|
|
@@ -228,5 +234,5 @@ function fe({
|
|
|
228
234
|
);
|
|
229
235
|
}
|
|
230
236
|
export {
|
|
231
|
-
|
|
237
|
+
pe as default
|
|
232
238
|
};
|