@appcorp/fusion-storybook 0.2.21 → 0.2.23
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/teacher/constants.d.ts +10 -1
- package/base-modules/teacher/constants.js +3 -0
- package/base-modules/teacher/more-actions.js +127 -65
- package/base-modules/teacher/validate.d.ts +8 -2
- package/base-modules/teacher/validate.js +7 -2
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
|
@@ -7,7 +7,13 @@
|
|
|
7
7
|
* - Gender Options
|
|
8
8
|
*/
|
|
9
9
|
export declare const pageLimit: number;
|
|
10
|
-
export declare const TEACHER_API_ROUTES:
|
|
10
|
+
export declare const TEACHER_API_ROUTES: {
|
|
11
|
+
readonly LIST: "/api/v1/teacher";
|
|
12
|
+
readonly UNIT: "/api/v1/teacher";
|
|
13
|
+
readonly AVATAR: "/api/v1/teacher-avatar";
|
|
14
|
+
readonly BULK: "/api/v1/teacher/bulk";
|
|
15
|
+
readonly BULK_STATUS: (jobId: string) => string;
|
|
16
|
+
};
|
|
11
17
|
export declare const GENDER_OPTIONS: readonly [{
|
|
12
18
|
readonly label: "Select Gender";
|
|
13
19
|
readonly value: "";
|
|
@@ -20,6 +26,9 @@ export declare const GENDER_OPTIONS: readonly [{
|
|
|
20
26
|
}, {
|
|
21
27
|
readonly label: "Other";
|
|
22
28
|
readonly value: "OTHER";
|
|
29
|
+
}, {
|
|
30
|
+
readonly label: "Prefer not to say";
|
|
31
|
+
readonly value: "PREFER_NOT_TO_SAY";
|
|
23
32
|
}];
|
|
24
33
|
export declare const AVATAR_MAX_FILE_SIZE: number;
|
|
25
34
|
export declare const AVATAR_ALLOWED_MIME_TYPES: readonly ["image/webp", "image/png", "image/jpeg", "image/jpg", "image/gif", "image/bmp"];
|
|
@@ -14,9 +14,11 @@ export const pageLimit = Number(process.env.NEXT_PUBLIC_PAGE_LIMIT) || 10;
|
|
|
14
14
|
// API ROUTES
|
|
15
15
|
// ============================================================================
|
|
16
16
|
export const TEACHER_API_ROUTES = {
|
|
17
|
+
LIST: "/api/v1/teacher",
|
|
17
18
|
UNIT: "/api/v1/teacher",
|
|
18
19
|
AVATAR: "/api/v1/teacher-avatar",
|
|
19
20
|
BULK: "/api/v1/teacher/bulk",
|
|
21
|
+
BULK_STATUS: (jobId) => `/api/v1/teacher/bulk/${jobId}`,
|
|
20
22
|
};
|
|
21
23
|
// ============================================================================
|
|
22
24
|
// GENDER OPTIONS
|
|
@@ -26,6 +28,7 @@ export const GENDER_OPTIONS = [
|
|
|
26
28
|
{ label: "Male", value: "MALE" },
|
|
27
29
|
{ label: "Female", value: "FEMALE" },
|
|
28
30
|
{ label: "Other", value: "OTHER" },
|
|
31
|
+
{ label: "Prefer not to say", value: "PREFER_NOT_TO_SAY" },
|
|
29
32
|
];
|
|
30
33
|
// ============================================================================
|
|
31
34
|
// AVATAR UPLOAD CONFIGURATION
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import { downloadFromUrl } from "@react-pakistan/util-functions";
|
|
4
|
-
import { showErrorToast, showSuccessToast, } from "@appcorp/shadcn/lib/toast-utils";
|
|
4
|
+
import { showErrorToast, showSuccessToast, showInfoToast, } from "@appcorp/shadcn/lib/toast-utils";
|
|
5
5
|
import { getCachedWorkspaceSync } from "../workspace/cache";
|
|
6
6
|
import converter from "json-2-csv";
|
|
7
7
|
import { Timeline } from "../../components/timeline";
|
|
@@ -11,7 +11,10 @@ import { teacherFormValidation } from "./validate";
|
|
|
11
11
|
import { TEACHER_API_ROUTES } from "./constants";
|
|
12
12
|
import { invalidateTeachersCache } from "./cache";
|
|
13
13
|
import { useTeacherContext, TEACHER_ACTION_TYPES } from "./context";
|
|
14
|
+
import { useRef, useEffect, useCallback } from "react";
|
|
14
15
|
const workspace = getCachedWorkspaceSync();
|
|
16
|
+
const POLL_INTERVAL_MS = 2000;
|
|
17
|
+
const POLL_TIMEOUT_MS = 300000;
|
|
15
18
|
const handleGetAllRecords = async (schoolId) => {
|
|
16
19
|
const response = await fetch(`${TEACHER_API_ROUTES.UNIT}?pageLimit=1000¤tPage=1&schoolId=${schoolId}`);
|
|
17
20
|
const result = await response.json();
|
|
@@ -19,92 +22,151 @@ const handleGetAllRecords = async (schoolId) => {
|
|
|
19
22
|
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
|
|
20
23
|
await downloadFromUrl(blob, "teacher.csv");
|
|
21
24
|
};
|
|
22
|
-
async function
|
|
25
|
+
async function submitBulkJob(csvData, method, signal) {
|
|
23
26
|
var _a;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
28
|
+
if (!schoolId)
|
|
29
|
+
throw new Error("School ID not found");
|
|
30
|
+
const res = await fetch(TEACHER_API_ROUTES.BULK, {
|
|
31
|
+
method,
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
"x-api-token": process.env.NEXT_PUBLIC_API_KEY,
|
|
35
|
+
},
|
|
36
|
+
body: JSON.stringify({ schoolId, csvData }),
|
|
37
|
+
signal,
|
|
38
|
+
});
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
const errorData = (await res.json().catch(() => ({})));
|
|
41
|
+
throw new Error(errorData.error || `Bulk operation failed with status ${res.status}`);
|
|
42
|
+
}
|
|
43
|
+
const data = (await res.json());
|
|
44
|
+
return data.jobId;
|
|
45
|
+
}
|
|
46
|
+
async function pollBulkJob(jobId, signal) {
|
|
47
|
+
const startTime = Date.now();
|
|
48
|
+
while (true) {
|
|
49
|
+
if (signal.aborted)
|
|
50
|
+
throw new Error("Polling cancelled");
|
|
51
|
+
if (Date.now() - startTime > POLL_TIMEOUT_MS) {
|
|
52
|
+
throw new Error("Bulk operation timed out");
|
|
53
|
+
}
|
|
54
|
+
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
|
|
55
|
+
if (signal.aborted)
|
|
56
|
+
throw new Error("Polling cancelled");
|
|
57
|
+
const res = await fetch(TEACHER_API_ROUTES.BULK_STATUS(jobId), {
|
|
58
|
+
headers: {
|
|
59
|
+
"x-api-token": process.env.NEXT_PUBLIC_API_KEY,
|
|
60
|
+
},
|
|
61
|
+
signal,
|
|
62
|
+
});
|
|
63
|
+
if (!res.ok)
|
|
64
|
+
continue;
|
|
65
|
+
const data = (await res.json());
|
|
66
|
+
if (data.status === "completed" || data.status === "failed") {
|
|
67
|
+
return data;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function formatErrorSummary(errors) {
|
|
72
|
+
if (!(errors === null || errors === void 0 ? void 0 : errors.length))
|
|
73
|
+
return "";
|
|
74
|
+
const lines = errors.slice(0, 5).map((e) => `Row ${e.row}: ${e.error}`);
|
|
75
|
+
const remaining = errors.length - 5;
|
|
76
|
+
if (remaining > 0)
|
|
77
|
+
lines.push(`...and ${remaining} more`);
|
|
78
|
+
return lines.join("\n");
|
|
79
|
+
}
|
|
80
|
+
export const TeacherMoreActions = () => {
|
|
81
|
+
const t = useTranslations("teacher");
|
|
82
|
+
const { dispatch } = useTeacherContext();
|
|
83
|
+
const abortRef = useRef(null);
|
|
84
|
+
// Abort any in-flight polling on unmount
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
return () => {
|
|
87
|
+
var _a;
|
|
88
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
89
|
+
};
|
|
90
|
+
}, []);
|
|
91
|
+
const closeDrawer = () => {
|
|
92
|
+
dispatch({
|
|
93
|
+
type: TEACHER_ACTION_TYPES.SET_DRAWER,
|
|
94
|
+
payload: { drawer: null },
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
const handleBulkFlow = useCallback(async (files, method) => {
|
|
98
|
+
var _a, _b, _c;
|
|
99
|
+
// Cancel previous in-flight request
|
|
100
|
+
(_a = abortRef.current) === null || _a === void 0 ? void 0 : _a.abort();
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
abortRef.current = controller;
|
|
103
|
+
const { signal } = controller;
|
|
104
|
+
const label = method === "POST" ? "create" : "update";
|
|
105
|
+
// Read + parse CSV
|
|
106
|
+
const file = files[0];
|
|
28
107
|
const text = await file.text();
|
|
29
108
|
const records = converter.csv2json(text);
|
|
30
109
|
if (!Array.isArray(records) || records.length === 0) {
|
|
31
110
|
showErrorToast("CSV file is empty or invalid");
|
|
32
|
-
return
|
|
111
|
+
return;
|
|
33
112
|
}
|
|
34
|
-
|
|
113
|
+
// Client-side validation — fast feedback, saves worker round-trip
|
|
114
|
+
const validationErrors = [];
|
|
35
115
|
for (let i = 0; i < records.length; i++) {
|
|
36
116
|
const result = teacherFormValidation.safeParse(records[i]);
|
|
37
117
|
if (!result.success) {
|
|
38
|
-
|
|
118
|
+
validationErrors.push({
|
|
39
119
|
row: i + 1,
|
|
40
120
|
messages: result.error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`),
|
|
41
121
|
});
|
|
42
122
|
}
|
|
43
123
|
}
|
|
44
|
-
if (
|
|
45
|
-
const summary =
|
|
124
|
+
if (validationErrors.length > 0) {
|
|
125
|
+
const summary = validationErrors
|
|
46
126
|
.slice(0, 5)
|
|
47
127
|
.map((e) => `Row ${e.row}: ${e.messages.join("; ")}`)
|
|
48
128
|
.join("\n");
|
|
49
|
-
const remaining =
|
|
50
|
-
showErrorToast(`Validation failed for ${
|
|
51
|
-
return
|
|
129
|
+
const remaining = validationErrors.length - 5;
|
|
130
|
+
showErrorToast(`Validation failed for ${validationErrors.length} row(s).\n${summary}${remaining > 0 ? `\n...and ${remaining} more` : ""}`);
|
|
131
|
+
return;
|
|
52
132
|
}
|
|
53
|
-
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
54
133
|
try {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
134
|
+
showInfoToast(`Bulk ${label} job queued (${records.length} records). Processing...`);
|
|
135
|
+
const jobId = await submitBulkJob(text, method, signal);
|
|
136
|
+
const status = await pollBulkJob(jobId, signal);
|
|
137
|
+
if (signal.aborted)
|
|
138
|
+
return;
|
|
139
|
+
if (status.status === "completed") {
|
|
140
|
+
const r = status.results;
|
|
141
|
+
if (r && ((_b = r.errors) === null || _b === void 0 ? void 0 : _b.length) > 0) {
|
|
142
|
+
const summary = formatErrorSummary(r.errors);
|
|
143
|
+
showSuccessToast(`Created ${r.created} | Updated ${r.updated} | Skipped ${r.skipped}\n${summary}`);
|
|
144
|
+
}
|
|
145
|
+
else if (r) {
|
|
146
|
+
showSuccessToast(`Created ${r.created} | Updated ${r.updated} | Skipped ${r.skipped}`);
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
showSuccessToast("Bulk operation completed successfully");
|
|
150
|
+
}
|
|
151
|
+
invalidateTeachersCache();
|
|
152
|
+
closeDrawer();
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const r = status.results;
|
|
156
|
+
const detail = ((_c = r === null || r === void 0 ? void 0 : r.errors) === null || _c === void 0 ? void 0 : _c.length)
|
|
157
|
+
? formatErrorSummary(r.errors)
|
|
158
|
+
: "Unknown error";
|
|
159
|
+
showErrorToast(`Bulk ${label} failed.\n${detail}`);
|
|
69
160
|
}
|
|
70
|
-
const errorData = await response.json().catch(() => ({}));
|
|
71
|
-
showErrorToast(errorData.error ||
|
|
72
|
-
`Bulk operation failed with status ${response.status}`);
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
catch (_b) {
|
|
76
|
-
showErrorToast("Network error during bulk operation");
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
catch (error) {
|
|
81
|
-
showErrorToast(`Failed to process CSV: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
export const TeacherMoreActions = () => {
|
|
86
|
-
const t = useTranslations("teacher");
|
|
87
|
-
const { dispatch } = useTeacherContext();
|
|
88
|
-
const closeDrawer = () => {
|
|
89
|
-
dispatch({
|
|
90
|
-
type: TEACHER_ACTION_TYPES.SET_DRAWER,
|
|
91
|
-
payload: { drawer: null },
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
|
-
const handleBulkCreate = async (files) => {
|
|
95
|
-
const ok = await processBulkCsv(files, "POST");
|
|
96
|
-
if (ok) {
|
|
97
|
-
invalidateTeachersCache();
|
|
98
|
-
closeDrawer();
|
|
99
161
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
invalidateTeachersCache();
|
|
105
|
-
closeDrawer();
|
|
162
|
+
catch (error) {
|
|
163
|
+
if (error.message === "Polling cancelled")
|
|
164
|
+
return;
|
|
165
|
+
showErrorToast(`Bulk ${label} failed: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
106
166
|
}
|
|
107
|
-
};
|
|
167
|
+
}, [closeDrawer]);
|
|
168
|
+
const handleBulkCreate = useCallback((files) => handleBulkFlow(files, "POST"), [handleBulkFlow]);
|
|
169
|
+
const handleBulkUpdate = useCallback((files) => handleBulkFlow(files, "PUT"), [handleBulkFlow]);
|
|
108
170
|
const create = [
|
|
109
171
|
{
|
|
110
172
|
id: "1",
|
|
@@ -12,10 +12,16 @@ export declare const teacherFormValidation: z.ZodObject<{
|
|
|
12
12
|
country: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
13
13
|
dateOfBirth: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
14
14
|
emergencyPhone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
15
|
+
campusId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
15
16
|
experience: z.ZodNullable<z.ZodOptional<z.ZodNumber>>;
|
|
16
17
|
firstName: z.ZodString;
|
|
17
|
-
gender: z.ZodNullable<z.ZodOptional<z.
|
|
18
|
-
|
|
18
|
+
gender: z.ZodPreprocess<z.ZodNullable<z.ZodOptional<z.ZodEnum<{
|
|
19
|
+
MALE: "MALE";
|
|
20
|
+
FEMALE: "FEMALE";
|
|
21
|
+
OTHER: "OTHER";
|
|
22
|
+
PREFER_NOT_TO_SAY: "PREFER_NOT_TO_SAY";
|
|
23
|
+
}>>>>;
|
|
24
|
+
joiningDate: z.ZodOptional<z.ZodString>;
|
|
19
25
|
lastName: z.ZodString;
|
|
20
26
|
phone: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
21
27
|
postalCode: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
@@ -26,10 +26,15 @@ export const teacherFormValidation = z.object({
|
|
|
26
26
|
// });
|
|
27
27
|
// }
|
|
28
28
|
// }),
|
|
29
|
+
campusId: z.string().optional().nullable(),
|
|
29
30
|
experience: z.number().optional().nullable(),
|
|
30
31
|
firstName: z.string().min(1, "First name is required"),
|
|
31
|
-
gender: z
|
|
32
|
-
|
|
32
|
+
gender: z
|
|
33
|
+
.preprocess((val) => (val === "" ? undefined : val), z
|
|
34
|
+
.enum(["MALE", "FEMALE", "OTHER", "PREFER_NOT_TO_SAY"])
|
|
35
|
+
.optional()
|
|
36
|
+
.nullable()),
|
|
37
|
+
joiningDate: z.string().optional(),
|
|
33
38
|
lastName: z.string().min(1, "Last name is required"),
|
|
34
39
|
phone: z.string().nullable().optional(),
|
|
35
40
|
// .superRefine((val, ctx) => {
|