@appcorp/fusion-storybook 0.2.29 → 0.2.31
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/base-modules/class/constants.d.ts +6 -1
- package/base-modules/class/constants.js +3 -0
- package/base-modules/class/context.js +2 -2
- package/base-modules/class/more-actions.js +205 -17
- package/base-modules/teacher/more-actions.js +27 -7
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -6,4 +6,9 @@
|
|
|
6
6
|
* - API Routes
|
|
7
7
|
*/
|
|
8
8
|
export declare const pageLimit: number;
|
|
9
|
-
export declare const CLASS_API_ROUTES:
|
|
9
|
+
export declare const CLASS_API_ROUTES: {
|
|
10
|
+
readonly LIST: "/api/v1/class";
|
|
11
|
+
readonly UNIT: "/api/v1/class";
|
|
12
|
+
readonly BULK: "/api/v1/class/bulk";
|
|
13
|
+
readonly BULK_STATUS: (jobId: string) => string;
|
|
14
|
+
};
|
|
@@ -13,5 +13,8 @@ export const pageLimit = Number(process.env.NEXT_PUBLIC_PAGE_LIMIT) || 10;
|
|
|
13
13
|
// API ROUTES
|
|
14
14
|
// ============================================================================
|
|
15
15
|
export const CLASS_API_ROUTES = {
|
|
16
|
+
LIST: "/api/v1/class",
|
|
16
17
|
UNIT: "/api/v1/class",
|
|
18
|
+
BULK: "/api/v1/class/bulk",
|
|
19
|
+
BULK_STATUS: (jobId) => `/api/v1/class/bulk/${jobId}`,
|
|
17
20
|
};
|
|
@@ -376,13 +376,13 @@ export const useClassModule = () => {
|
|
|
376
376
|
// ============================================================================
|
|
377
377
|
const headerActions = useMemo(() => [
|
|
378
378
|
{
|
|
379
|
-
enabled:
|
|
379
|
+
enabled: true,
|
|
380
380
|
handleOnClick: handleMoreActions,
|
|
381
381
|
label: t("actionHeaderMoreActions"),
|
|
382
382
|
order: 0,
|
|
383
383
|
},
|
|
384
384
|
{
|
|
385
|
-
enabled:
|
|
385
|
+
enabled: true,
|
|
386
386
|
handleOnClick: handleFilters,
|
|
387
387
|
label: t("actionHeaderFilters"),
|
|
388
388
|
order: 1,
|
|
@@ -1,31 +1,216 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* Provides bulk CSV import/export actions for the class module.
|
|
7
|
-
*/
|
|
8
|
-
import { API_METHODS, downloadFromUrl } from "@react-pakistan/util-functions";
|
|
3
|
+
import { downloadFromUrl } from "@react-pakistan/util-functions";
|
|
4
|
+
import { showErrorToast, showSuccessToast, showInfoToast, } from "@appcorp/shadcn/lib/toast-utils";
|
|
9
5
|
import { getCachedWorkspaceSync } from "../workspace/cache";
|
|
10
6
|
import converter from "json-2-csv";
|
|
11
7
|
import { Timeline } from "../../components/timeline";
|
|
12
8
|
import { useTranslations } from "next-intl";
|
|
9
|
+
import { CSVS } from "../../constants";
|
|
10
|
+
import { CLASS_API_ROUTES, pageLimit } from "./constants";
|
|
11
|
+
import { invalidateClassesCache } from "./cache";
|
|
12
|
+
import { CLASS_ACTION_TYPES, useClassContext } from "./context";
|
|
13
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
14
|
+
const workspace = getCachedWorkspaceSync();
|
|
15
|
+
const POLL_INTERVAL_MS = 2000;
|
|
16
|
+
const POLL_TIMEOUT_MS = 300000;
|
|
17
|
+
const handleGetAllRecords = async (schoolId) => {
|
|
18
|
+
const response = await fetch(`${CLASS_API_ROUTES.UNIT}?pageLimit=1000¤tPage=1&schoolId=${schoolId}`);
|
|
19
|
+
const result = await response.json();
|
|
20
|
+
const csv = await converter.json2csv(result.items || [], {});
|
|
21
|
+
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
|
22
|
+
await downloadFromUrl(blob, "class.csv");
|
|
23
|
+
};
|
|
24
|
+
async function submitBulkJob(csvData, method, signal) {
|
|
25
|
+
var _a;
|
|
26
|
+
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
27
|
+
if (!schoolId)
|
|
28
|
+
throw new Error("School ID not found");
|
|
29
|
+
const res = await fetch(CLASS_API_ROUTES.BULK, {
|
|
30
|
+
method,
|
|
31
|
+
headers: {
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify({ schoolId, csvData }),
|
|
35
|
+
signal,
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
const errorData = (await res.json().catch(() => ({})));
|
|
39
|
+
throw new Error(errorData.error || `Bulk operation failed with status ${res.status}`);
|
|
40
|
+
}
|
|
41
|
+
const data = (await res.json());
|
|
42
|
+
return data.jobId;
|
|
43
|
+
}
|
|
44
|
+
async function pollBulkJob(jobId, signal, onProgress) {
|
|
45
|
+
const startTime = Date.now();
|
|
46
|
+
let lastProgressToast = 0;
|
|
47
|
+
while (true) {
|
|
48
|
+
if (signal.aborted)
|
|
49
|
+
throw new Error("Polling cancelled");
|
|
50
|
+
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
51
|
+
throw new Error("Bulk operation timed out");
|
|
52
|
+
}
|
|
53
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
|
|
54
|
+
if (signal.aborted)
|
|
55
|
+
throw new Error("Polling cancelled");
|
|
56
|
+
const res = await fetch(CLASS_API_ROUTES.BULK_STATUS(jobId), {
|
|
57
|
+
signal,
|
|
58
|
+
});
|
|
59
|
+
if (!res.ok)
|
|
60
|
+
continue;
|
|
61
|
+
const data = (await res.json());
|
|
62
|
+
if (data.status === "completed" || data.status === "failed") {
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
// Show progress toast every 10 seconds while running
|
|
66
|
+
if (data.status === "running" && Date.now() - lastProgressToast > 10000) {
|
|
67
|
+
lastProgressToast = Date.now();
|
|
68
|
+
onProgress === null || onProgress === void 0 ? void 0 : onProgress(data.processed, data.total);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function formatErrorSummary(errors) {
|
|
73
|
+
if (!(errors === null || errors === void 0 ? void 0 : errors.length))
|
|
74
|
+
return "";
|
|
75
|
+
const lines = errors.slice(0, 5).map((e) => `Row ${e.row}: ${e.error}`);
|
|
76
|
+
const remaining = errors.length - 5;
|
|
77
|
+
if (remaining > 0)
|
|
78
|
+
lines.push(`...and ${remaining} more`);
|
|
79
|
+
return lines.join("\n");
|
|
80
|
+
}
|
|
13
81
|
export const ClassMoreActions = () => {
|
|
14
82
|
const t = useTranslations("class");
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
83
|
+
const { dispatch } = useClassContext();
|
|
84
|
+
const abortRef = useRef(null);
|
|
85
|
+
// Abort any in-flight polling on unmount
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
return () => {
|
|
88
|
+
var _a;
|
|
89
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
90
|
+
};
|
|
91
|
+
}, []);
|
|
92
|
+
const handleBulkFlow = useCallback(async (files, method) => {
|
|
93
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
94
|
+
const closeDrawer = () => {
|
|
95
|
+
dispatch({
|
|
96
|
+
type: CLASS_ACTION_TYPES.SET_DRAWER,
|
|
97
|
+
payload: { drawer: null },
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
// Cancel previous in-flight request
|
|
101
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
102
|
+
const controller = new AbortController();
|
|
103
|
+
abortRef.current = controller;
|
|
104
|
+
const { signal } = controller;
|
|
105
|
+
const label = method === "POST" ? "create" : "update";
|
|
106
|
+
// Read + parse CSV
|
|
107
|
+
const file = files[0];
|
|
108
|
+
const text = await file.text();
|
|
109
|
+
const records = converter.csv2json(text);
|
|
110
|
+
if (!Array.isArray(records) || records.length === 0) {
|
|
111
|
+
showErrorToast("CSV file is empty or invalid");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
// Client-side validation — basic required field check
|
|
115
|
+
const validationErrors = [];
|
|
116
|
+
for (let i = 0; i < records.length; i++) {
|
|
117
|
+
const row = records[i];
|
|
118
|
+
const msgs = [];
|
|
119
|
+
if (method === "POST") {
|
|
120
|
+
if (!((_b = row.code) === null || _b === void 0 ? void 0 : _b.trim()))
|
|
121
|
+
msgs.push("code is required");
|
|
122
|
+
if (!((_c = row.name) === null || _c === void 0 ? void 0 : _c.trim()))
|
|
123
|
+
msgs.push("name is required");
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
if (!((_d = row.code) === null || _d === void 0 ? void 0 : _d.trim()))
|
|
127
|
+
msgs.push("code is required for update");
|
|
128
|
+
}
|
|
129
|
+
if (msgs.length > 0) {
|
|
130
|
+
validationErrors.push({ row: i + 1, messages: msgs });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (validationErrors.length > 0) {
|
|
134
|
+
const summary = validationErrors
|
|
135
|
+
.slice(0, 5)
|
|
136
|
+
.map((e) => `Row ${e.row}: ${e.messages.join("; ")}`)
|
|
137
|
+
.join("\n");
|
|
138
|
+
const remaining = validationErrors.length - 5;
|
|
139
|
+
showErrorToast(`Validation failed for ${validationErrors.length} row(s).\n${summary}${remaining > 0 ? `\n...and ${remaining} more` : ""}`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
showInfoToast(`Bulk ${label} job queued (${records.length} records). Processing...`);
|
|
144
|
+
let jobId;
|
|
145
|
+
try {
|
|
146
|
+
jobId = await submitBulkJob(text, method, signal);
|
|
147
|
+
}
|
|
148
|
+
catch (submitError) {
|
|
149
|
+
showErrorToast(`Failed to submit ${label} job: ${submitError instanceof Error ? submitError.message : "Unknown error"}`);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const status = await pollBulkJob(jobId, signal, (processed, total) => {
|
|
153
|
+
showInfoToast(`Processing ${processed}/${total} classes...`);
|
|
154
|
+
});
|
|
155
|
+
if (signal.aborted)
|
|
156
|
+
return;
|
|
157
|
+
if (status.status === "completed") {
|
|
158
|
+
const r = status.results;
|
|
159
|
+
if (r && ((_e = r.errors) === null || _e === void 0 ? void 0 : _e.length) > 0) {
|
|
160
|
+
const summary = formatErrorSummary(r.errors);
|
|
161
|
+
showSuccessToast(`Created ${r.created} | Updated ${r.updated} | Skipped ${r.skipped}\n${summary}`);
|
|
162
|
+
}
|
|
163
|
+
else if (r) {
|
|
164
|
+
showSuccessToast(`Created ${r.created} | Updated ${r.updated} | Skipped ${r.skipped}`);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
showSuccessToast("Bulk operation completed successfully");
|
|
168
|
+
}
|
|
169
|
+
invalidateClassesCache();
|
|
170
|
+
const schoolId = ((_f = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _f === void 0 ? void 0 : _f.id) || "";
|
|
171
|
+
fetch(`${CLASS_API_ROUTES.LIST}?currentPage=1&pageLimit=${pageLimit}&schoolId=${schoolId}`, {
|
|
172
|
+
headers: {
|
|
173
|
+
"Content-Type": "application/json",
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
.then(async (res) => {
|
|
177
|
+
var _a, _b;
|
|
178
|
+
if (!res.ok)
|
|
179
|
+
return;
|
|
180
|
+
const data = await res.json();
|
|
181
|
+
dispatch({
|
|
182
|
+
type: CLASS_ACTION_TYPES.SET_ITEMS,
|
|
183
|
+
payload: {
|
|
184
|
+
items: (_a = data.items) !== null && _a !== void 0 ? _a : [],
|
|
185
|
+
count: (_b = data.count) !== null && _b !== void 0 ? _b : 0,
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
})
|
|
189
|
+
.catch(() => { });
|
|
190
|
+
closeDrawer();
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
const r = status.results;
|
|
194
|
+
const detail = ((_g = r === null || r === void 0 ? void 0 : r.errors) === null || _g === void 0 ? void 0 : _g.length)
|
|
195
|
+
? formatErrorSummary(r.errors)
|
|
196
|
+
: "Unknown error";
|
|
197
|
+
showErrorToast(`Bulk ${label} failed.\n${detail}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
if (error.message === "Polling cancelled")
|
|
202
|
+
return;
|
|
203
|
+
showErrorToast(`Bulk ${label} failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
204
|
+
}
|
|
205
|
+
}, [dispatch]);
|
|
206
|
+
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
207
|
+
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
23
208
|
const create = [
|
|
24
209
|
{
|
|
25
210
|
id: "1",
|
|
26
211
|
title: t("downloadEmptyCsvTemplate"),
|
|
27
212
|
handleOnClick: async () => {
|
|
28
|
-
await downloadFromUrl(
|
|
213
|
+
await downloadFromUrl(CSVS.Class, "class-template.csv");
|
|
29
214
|
},
|
|
30
215
|
},
|
|
31
216
|
{ id: "2", title: t("addYourDataToTheCsv") },
|
|
@@ -44,7 +229,10 @@ export const ClassMoreActions = () => {
|
|
|
44
229
|
},
|
|
45
230
|
},
|
|
46
231
|
{ id: "2", title: t("updateYourDataInTheCsv") },
|
|
47
|
-
{
|
|
232
|
+
{
|
|
233
|
+
id: "3",
|
|
234
|
+
title: t("uploadTheCompletedCsvToTheSystem"),
|
|
235
|
+
},
|
|
48
236
|
];
|
|
49
|
-
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate") }), _jsx(Timeline, { events: update, heading: t("bulkUpdate") })] }));
|
|
237
|
+
return (_jsxs("div", { className: "space-y-4", children: [_jsx(Timeline, { events: create, heading: t("bulkCreate"), handleOnBulkCreate: handleBulkCreate }), _jsx(Timeline, { events: update, heading: t("bulkUpdate"), handleOnBulkCreate: handleBulkUpdate })] }));
|
|
50
238
|
};
|
|
@@ -8,9 +8,9 @@ import { Timeline } from "../../components/timeline";
|
|
|
8
8
|
import { useTranslations } from "next-intl";
|
|
9
9
|
import { CSVS } from "../../constants";
|
|
10
10
|
import { teacherFormValidation } from "./validate";
|
|
11
|
-
import { TEACHER_API_ROUTES } from "./constants";
|
|
11
|
+
import { TEACHER_API_ROUTES, pageLimit } from "./constants";
|
|
12
12
|
import { invalidateTeachersCache } from "./cache";
|
|
13
|
-
import { TEACHER_ACTION_TYPES,
|
|
13
|
+
import { TEACHER_ACTION_TYPES, useTeacherContext } from "./context";
|
|
14
14
|
import { useRef, useEffect, useCallback } from "react";
|
|
15
15
|
const workspace = getCachedWorkspaceSync();
|
|
16
16
|
const POLL_INTERVAL_MS = 2000;
|
|
@@ -93,7 +93,7 @@ function formatErrorSummary(errors) {
|
|
|
93
93
|
}
|
|
94
94
|
export const TeacherMoreActions = () => {
|
|
95
95
|
const t = useTranslations("teacher");
|
|
96
|
-
const { dispatch
|
|
96
|
+
const { dispatch } = useTeacherContext();
|
|
97
97
|
const abortRef = useRef(null);
|
|
98
98
|
// Abort any in-flight polling on unmount
|
|
99
99
|
useEffect(() => {
|
|
@@ -103,7 +103,7 @@ export const TeacherMoreActions = () => {
|
|
|
103
103
|
};
|
|
104
104
|
}, []);
|
|
105
105
|
const handleBulkFlow = useCallback(async (files, method) => {
|
|
106
|
-
var _a, _b, _c;
|
|
106
|
+
var _a, _b, _c, _d;
|
|
107
107
|
const closeDrawer = () => {
|
|
108
108
|
dispatch({
|
|
109
109
|
type: TEACHER_ACTION_TYPES.SET_DRAWER,
|
|
@@ -172,12 +172,32 @@ export const TeacherMoreActions = () => {
|
|
|
172
172
|
showSuccessToast("Bulk operation completed successfully");
|
|
173
173
|
}
|
|
174
174
|
invalidateTeachersCache();
|
|
175
|
-
|
|
175
|
+
const schoolId = ((_c = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _c === void 0 ? void 0 : _c.id) || "";
|
|
176
|
+
fetch(`${TEACHER_API_ROUTES.LIST}?currentPage=1&pageLimit=${pageLimit}&schoolId=${schoolId}`, {
|
|
177
|
+
headers: {
|
|
178
|
+
"Content-Type": "application/json",
|
|
179
|
+
"x-api-token": process.env.NEXT_PUBLIC_API_KEY,
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
.then(async (res) => {
|
|
183
|
+
var _a, _b;
|
|
184
|
+
if (!res.ok)
|
|
185
|
+
return;
|
|
186
|
+
const data = await res.json();
|
|
187
|
+
dispatch({
|
|
188
|
+
type: TEACHER_ACTION_TYPES.SET_ITEMS,
|
|
189
|
+
payload: {
|
|
190
|
+
items: (_a = data.items) !== null && _a !== void 0 ? _a : [],
|
|
191
|
+
count: (_b = data.count) !== null && _b !== void 0 ? _b : 0,
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
})
|
|
195
|
+
.catch(() => { });
|
|
176
196
|
closeDrawer();
|
|
177
197
|
}
|
|
178
198
|
else {
|
|
179
199
|
const r = status.results;
|
|
180
|
-
const detail = ((
|
|
200
|
+
const detail = ((_d = r === null || r === void 0 ? void 0 : r.errors) === null || _d === void 0 ? void 0 : _d.length)
|
|
181
201
|
? formatErrorSummary(r.errors)
|
|
182
202
|
: "Unknown error";
|
|
183
203
|
showErrorToast(`Bulk ${label} failed.\n${detail}`);
|
|
@@ -188,7 +208,7 @@ export const TeacherMoreActions = () => {
|
|
|
188
208
|
return;
|
|
189
209
|
showErrorToast(`Bulk ${label} failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
190
210
|
}
|
|
191
|
-
}, [dispatch
|
|
211
|
+
}, [dispatch]);
|
|
192
212
|
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
193
213
|
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
194
214
|
const create = [
|