@skalfa/skalfa-app 1.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.
- package/.env.example +44 -0
- package/README.md +28 -0
- package/app/auth/edit/page.tsx +65 -0
- package/app/auth/login/page.tsx +63 -0
- package/app/auth/me/page.tsx +58 -0
- package/app/auth/register/page.tsx +69 -0
- package/app/auth/verify/page.tsx +53 -0
- package/app/dashboard/layout.tsx +47 -0
- package/app/dashboard/page.tsx +9 -0
- package/app/dashboard/user/page.tsx +77 -0
- package/app/index.ts +14 -0
- package/app/layout.tsx +38 -0
- package/app/page.tsx +13 -0
- package/barrels.json +6 -0
- package/blueprints/starter.blueprint.json +103 -0
- package/components/base.components/accordion/Accordion.component.tsx +82 -0
- package/components/base.components/breadcrumb/Breadcrumb.component.tsx +80 -0
- package/components/base.components/button/Button.component.tsx +91 -0
- package/components/base.components/button/IconButton.component.tsx +88 -0
- package/components/base.components/button/button.decorate.ts +82 -0
- package/components/base.components/card/AlertCard.component.tsx +69 -0
- package/components/base.components/card/Card.component.tsx +25 -0
- package/components/base.components/card/DashboardCard.component.tsx +44 -0
- package/components/base.components/card/GalleryCard.component.tsx +50 -0
- package/components/base.components/card/ProductCard.component.tsx +65 -0
- package/components/base.components/card/ProfileCard.component.tsx +71 -0
- package/components/base.components/carousel/Carousel.component.tsx +113 -0
- package/components/base.components/chip/Chip.component.tsx +39 -0
- package/components/base.components/document/DocumentViewer.component.tsx +164 -0
- package/components/base.components/document/ExportExcel.component.tsx +340 -0
- package/components/base.components/document/ImportExcel.component.tsx +315 -0
- package/components/base.components/document/PrintTable.component.tsx +204 -0
- package/components/base.components/document/RenderPDF.component.tsx +416 -0
- package/components/base.components/index.ts +85 -0
- package/components/base.components/input/Checkbox.component.tsx +109 -0
- package/components/base.components/input/Input.component.tsx +332 -0
- package/components/base.components/input/InputCheckbox.component.tsx +174 -0
- package/components/base.components/input/InputCurrency.component.tsx +163 -0
- package/components/base.components/input/InputDate.component.tsx +352 -0
- package/components/base.components/input/InputDatetime.component.tsx +260 -0
- package/components/base.components/input/InputDocument.component.tsx +352 -0
- package/components/base.components/input/InputImage.component.tsx +533 -0
- package/components/base.components/input/InputMap.component.tsx +318 -0
- package/components/base.components/input/InputNumber.component.tsx +192 -0
- package/components/base.components/input/InputOtp.component.tsx +169 -0
- package/components/base.components/input/InputPassword.component.tsx +236 -0
- package/components/base.components/input/InputRadio.component.tsx +175 -0
- package/components/base.components/input/InputTime.component.tsx +276 -0
- package/components/base.components/input/InputValues.component.tsx +68 -0
- package/components/base.components/input/Radio.component.tsx +102 -0
- package/components/base.components/input/Select.component.tsx +541 -0
- package/components/base.components/modal/BottomSheet.component.tsx +246 -0
- package/components/base.components/modal/FloatingPage.component.tsx +104 -0
- package/components/base.components/modal/Modal.component.tsx +96 -0
- package/components/base.components/modal/ModalConfirm.component.tsx +218 -0
- package/components/base.components/modal/Toast.component.tsx +126 -0
- package/components/base.components/nav/Bottombar.component.tsx +116 -0
- package/components/base.components/nav/Footer.component.tsx +144 -0
- package/components/base.components/nav/Headbar.component.tsx +104 -0
- package/components/base.components/nav/Navbar.component.tsx +100 -0
- package/components/base.components/nav/Sidebar.component.tsx +301 -0
- package/components/base.components/nav/Tabbar.component.tsx +60 -0
- package/components/base.components/nav/Wizard.component.tsx +73 -0
- package/components/base.components/supervision/FormSupervision.component.tsx +434 -0
- package/components/base.components/supervision/TableSupervision.component.tsx +697 -0
- package/components/base.components/table/ControlBar.component.tsx +497 -0
- package/components/base.components/table/FilterComponent.tsx +518 -0
- package/components/base.components/table/Pagination.component.tsx +159 -0
- package/components/base.components/table/Table.component.tsx +469 -0
- package/components/base.components/typography/TypographyArticle.component.tsx +26 -0
- package/components/base.components/typography/TypographyColumn.component.tsx +20 -0
- package/components/base.components/typography/TypographyContent.component.tsx +20 -0
- package/components/base.components/typography/TypographyTips.component.tsx +20 -0
- package/components/base.components/wrap/Draggable.component.tsx +303 -0
- package/components/base.components/wrap/IDBProvider.tsx +12 -0
- package/components/base.components/wrap/Image.component.tsx +10 -0
- package/components/base.components/wrap/OutsideClick.component.tsx +48 -0
- package/components/base.components/wrap/ScrollContainer.component.tsx +104 -0
- package/components/base.components/wrap/ShortcutProvider.tsx +57 -0
- package/components/base.components/wrap/Swipe.component.tsx +93 -0
- package/components/construct.components/example.tsx +1 -0
- package/components/construct.components/index.ts +5 -0
- package/components/index.ts +3 -0
- package/components/structure.components/example.tsx +1 -0
- package/components/structure.components/index.ts +5 -0
- package/contexts/AppProvider.tsx +12 -0
- package/contexts/Auth.context.tsx +64 -0
- package/contexts/Toggle.context.tsx +44 -0
- package/contexts/index.ts +7 -0
- package/eslint.config.mjs +34 -0
- package/langs/index.ts +1 -0
- package/langs/validation.langs.ts +17 -0
- package/next.config.ts +17 -0
- package/package.json +43 -0
- package/postcss.config.mjs +12 -0
- package/public/204.svg +19 -0
- package/public/500.svg +39 -0
- package/public/images/avatar.jpg +0 -0
- package/public/images/example.png +0 -0
- package/schema/idb/app.schema.ts +9 -0
- package/schema/index.ts +5 -0
- package/styles/globals.css +231 -0
- package/styles/tailwind.safelist +69 -0
- package/tailwind.config.ts +10 -0
- package/tsconfig.json +35 -0
- package/utils/commands/barrels.ts +28 -0
- package/utils/commands/blueprint.ts +421 -0
- package/utils/commands/light.ts +21 -0
- package/utils/commands/logger.ts +42 -0
- package/utils/commands/stubs/table-blueprint.stub +13 -0
- package/utils/commands/use-pdf.ts +29 -0
- package/utils/index.ts +3 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import ExcelJS from "exceljs";
|
|
4
|
+
import { useEffect, useMemo, useState } from "react";
|
|
5
|
+
import { faArrowLeft, faArrowRight, faEdit, faPlus, faTimes } from "@fortawesome/free-solid-svg-icons";
|
|
6
|
+
import { api, FetchControlType, useTable } from "@utils";
|
|
7
|
+
import { TableComponent, ButtonComponent, IconButtonComponent, SelectComponent, ModalComponent } from "@components";
|
|
8
|
+
import { useToggleContext } from "@contexts";
|
|
9
|
+
import { FilterComponent } from "../table/FilterComponent";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
type ExportColumn = {
|
|
14
|
+
selector : string;
|
|
15
|
+
label : string;
|
|
16
|
+
source : string | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type ExportExcelColumnControlType = {
|
|
20
|
+
label : string;
|
|
21
|
+
selector : string;
|
|
22
|
+
status ?: "default" | "optional" | "hidden";
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type ExportExcelProps = {
|
|
26
|
+
fetchControl : FetchControlType;
|
|
27
|
+
columnControl ?: ExportExcelColumnControlType[];
|
|
28
|
+
filename ?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
const ADD_COLUMN_KEY = "__add_column__";
|
|
34
|
+
|
|
35
|
+
function numberToExcelColumn(index: number): string {
|
|
36
|
+
let column = "";
|
|
37
|
+
let n = index;
|
|
38
|
+
|
|
39
|
+
while (n >= 0) {
|
|
40
|
+
column = String.fromCharCode((n % 26) + 65) + column;
|
|
41
|
+
n = Math.floor(n / 26) - 1;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return column;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
export function ExportExcel({ fetchControl, columnControl, filename }: ExportExcelProps) {
|
|
50
|
+
const { toggle, setToggle } = useToggleContext()
|
|
51
|
+
|
|
52
|
+
const { data, tableControl, setParam, params } = useTable(fetchControl, undefined, undefined, false);
|
|
53
|
+
|
|
54
|
+
const fields = useMemo(() => {
|
|
55
|
+
if (columnControl?.length) {
|
|
56
|
+
return columnControl.filter(c => c.status !== "hidden").map(c => c.selector);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!data?.data?.[0]) return [];
|
|
60
|
+
return Object.keys(data.data[0]);
|
|
61
|
+
}, [data, columnControl]);
|
|
62
|
+
|
|
63
|
+
const [columns, setColumns] = useState<ExportColumn[]>([]);
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (!data?.data?.[0]) return;
|
|
68
|
+
|
|
69
|
+
setColumns(prev => {
|
|
70
|
+
if (prev.length) return prev;
|
|
71
|
+
|
|
72
|
+
const baseFields = columnControl?.length ? columnControl.filter(c => c.status === "default" || c.status === undefined).map(c => c.selector) : Object.keys(data.data[0]);
|
|
73
|
+
|
|
74
|
+
return baseFields.map((field, i) => {
|
|
75
|
+
const label = numberToExcelColumn(i);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
selector : label,
|
|
79
|
+
label : label,
|
|
80
|
+
source : field,
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}, [data, columnControl]);
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
const addColumn = (selector: string, source: string) => {
|
|
88
|
+
setColumns(prev => [...prev, { selector, label: selector, source }]);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const removeColumn = (selector: string) => {
|
|
92
|
+
setColumns(prev => prev.filter(c => c.selector !== selector).map((c, i) => {
|
|
93
|
+
const label = numberToExcelColumn(i);
|
|
94
|
+
return { ...c, selector: label, label };
|
|
95
|
+
}));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const moveColumn = (selector: string, dir: "left" | "right") => {
|
|
99
|
+
setColumns(prev => {
|
|
100
|
+
const idx = prev.findIndex(c => c.selector === selector);
|
|
101
|
+
if (idx === -1) return prev;
|
|
102
|
+
|
|
103
|
+
const target = dir === "left" ? idx - 1 : idx + 1;
|
|
104
|
+
if (target < 0 || target >= prev.length) return prev;
|
|
105
|
+
|
|
106
|
+
const next = [...prev];
|
|
107
|
+
[next[idx], next[target]] = [next[target], next[idx]];
|
|
108
|
+
|
|
109
|
+
return next.map((c, i) => {
|
|
110
|
+
const label = numberToExcelColumn(i);
|
|
111
|
+
return { ...c, selector: label, label };
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const setSource = (selector: string, source: string | null) => setColumns(prev => prev.map(c => (c.selector === selector ? { ...c, source } : c)));
|
|
117
|
+
|
|
118
|
+
const getColumnLabel = (source: string | null) => {
|
|
119
|
+
if (!source) return "";
|
|
120
|
+
|
|
121
|
+
const found = columnControl?.find(c => c.selector === source);
|
|
122
|
+
|
|
123
|
+
return found?.label ?? source;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
const tableColumns = useMemo(() => {
|
|
128
|
+
return [
|
|
129
|
+
...columns?.map((c => ({
|
|
130
|
+
...c,
|
|
131
|
+
label: <div className="w-full text-center">{c.label}</div>
|
|
132
|
+
}))),
|
|
133
|
+
{ selector: ADD_COLUMN_KEY, label: "" } as any,
|
|
134
|
+
];
|
|
135
|
+
}, [columns]);
|
|
136
|
+
|
|
137
|
+
const tableData = useMemo(() => {
|
|
138
|
+
if (!data?.data || !columns.length) return [];
|
|
139
|
+
|
|
140
|
+
const controlRow: Record<string, any> = {};
|
|
141
|
+
|
|
142
|
+
columns.forEach((col, iCol) => {
|
|
143
|
+
controlRow[col.selector] = (
|
|
144
|
+
<div className="flex justify-between">
|
|
145
|
+
<p className="font-semibold">{getColumnLabel(col.source)}</p>
|
|
146
|
+
|
|
147
|
+
<div className="flex gap-1">
|
|
148
|
+
{iCol > 0 && (
|
|
149
|
+
<IconButtonComponent
|
|
150
|
+
icon={faArrowLeft}
|
|
151
|
+
size="xs"
|
|
152
|
+
variant="outline"
|
|
153
|
+
className="!text-foreground"
|
|
154
|
+
onClick={() => moveColumn(col.selector, "left")}
|
|
155
|
+
/>
|
|
156
|
+
)}
|
|
157
|
+
|
|
158
|
+
{iCol < (columns.length - 1) && (
|
|
159
|
+
<IconButtonComponent
|
|
160
|
+
icon={faArrowRight}
|
|
161
|
+
size="xs"
|
|
162
|
+
variant="outline"
|
|
163
|
+
className="!text-foreground"
|
|
164
|
+
onClick={() => moveColumn(col.selector, "right")}
|
|
165
|
+
/>
|
|
166
|
+
)}
|
|
167
|
+
|
|
168
|
+
<IconButtonComponent
|
|
169
|
+
icon={faEdit}
|
|
170
|
+
size="xs"
|
|
171
|
+
paint="warning"
|
|
172
|
+
variant="outline"
|
|
173
|
+
disabled={columns.length <= 1}
|
|
174
|
+
onClick={() => setToggle("MODAL_FIELD_EXPORT", {selector: col.selector, value: col.source})}
|
|
175
|
+
/>
|
|
176
|
+
|
|
177
|
+
<IconButtonComponent
|
|
178
|
+
icon={faTimes}
|
|
179
|
+
size="xs"
|
|
180
|
+
paint="danger"
|
|
181
|
+
variant="outline"
|
|
182
|
+
disabled={columns.length <= 1}
|
|
183
|
+
onClick={() => removeColumn(col.selector)}
|
|
184
|
+
/>
|
|
185
|
+
</div>
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
</div>
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
controlRow[ADD_COLUMN_KEY] = (
|
|
193
|
+
<div className="flex justify-center">
|
|
194
|
+
<ButtonComponent
|
|
195
|
+
icon={faPlus}
|
|
196
|
+
label="Tambah Kolom"
|
|
197
|
+
size="xs"
|
|
198
|
+
variant="outline"
|
|
199
|
+
onClick={() => {
|
|
200
|
+
const idx = columns.length;
|
|
201
|
+
const label = numberToExcelColumn(idx);
|
|
202
|
+
setToggle("MODAL_FIELD_EXPORT", { selector: label, value: "", add: true })
|
|
203
|
+
}}
|
|
204
|
+
/>
|
|
205
|
+
</div>
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
const excelRows = data.data.map((item: Record<string, any>) => {
|
|
209
|
+
const row: Record<string, any> = {};
|
|
210
|
+
|
|
211
|
+
row[ADD_COLUMN_KEY] = "-"
|
|
212
|
+
columns.forEach(col => {
|
|
213
|
+
row[col.selector] = col.source ? <span className="line-clamp-1">{item[col.source] || "-"}</span> : "-";
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return row;
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
return [controlRow, ...excelRows];
|
|
220
|
+
}, [data, columns, fields]);
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
const handleDownloadExcel = async () => {
|
|
224
|
+
const record = await api({...fetchControl, params: {...fetchControl.params, ...params, page: 1, paginate: 9999}});
|
|
225
|
+
if (!record?.data?.data?.length || !columns.length) return;
|
|
226
|
+
|
|
227
|
+
const workbook = new ExcelJS.Workbook();
|
|
228
|
+
const worksheet = workbook.addWorksheet("Export");
|
|
229
|
+
|
|
230
|
+
worksheet.columns = columns.map(col => ({
|
|
231
|
+
header : getColumnLabel(col.source),
|
|
232
|
+
key : col.selector,
|
|
233
|
+
width : 20,
|
|
234
|
+
}));
|
|
235
|
+
|
|
236
|
+
worksheet.getRow(1).eachCell(cell => {
|
|
237
|
+
cell.font = { bold: true };
|
|
238
|
+
cell.alignment = { horizontal: "center", vertical: "middle" };
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
record.data.data.forEach((item: Record<string, any>) => {
|
|
242
|
+
const row: Record<string, any> = {};
|
|
243
|
+
|
|
244
|
+
columns.forEach(col => {
|
|
245
|
+
row[col.selector] = col.source ? item[col.source] : "";
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
worksheet.addRow(row);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
const buffer = await workbook.xlsx.writeBuffer();
|
|
252
|
+
const blob = new Blob([buffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
|
|
253
|
+
|
|
254
|
+
const link = document.createElement("a");
|
|
255
|
+
link.href = URL.createObjectURL(blob);
|
|
256
|
+
link.download = filename ? filename + ".xlsx" : "export.xlsx";
|
|
257
|
+
link.click();
|
|
258
|
+
|
|
259
|
+
URL.revokeObjectURL(link.href);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
return (
|
|
265
|
+
<>
|
|
266
|
+
<div className="p-4">
|
|
267
|
+
<FilterComponent
|
|
268
|
+
columns={columnControl?.length
|
|
269
|
+
? columnControl.filter(c => c.status !== "hidden").map(c => ({
|
|
270
|
+
label: c.label,
|
|
271
|
+
selector: c.selector,
|
|
272
|
+
type: "text"
|
|
273
|
+
}))
|
|
274
|
+
: (data?.data?.[0] ? Object.keys(data.data[0]).map((c) => ({
|
|
275
|
+
label: c,
|
|
276
|
+
selector: c,
|
|
277
|
+
type: "text"
|
|
278
|
+
})) : [])
|
|
279
|
+
}
|
|
280
|
+
onChange={(filters) => setParam('filter', filters)}
|
|
281
|
+
value={params?.filter || []}
|
|
282
|
+
/>
|
|
283
|
+
</div>
|
|
284
|
+
|
|
285
|
+
<TableComponent
|
|
286
|
+
controlBar={false}
|
|
287
|
+
columns={tableColumns}
|
|
288
|
+
data={tableData}
|
|
289
|
+
{...tableControl}
|
|
290
|
+
pagination={false}
|
|
291
|
+
className="p-4 bg-background row::bg-transparent row::border-0 row::gap-0 row::!hover:bg-background column::p-2 column::border head-column::p-2 head-column::border"
|
|
292
|
+
/>
|
|
293
|
+
|
|
294
|
+
<div className="px-4 mt-8">
|
|
295
|
+
<ButtonComponent label="Download Excel" block onClick={() => handleDownloadExcel()} />
|
|
296
|
+
</div>
|
|
297
|
+
|
|
298
|
+
<ModalComponent
|
|
299
|
+
show={!!toggle["MODAL_FIELD_EXPORT"]}
|
|
300
|
+
onClose={() => setToggle("MODAL_FIELD_EXPORT", false)}
|
|
301
|
+
title="Pilih Kolom"
|
|
302
|
+
footer={
|
|
303
|
+
<div className="flex justify-end">
|
|
304
|
+
<ButtonComponent
|
|
305
|
+
label="Terapkan"
|
|
306
|
+
onClick={() => {
|
|
307
|
+
if(!!(toggle["MODAL_FIELD_EXPORT"] as { value: string })?.value) {
|
|
308
|
+
if((toggle["MODAL_FIELD_EXPORT"] as { add: boolean })?.add) {
|
|
309
|
+
addColumn((toggle["MODAL_FIELD_EXPORT"] as { selector: string })?.selector, (toggle["MODAL_FIELD_EXPORT"] as { value: string })?.value)
|
|
310
|
+
} else {
|
|
311
|
+
setSource((toggle["MODAL_FIELD_EXPORT"] as { selector: string })?.selector, (toggle["MODAL_FIELD_EXPORT"] as { value: string })?.value)
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
setToggle("MODAL_FIELD_EXPORT", false)
|
|
315
|
+
}}
|
|
316
|
+
/>
|
|
317
|
+
</div>
|
|
318
|
+
}
|
|
319
|
+
>
|
|
320
|
+
<div className="p-4">
|
|
321
|
+
<SelectComponent
|
|
322
|
+
name={`column_${(toggle["MODAL_FIELD_EXPORT"] as { selector: string })?.selector}`}
|
|
323
|
+
placeholder="Pilih kolom data..."
|
|
324
|
+
value={(toggle["MODAL_FIELD_EXPORT"] as { value: string })?.value ?? ""}
|
|
325
|
+
onChange={e => setToggle("MODAL_FIELD_EXPORT", {...(toggle["MODAL_FIELD_EXPORT"] as object), value: e})}
|
|
326
|
+
options={fields.map((f: string) => {
|
|
327
|
+
const col = columnControl?.find(c => c.selector === f);
|
|
328
|
+
if (col?.status === "hidden") return null;
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
label: col?.label || f,
|
|
332
|
+
value: f,
|
|
333
|
+
};
|
|
334
|
+
}).filter(Boolean) as any}
|
|
335
|
+
/>
|
|
336
|
+
</div>
|
|
337
|
+
</ModalComponent>
|
|
338
|
+
</>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMemo, useState } from "react";
|
|
4
|
+
import ExcelJS from "exceljs";
|
|
5
|
+
import { faEdit } from "@fortawesome/free-solid-svg-icons";
|
|
6
|
+
import { TableComponent, ButtonComponent, SelectComponent, ModalComponent, IconButtonComponent } from "@components";
|
|
7
|
+
import { useToggleContext } from "@contexts";
|
|
8
|
+
import { api, FetchControlType } from "@utils";
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export type ImportExcelColumnControlType = {
|
|
13
|
+
label: string;
|
|
14
|
+
selector: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ImportColumn = {
|
|
18
|
+
selector: string;
|
|
19
|
+
label: string;
|
|
20
|
+
source: string | null;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type ImportExcelProps = {
|
|
24
|
+
columnControl: ImportExcelColumnControlType[];
|
|
25
|
+
onSubmit?: (rows: any[]) => void;
|
|
26
|
+
submitControl?: FetchControlType;
|
|
27
|
+
fetchControl?: FetchControlType;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
function numberToExcelColumn(index: number): string
|
|
33
|
+
{
|
|
34
|
+
let column = "";
|
|
35
|
+
let n = index;
|
|
36
|
+
|
|
37
|
+
while (n >= 0) {
|
|
38
|
+
column = String.fromCharCode((n % 26) + 65) + column;
|
|
39
|
+
n = Math.floor(n / 26) - 1;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return column;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export function ImportExcel({ columnControl, onSubmit, submitControl, fetchControl }: ImportExcelProps) {
|
|
48
|
+
const { toggle, setToggle } = useToggleContext()
|
|
49
|
+
|
|
50
|
+
const [columns, setColumns] = useState<ImportColumn[]>([]);
|
|
51
|
+
const [rows, setRows] = useState<Record<string, any>[]>([]);
|
|
52
|
+
const [loaded, setLoaded] = useState(false);
|
|
53
|
+
|
|
54
|
+
const [processing, setProcessing] = useState(false);
|
|
55
|
+
const [progress, setProgress] = useState({ success: 0, failed: 0, total: 0 });
|
|
56
|
+
const [errors, setErrors] = useState<Record<number, string>>({});
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
const handleImportFile = async (file: File) => {
|
|
60
|
+
const workbook = new ExcelJS.Workbook();
|
|
61
|
+
const buffer = await file.arrayBuffer();
|
|
62
|
+
|
|
63
|
+
await workbook.xlsx.load(buffer);
|
|
64
|
+
const sheet = workbook.worksheets[0];
|
|
65
|
+
|
|
66
|
+
const excelColumns: ImportColumn[] = [];
|
|
67
|
+
sheet.getRow(1).eachCell((_, colIndex) => {
|
|
68
|
+
const label = numberToExcelColumn(colIndex - 1);
|
|
69
|
+
|
|
70
|
+
excelColumns.push({
|
|
71
|
+
selector: label,
|
|
72
|
+
label: label,
|
|
73
|
+
source: null,
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const excelRows: Record<string, any>[] = [];
|
|
78
|
+
sheet.eachRow((row, rowIndex) => {
|
|
79
|
+
if (rowIndex === 1) return;
|
|
80
|
+
|
|
81
|
+
const item: Record<string, any> = {};
|
|
82
|
+
excelColumns.forEach((col, i) => {
|
|
83
|
+
item[col.selector] = row.getCell(i + 1).value;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
excelRows.push(item);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
setColumns(excelColumns);
|
|
90
|
+
setRows(excelRows);
|
|
91
|
+
setLoaded(true);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const getColumnLabel = (source: string | null) => {
|
|
95
|
+
if (!source) return "";
|
|
96
|
+
|
|
97
|
+
const found = columnControl?.find(c => c.selector === source);
|
|
98
|
+
|
|
99
|
+
return found?.label ?? source;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
const tableColumns = useMemo(() => {
|
|
104
|
+
return columns?.map((c => ({
|
|
105
|
+
...c,
|
|
106
|
+
label: <div className="w-full text-center">{c.label}</div>
|
|
107
|
+
})));
|
|
108
|
+
}, [columns]);
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
const tableData = useMemo(() => {
|
|
112
|
+
if (!loaded) return [];
|
|
113
|
+
|
|
114
|
+
const mappingRow: Record<string, any> = {};
|
|
115
|
+
|
|
116
|
+
columns.forEach(col => {
|
|
117
|
+
mappingRow[col.selector] = (
|
|
118
|
+
<>
|
|
119
|
+
<div className="flex justify-between">
|
|
120
|
+
<p className="font-semibold">{getColumnLabel(col.source) || <p className="text-light-foreground">-- PILIH KOLOM --</p>}</p>
|
|
121
|
+
|
|
122
|
+
<IconButtonComponent
|
|
123
|
+
icon={faEdit}
|
|
124
|
+
size="xs"
|
|
125
|
+
paint="warning"
|
|
126
|
+
variant="outline"
|
|
127
|
+
disabled={columns.length <= 1}
|
|
128
|
+
onClick={() => setToggle("MODAL_FIELD_IMPORT", { selector: col.selector, value: col.source })}
|
|
129
|
+
/>
|
|
130
|
+
</div>
|
|
131
|
+
</>
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
return [mappingRow, ...rows];
|
|
136
|
+
}, [columns, rows, loaded, columnControl]);
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
const generatePayload = () => {
|
|
140
|
+
return rows.map(row => {
|
|
141
|
+
const payload: Record<string, any> = {};
|
|
142
|
+
|
|
143
|
+
columns.forEach(col => {
|
|
144
|
+
if (col.source) {
|
|
145
|
+
payload[col.source] = row[col.selector];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return payload;
|
|
150
|
+
});
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const handleSubmit = async () => {
|
|
154
|
+
const payload = generatePayload();
|
|
155
|
+
|
|
156
|
+
if (onSubmit) {
|
|
157
|
+
onSubmit(payload);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
setProcessing(true);
|
|
162
|
+
setProgress({ success: 0, failed: 0, total: payload.length });
|
|
163
|
+
setErrors({});
|
|
164
|
+
|
|
165
|
+
if (submitControl?.path) {
|
|
166
|
+
try {
|
|
167
|
+
await api({
|
|
168
|
+
path : submitControl?.path,
|
|
169
|
+
method : "POST",
|
|
170
|
+
payload : { data: payload },
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
setProgress({ success: payload.length, failed: 0, total: payload.length });
|
|
174
|
+
setToggle("MODAL_IMPORT_SUCCESS", true);
|
|
175
|
+
|
|
176
|
+
setTimeout(() => {
|
|
177
|
+
setToggle(Object.keys(toggle).find(k => k.startsWith("MODAL_IMPORT_")) || "", false);
|
|
178
|
+
}, 1500);
|
|
179
|
+
|
|
180
|
+
} catch (e: any) {
|
|
181
|
+
setProgress({ success: 0, failed: payload.length, total: payload.length });
|
|
182
|
+
setErrors({ 0: e.message || "Bulk import failed" });
|
|
183
|
+
}
|
|
184
|
+
} else if (fetchControl) {
|
|
185
|
+
let successCount = 0;
|
|
186
|
+
let failedCount = 0;
|
|
187
|
+
|
|
188
|
+
for (let i = 0; i < payload.length; i++) {
|
|
189
|
+
const item = payload[i];
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
await api({
|
|
193
|
+
...fetchControl,
|
|
194
|
+
method: "POST",
|
|
195
|
+
payload: item,
|
|
196
|
+
});
|
|
197
|
+
successCount++;
|
|
198
|
+
} catch (e: any) {
|
|
199
|
+
failedCount++;
|
|
200
|
+
setErrors(prev => ({ ...prev, [i]: e.message || "Failed" }));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
setProgress({ success: successCount, failed: failedCount, total: payload.length });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
setProcessing(false);
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<>
|
|
212
|
+
|
|
213
|
+
{!loaded && (
|
|
214
|
+
<div className="p-4 relative">
|
|
215
|
+
<input
|
|
216
|
+
type="file"
|
|
217
|
+
accept=".xlsx"
|
|
218
|
+
onChange={e =>
|
|
219
|
+
{
|
|
220
|
+
const file = e.target.files?.[0];
|
|
221
|
+
if (file) handleImportFile(file);
|
|
222
|
+
}}
|
|
223
|
+
className="text-transparent bg-background w-full aspect-video border border-dashed relative file:hidden placeholder:hidden rounded-md cursor-pointer"
|
|
224
|
+
/>
|
|
225
|
+
|
|
226
|
+
<div className="absolute top-1/2 left-1/2 -translate-1/2 text-light-foreground">
|
|
227
|
+
Pilih atau tarik file excel di sini
|
|
228
|
+
</div>
|
|
229
|
+
</div>
|
|
230
|
+
)}
|
|
231
|
+
|
|
232
|
+
{loaded && (
|
|
233
|
+
<TableComponent
|
|
234
|
+
controlBar={false}
|
|
235
|
+
columns={tableColumns}
|
|
236
|
+
data={tableData}
|
|
237
|
+
pagination={false}
|
|
238
|
+
noIndex
|
|
239
|
+
className="p-4 bg-background row::bg-background row::border-0 row::gap-0 row::!hover:bg-background column::p-2 column::border head-column::p-2 head-column::border"
|
|
240
|
+
/>
|
|
241
|
+
)}
|
|
242
|
+
|
|
243
|
+
{loaded && !processing && (
|
|
244
|
+
<div className="px-4 mt-8">
|
|
245
|
+
<ButtonComponent
|
|
246
|
+
label="Import Data"
|
|
247
|
+
block
|
|
248
|
+
onClick={handleSubmit}
|
|
249
|
+
/>
|
|
250
|
+
</div>
|
|
251
|
+
)}
|
|
252
|
+
|
|
253
|
+
{processing && (
|
|
254
|
+
<div className="px-4 mt-8 flex flex-col gap-2">
|
|
255
|
+
<div className="flex justify-between text-sm">
|
|
256
|
+
<span>Processing: {progress.success + progress.failed} / {progress.total}</span>
|
|
257
|
+
{progress.failed > 0 && <span className="text-danger">{progress.failed} Failed</span>}
|
|
258
|
+
</div>
|
|
259
|
+
<div className="w-full h-2 bg-secondary rounded-full overflow-hidden">
|
|
260
|
+
<div
|
|
261
|
+
className="h-full bg-primary transition-all duration-300"
|
|
262
|
+
style={{ width: `${((progress.success + progress.failed) / progress.total) * 100}%` }}
|
|
263
|
+
/>
|
|
264
|
+
</div>
|
|
265
|
+
</div>
|
|
266
|
+
)}
|
|
267
|
+
|
|
268
|
+
{Object.keys(errors).length > 0 && (
|
|
269
|
+
<div className="px-4 mt-4 max-h-40 overflow-y-auto">
|
|
270
|
+
<p className="text-danger font-semibold mb-2">Errors Details:</p>
|
|
271
|
+
{Object.entries(errors).map(([idx, msg]) => (
|
|
272
|
+
<div key={idx} className="text-xs text-danger">
|
|
273
|
+
Row {Number(idx) + 1}: {msg}
|
|
274
|
+
</div>
|
|
275
|
+
))}
|
|
276
|
+
</div>
|
|
277
|
+
)}
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
<ModalComponent
|
|
281
|
+
show={!!toggle["MODAL_FIELD_IMPORT"]}
|
|
282
|
+
onClose={() => setToggle("MODAL_FIELD_IMPORT", false)}
|
|
283
|
+
title="Pilih Kolom"
|
|
284
|
+
footer={
|
|
285
|
+
<div className="flex justify-end">
|
|
286
|
+
<ButtonComponent
|
|
287
|
+
label="Terapkan"
|
|
288
|
+
onClick={() => {
|
|
289
|
+
if (!!(toggle["MODAL_FIELD_IMPORT"] as { value: string })?.value) {
|
|
290
|
+
setColumns(prev =>
|
|
291
|
+
prev.map(c => c.selector === (toggle["MODAL_FIELD_IMPORT"] as { selector: string })?.selector ? { ...c, source: String((toggle["MODAL_FIELD_IMPORT"] as { value: string })?.value) } : c)
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
setToggle("MODAL_FIELD_IMPORT", false)
|
|
295
|
+
}}
|
|
296
|
+
/>
|
|
297
|
+
</div>
|
|
298
|
+
}
|
|
299
|
+
>
|
|
300
|
+
<div className="p-4">
|
|
301
|
+
<SelectComponent
|
|
302
|
+
name={`column_${(toggle["MODAL_FIELD_IMPORT"] as { selector: string })?.selector}`}
|
|
303
|
+
placeholder="Pilih kolom data..."
|
|
304
|
+
value={(toggle["MODAL_FIELD_IMPORT"] as { value: string })?.value ?? ""}
|
|
305
|
+
onChange={e => setToggle("MODAL_FIELD_IMPORT", { ...(toggle["MODAL_FIELD_IMPORT"] as object), value: e })}
|
|
306
|
+
options={columnControl.map(c => ({
|
|
307
|
+
label: c.label,
|
|
308
|
+
value: c.selector,
|
|
309
|
+
}))}
|
|
310
|
+
/>
|
|
311
|
+
</div>
|
|
312
|
+
</ModalComponent>
|
|
313
|
+
</>
|
|
314
|
+
);
|
|
315
|
+
}
|