@appcorp/fusion-storybook 0.5.37 → 0.5.40

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.
Files changed (35) hide show
  1. package/base-modules/admission/context/use-admission-module.d.ts +4 -1
  2. package/base-modules/admission/context/use-admission-module.js +63 -40
  3. package/base-modules/expense/context/shared.d.ts +5 -0
  4. package/base-modules/expense/context/shared.js +1 -0
  5. package/base-modules/expense/context/use-expense-module.d.ts +2 -1
  6. package/base-modules/expense/context/use-expense-module.js +49 -5
  7. package/base-modules/expense/form.js +4 -2
  8. package/base-modules/school/context.d.ts +13 -7
  9. package/base-modules/school/context.js +46 -5
  10. package/base-modules/school/form.js +11 -1
  11. package/base-modules/student-profile/context/use-student-profile-module.js +4 -0
  12. package/base-modules/teacher/context.js +9 -37
  13. package/base-modules/user/context/use-user-module.d.ts +1 -0
  14. package/base-modules/user/context/use-user-module.js +22 -43
  15. package/base-modules/user/context.d.ts +5 -0
  16. package/base-modules/user/context.js +1 -0
  17. package/base-modules/workspace/context.d.ts +13 -37
  18. package/base-modules/workspace/context.js +47 -25
  19. package/base-modules/workspace/form.js +11 -1
  20. package/base-modules/workspace/view.js +12 -9
  21. package/constants.d.ts +0 -4
  22. package/constants.js +0 -4
  23. package/package.json +2 -1
  24. package/tsconfig.build.tsbuildinfo +1 -1
  25. package/types/user-management.d.ts +2 -12
  26. package/utils/student-usage-api.d.ts +6 -0
  27. package/utils/student-usage-api.js +16 -0
  28. package/utils/student-usage.d.ts +20 -0
  29. package/utils/student-usage.js +40 -0
  30. package/utils/upload-file.d.ts +42 -0
  31. package/utils/upload-file.js +116 -0
  32. package/base-modules/teacher/avatar-upload.d.ts +0 -36
  33. package/base-modules/teacher/avatar-upload.js +0 -104
  34. package/utils/file-to-base64.d.ts +0 -1
  35. package/utils/file-to-base64.js +0 -10
@@ -1,4 +1,5 @@
1
1
  import { AdmissionAIBE, AdmissionBE, DiscountCodeBE } from "../../../type";
2
+ import { type StudentUsage } from "../../../utils/student-usage";
2
3
  import { type RowAction, type TableRow } from "@appcorp/shadcn/components/enhanced-table";
3
4
  export declare const useAdmissionModule: () => {
4
5
  applyFilters: () => void;
@@ -36,8 +37,10 @@ export declare const useAdmissionModule: () => {
36
37
  }[];
37
38
  listLoading: boolean;
38
39
  rowActions: RowAction[];
40
+ studentUsage: StudentUsage | null;
41
+ refreshStudentUsage: () => Promise<void>;
39
42
  updateLoading: boolean;
40
- uploadAdmissionDocuments: (files: File[], registrationCode: string, prefix?: string) => Promise<string[]>;
43
+ uploadAdmissionDocuments: (files: File[]) => Promise<string[]>;
41
44
  state: {
42
45
  items: AdmissionBE[];
43
46
  count: number;
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { useCallback, useEffect, useMemo, useRef } from "react";
2
+ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
3
3
  import { useTheme } from "next-themes";
4
4
  import { useTranslations } from "next-intl";
5
5
  import { isCreatedOrUpdated } from "@react-pakistan/util-functions/general/is-created-or-updated";
@@ -11,12 +11,14 @@ import { useDebounce } from "@react-pakistan/util-functions/hooks/use-debounce";
11
11
  import { useFetch } from "@react-pakistan/util-functions/hooks/use-fetch";
12
12
  import { generateThemeToast, TOAST_VARIANT, } from "@appcorp/shadcn/lib/toast-utils";
13
13
  import { ADMISSION_STATUS, GENDER, } from "../../../type";
14
- import { ADMISSION_API_ROUTES, DISCOUNT_CODE_API_ROUTES, DOCUMENT_UPLOAD_API_ROUTES, } from "../../../constants";
14
+ import { ADMISSION_API_ROUTES, DISCOUNT_CODE_API_ROUTES, } from "../../../constants";
15
15
  import { admissionFormValidation } from "../validate";
16
16
  import { getCachedWorkspaceSync } from "../../workspace/cache";
17
17
  import { downloadAdmissionFormPDF, } from "../../../utils/admission-pdf";
18
- import { fileToBase64 } from "../../../utils/file-to-base64";
19
18
  import { compressPdf, DocumentTooLargeError, isPdfFile, MAX_DOCUMENT_SIZE_BYTES, } from "../../../utils/compress-pdf";
19
+ import { uploadFiles } from "../../../utils/upload-file";
20
+ import { emitStudentUsageChanged, onStudentUsageChanged, } from "../../../utils/student-usage";
21
+ import { fetchStudentUsage } from "../../../utils/student-usage-api";
20
22
  import { wakeStorageServer } from "../../../utils/wake-storage";
21
23
  import { normalizeIdNumber } from "../../../utils/id-number";
22
24
  import { isGovernmentIdType, } from "../../../utils/government-id";
@@ -24,7 +26,7 @@ import { toE164Phone } from "../../../utils/to-e164-phone";
24
26
  import { Filter, Plus } from "lucide-react";
25
27
  import { ADMISSION_DRAWER, ADMISSION_ACTION_TYPES, useAdmissionContext, } from "../context";
26
28
  export const useAdmissionModule = () => {
27
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
29
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
28
30
  // ============================================================================
29
31
  // 1.4.1 STATE & CORE HOOKS
30
32
  // ============================================================================
@@ -34,6 +36,7 @@ export const useAdmissionModule = () => {
34
36
  const t = useTranslations("admission");
35
37
  const { theme } = useTheme();
36
38
  const workspace = getCachedWorkspaceSync();
39
+ const [studentUsage, setStudentUsage] = useState(null);
37
40
  const listFetchNowRef = useRef(null);
38
41
  const debouncedQuery = useDebounce(state.searchQuery, 800);
39
42
  // Discount codes are school-scoped; fetch once (not per render) so both the
@@ -330,11 +333,9 @@ export const useAdmissionModule = () => {
330
333
  (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
331
334
  }
332
335
  }, [showToast, t]);
333
- const uploadAdmissionDocuments = useCallback(async (files, registrationCode, prefix) => {
334
- var _a;
335
- const schoolId = (_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id;
336
- if (!schoolId) {
337
- throw new Error("School context is not available");
336
+ const uploadAdmissionDocuments = useCallback(async (files) => {
337
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id)) {
338
+ throw new Error("Workspace context is not available");
338
339
  }
339
340
  const prepared = await Promise.all(files.map(async (file) => isPdfFile(file) ? await compressPdf(file) : file));
340
341
  for (const file of prepared) {
@@ -342,24 +343,20 @@ export const useAdmissionModule = () => {
342
343
  throw new DocumentTooLargeError(file.name, file.size);
343
344
  }
344
345
  }
345
- const payload = await Promise.all(prepared.map(async (file) => ({
346
- fileName: prefix ? `${prefix}-${file.name}` : file.name,
347
- contentType: file.type || "application/octet-stream",
348
- fileBase64: await fileToBase64(file),
349
- })));
350
- const { data, error } = await fetchData({
351
- url: DOCUMENT_UPLOAD_API_ROUTES.UNIT,
352
- method: API_METHODS.POST,
353
- headers: {
354
- "Content-Type": "application/json",
355
- },
356
- body: JSON.stringify({ schoolId, registrationCode, files: payload }),
346
+ const uploaded = await uploadFiles(prepared, {
347
+ workspaceId: workspace.id,
348
+ formName: "admission",
349
+ maxSizeBytes: MAX_DOCUMENT_SIZE_BYTES,
357
350
  });
358
- if (error || !(data === null || data === void 0 ? void 0 : data.urls)) {
359
- throw new Error("Upload failed");
360
- }
361
- return data.urls;
362
- }, [(_g = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _g === void 0 ? void 0 : _g.id]);
351
+ return uploaded.map((file) => file.url);
352
+ }, [workspace === null || workspace === void 0 ? void 0 : workspace.id]);
353
+ const refreshStudentUsage = useCallback(async () => {
354
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id))
355
+ return;
356
+ const usage = await fetchStudentUsage(workspace.id);
357
+ if (usage)
358
+ setStudentUsage(usage);
359
+ }, [workspace === null || workspace === void 0 ? void 0 : workspace.id]);
363
360
  // ============================================================================
364
361
  // 1.4.5 API HOOKS
365
362
  // ============================================================================
@@ -431,7 +428,7 @@ export const useAdmissionModule = () => {
431
428
  schoolId: (_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id,
432
429
  }),
433
430
  });
434
- }, [t, deleteFetchNow, (_h = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _h === void 0 ? void 0 : _h.id]);
431
+ }, [t, deleteFetchNow, (_g = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _g === void 0 ? void 0 : _g.id]);
435
432
  const handleRemoveDocument = useCallback(async (url) => {
436
433
  var _a;
437
434
  const next = (state.documents || []).filter((d) => d !== url);
@@ -461,7 +458,7 @@ export const useAdmissionModule = () => {
461
458
  }
462
459
  }, [
463
460
  dispatch,
464
- (_j = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _j === void 0 ? void 0 : _j.id,
461
+ (_h = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _h === void 0 ? void 0 : _h.id,
465
462
  state.documents,
466
463
  state.id,
467
464
  state.classForAdmission,
@@ -619,7 +616,7 @@ export const useAdmissionModule = () => {
619
616
  status,
620
617
  }),
621
618
  });
622
- }, [updateFetchNow, (_k = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _k === void 0 ? void 0 : _k.id]);
619
+ }, [updateFetchNow, (_j = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _j === void 0 ? void 0 : _j.id]);
623
620
  const handleApprove = useCallback((row) => {
624
621
  handleUpdateStatus(row, ADMISSION_STATUS.APPROVED, t("messagesApproveConfirmation"));
625
622
  }, [handleUpdateStatus, t]);
@@ -648,6 +645,8 @@ export const useAdmissionModule = () => {
648
645
  }),
649
646
  });
650
647
  (_b = listFetchNowRef.current) === null || _b === void 0 ? void 0 : _b.call(listFetchNowRef);
648
+ emitStudentUsageChanged();
649
+ await refreshStudentUsage();
651
650
  // `fetchData` returns the error body as `data` on failure — check the
652
651
  // error first so we never show both toasts.
653
652
  if (error) {
@@ -657,7 +656,7 @@ export const useAdmissionModule = () => {
657
656
  if (data) {
658
657
  showToast(t("messagesAdmitSuccess"), TOAST_VARIANT.SUCCESS);
659
658
  }
660
- }, [t, showToast, (_l = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _l === void 0 ? void 0 : _l.id]);
659
+ }, [t, showToast, (_k = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _k === void 0 ? void 0 : _k.id, refreshStudentUsage]);
661
660
  // ============================================================================
662
661
  // 1.4.7 NETWORK ACTIONS
663
662
  // ============================================================================
@@ -754,7 +753,7 @@ export const useAdmissionModule = () => {
754
753
  return;
755
754
  }
756
755
  showToast(t("messagesAnalyzeSuccess"), TOAST_VARIANT.SUCCESS);
757
- }, [(_m = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _m === void 0 ? void 0 : _m.id, t, showToast, byIdFetchNow, dispatch]);
756
+ }, [(_l = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _l === void 0 ? void 0 : _l.id, t, showToast, byIdFetchNow, dispatch]);
758
757
  const handleSubmit = useCallback(() => {
759
758
  dispatch({
760
759
  type: ADMISSION_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
@@ -769,25 +768,25 @@ export const useAdmissionModule = () => {
769
768
  const merged = state.documentsFiles.length
770
769
  ? [
771
770
  ...(state.documents || []),
772
- ...(await uploadAdmissionDocuments(state.documentsFiles, updateParams.registrationCode)),
771
+ ...(await uploadAdmissionDocuments(state.documentsFiles)),
773
772
  ]
774
773
  : state.documents || [];
775
774
  const studentIdImages = state.studentIdImagesFiles.length
776
775
  ? [
777
776
  ...(state.studentIdImages || []),
778
- ...(await uploadAdmissionDocuments(state.studentIdImagesFiles, updateParams.registrationCode, "student-id")),
777
+ ...(await uploadAdmissionDocuments(state.studentIdImagesFiles)),
779
778
  ]
780
779
  : state.studentIdImages || [];
781
780
  const fatherIdImages = state.fatherIdImagesFiles.length
782
781
  ? [
783
782
  ...(state.fatherIdImages || []),
784
- ...(await uploadAdmissionDocuments(state.fatherIdImagesFiles, updateParams.registrationCode, "father-id")),
783
+ ...(await uploadAdmissionDocuments(state.fatherIdImagesFiles)),
785
784
  ]
786
785
  : state.fatherIdImages || [];
787
786
  const motherIdImages = state.motherIdImagesFiles.length
788
787
  ? [
789
788
  ...(state.motherIdImages || []),
790
- ...(await uploadAdmissionDocuments(state.motherIdImagesFiles, updateParams.registrationCode, "mother-id")),
789
+ ...(await uploadAdmissionDocuments(state.motherIdImagesFiles)),
791
790
  ]
792
791
  : state.motherIdImages || [];
793
792
  const studentIdType = isGovernmentIdType(updateParams.studentIdType)
@@ -926,12 +925,12 @@ export const useAdmissionModule = () => {
926
925
  state.motherIdImages,
927
926
  state.motherIdImagesFiles,
928
927
  uploadAdmissionDocuments,
929
- (_o = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _o === void 0 ? void 0 : _o.id,
928
+ (_m = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _m === void 0 ? void 0 : _m.id,
930
929
  ]);
931
930
  // ============================================================================
932
931
  // 1.4.8 HEADER & ROW ACTIONS
933
932
  // ============================================================================
934
- const hasGeminiSecrets = Boolean(((_p = workspace === null || workspace === void 0 ? void 0 : workspace.secrets) === null || _p === void 0 ? void 0 : _p.GEMINI_API_KEY) && ((_q = workspace === null || workspace === void 0 ? void 0 : workspace.secrets) === null || _q === void 0 ? void 0 : _q.GEMINI_MODEL));
933
+ const hasGeminiSecrets = Boolean(((_o = workspace === null || workspace === void 0 ? void 0 : workspace.secrets) === null || _o === void 0 ? void 0 : _o.GEMINI_API_KEY) && ((_p = workspace === null || workspace === void 0 ? void 0 : workspace.secrets) === null || _p === void 0 ? void 0 : _p.GEMINI_MODEL));
935
934
  const headerActions = useMemo(() => [
936
935
  {
937
936
  enabled: true,
@@ -985,8 +984,10 @@ export const useAdmissionModule = () => {
985
984
  order: 5,
986
985
  },
987
986
  {
988
- enabled: (row) => (row === null || row === void 0 ? void 0 : row.status) === ADMISSION_STATUS.APPROVED,
989
- handleOnClick: handleAddStudent,
987
+ enabled: (row) => (row === null || row === void 0 ? void 0 : row.status) === ADMISSION_STATUS.APPROVED &&
988
+ !(studentUsage === null || studentUsage === void 0 ? void 0 : studentUsage.reached),
989
+ // eslint-disable-next-line react-hooks/refs -- invoked on click, not during render
990
+ handleOnClick: (row) => handleAddStudent(row),
990
991
  label: t("actionsButtonAddStudent"),
991
992
  order: 6,
992
993
  },
@@ -999,7 +1000,8 @@ export const useAdmissionModule = () => {
999
1000
  {
1000
1001
  enabled: (row) => hasGeminiSecrets &&
1001
1002
  (row === null || row === void 0 ? void 0 : row.status) === ADMISSION_STATUS.PENDING,
1002
- handleOnClick: handleAnalyze,
1003
+ // eslint-disable-next-line react-hooks/refs -- invoked on click, not during render
1004
+ handleOnClick: (row) => handleAnalyze(row),
1003
1005
  label: t("actionsButtonAnalyzeWithAi"),
1004
1006
  order: 8,
1005
1007
  },
@@ -1021,6 +1023,7 @@ export const useAdmissionModule = () => {
1021
1023
  handleAnalyze,
1022
1024
  handleDelete,
1023
1025
  hasGeminiSecrets,
1026
+ studentUsage === null || studentUsage === void 0 ? void 0 : studentUsage.reached,
1024
1027
  t,
1025
1028
  ]);
1026
1029
  const applyFilters = useCallback(() => {
@@ -1054,6 +1057,24 @@ export const useAdmissionModule = () => {
1054
1057
  useEffect(() => {
1055
1058
  listFetchNowRef.current = listFetchNow;
1056
1059
  }, [listFetchNow]);
1060
+ useEffect(() => {
1061
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id))
1062
+ return;
1063
+ let active = true;
1064
+ const load = async () => {
1065
+ const usage = await fetchStudentUsage(workspace.id);
1066
+ if (active && usage)
1067
+ setStudentUsage(usage);
1068
+ };
1069
+ void load();
1070
+ const unsubscribe = onStudentUsageChanged(() => {
1071
+ void load();
1072
+ });
1073
+ return () => {
1074
+ active = false;
1075
+ unsubscribe();
1076
+ };
1077
+ }, [workspace === null || workspace === void 0 ? void 0 : workspace.id]);
1057
1078
  useEffect(() => {
1058
1079
  var _a;
1059
1080
  if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id))
@@ -1092,6 +1113,8 @@ export const useAdmissionModule = () => {
1092
1113
  headerActions,
1093
1114
  listLoading,
1094
1115
  rowActions,
1116
+ studentUsage,
1117
+ refreshStudentUsage,
1095
1118
  updateLoading,
1096
1119
  uploadAdmissionDocuments });
1097
1120
  };
@@ -35,6 +35,7 @@ export declare const EXPENSE_ACTION_TYPES: {
35
35
  amount: number;
36
36
  approvedBy: string;
37
37
  attachments: string[];
38
+ attachmentsFiles: File[];
38
39
  category: EXPENSE_CATEGORY | "";
39
40
  description: string;
40
41
  enabled: boolean;
@@ -75,6 +76,7 @@ export declare const EXPENSE_ACTION_TYPES: {
75
76
  amount: number;
76
77
  approvedBy: string;
77
78
  attachments: string[];
79
+ attachmentsFiles: File[];
78
80
  category: EXPENSE_CATEGORY | "";
79
81
  description: string;
80
82
  enabled: boolean;
@@ -117,6 +119,7 @@ export declare const EXPENSE_ACTION_TYPES: {
117
119
  amount: number;
118
120
  approvedBy: string;
119
121
  attachments: string[];
122
+ attachmentsFiles: File[];
120
123
  category: EXPENSE_CATEGORY | "";
121
124
  description: string;
122
125
  enabled: boolean;
@@ -157,6 +160,7 @@ export declare const EXPENSE_ACTION_TYPES: {
157
160
  amount: number;
158
161
  approvedBy: string;
159
162
  attachments: string[];
163
+ attachmentsFiles: File[];
160
164
  category: EXPENSE_CATEGORY | "";
161
165
  description: string;
162
166
  enabled: boolean;
@@ -197,6 +201,7 @@ export declare const EXPENSE_ACTION_TYPES: {
197
201
  amount: number;
198
202
  approvedBy: string;
199
203
  attachments: string[];
204
+ attachmentsFiles: File[];
200
205
  category: EXPENSE_CATEGORY | "";
201
206
  description: string;
202
207
  enabled: boolean;
@@ -32,6 +32,7 @@ const expenseConfig = {
32
32
  amount: 0,
33
33
  approvedBy: "",
34
34
  attachments: [],
35
+ attachmentsFiles: [],
35
36
  category: "",
36
37
  description: "",
37
38
  enabled: true,
@@ -6,7 +6,7 @@ export declare const useExpenseModule: () => {
6
6
  clearSearch: () => void;
7
7
  closeDrawer: () => void;
8
8
  deleteLoading: boolean;
9
- handleChange: (field: string, value: string | number | boolean | string[] | undefined) => void;
9
+ handleChange: (field: string, value: string | number | boolean | string[] | File[] | undefined) => void;
10
10
  handleCreate: () => void;
11
11
  handleDelete: (row?: TableRow) => void;
12
12
  handleEdit: (row?: TableRow) => void;
@@ -39,6 +39,7 @@ export declare const useExpenseModule: () => {
39
39
  amount: number;
40
40
  approvedBy: string;
41
41
  attachments: string[];
42
+ attachmentsFiles: File[];
42
43
  category: import("../../../types").EXPENSE_CATEGORY | "";
43
44
  description: string;
44
45
  enabled: boolean;
@@ -5,6 +5,7 @@ import { useTranslations } from "next-intl";
5
5
  import { validateForm } from "@react-pakistan/util-functions/general/validate-form";
6
6
  import { generateThemeToast, TOAST_VARIANT, } from "@appcorp/shadcn/lib/toast-utils";
7
7
  import { getCachedWorkspaceSync } from "../../workspace/cache";
8
+ import { uploadFiles } from "../../../utils/upload-file";
8
9
  import { expenseFormValidation } from "../validate";
9
10
  import { EXPENSE_ACTION_TYPES, EXPENSE_DRAWER, useExpenseContext, } from "./shared";
10
11
  import { useExpenseApiContext } from "./expense-api-provider";
@@ -87,6 +88,7 @@ export const useExpenseModule = () => {
87
88
  amount: 0,
88
89
  approvedBy: "",
89
90
  attachments: [],
91
+ attachmentsFiles: [],
90
92
  category: "",
91
93
  description: "",
92
94
  enabled: true,
@@ -224,10 +226,43 @@ export const useExpenseModule = () => {
224
226
  validateForm({
225
227
  params: updateParams,
226
228
  schema: expenseFormValidation,
227
- successCallback: () => {
228
- updateFetchNow(undefined, {
229
- body: JSON.stringify(updateParams),
230
- });
229
+ successCallback: async () => {
230
+ try {
231
+ let attachments = state.attachments || [];
232
+ if (state.attachmentsFiles.length > 0) {
233
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id)) {
234
+ throw new Error("Workspace context is not available");
235
+ }
236
+ const uploaded = await uploadFiles(state.attachmentsFiles, {
237
+ workspaceId: workspace.id,
238
+ formName: "expense",
239
+ });
240
+ attachments = [
241
+ ...attachments.filter((url) => !url.startsWith("blob:")),
242
+ ...uploaded.map((file) => file.url),
243
+ ];
244
+ dispatch({
245
+ type: EXPENSE_ACTION_TYPES.SET_INPUT_FIELD,
246
+ payload: { key: "attachments", value: attachments },
247
+ });
248
+ dispatch({
249
+ type: EXPENSE_ACTION_TYPES.SET_INPUT_FIELD,
250
+ payload: { key: "attachmentsFiles", value: [] },
251
+ });
252
+ }
253
+ updateFetchNow(undefined, {
254
+ body: JSON.stringify(Object.assign(Object.assign({}, updateParams), { attachments })),
255
+ });
256
+ }
257
+ catch (error) {
258
+ dispatch({
259
+ type: EXPENSE_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
260
+ payload: { disabled: false },
261
+ });
262
+ showToast(error instanceof Error
263
+ ? error.message
264
+ : t("messagesAttachmentsUploadFailed"), TOAST_VARIANT.ERROR);
265
+ }
231
266
  },
232
267
  errorCallback: (errors) => {
233
268
  dispatch({
@@ -241,7 +276,16 @@ export const useExpenseModule = () => {
241
276
  showToast(t("messagesFormErrors"), TOAST_VARIANT.ERROR);
242
277
  },
243
278
  });
244
- }, [dispatch, updateParams, updateFetchNow, t, showToast]);
279
+ }, [
280
+ dispatch,
281
+ updateParams,
282
+ updateFetchNow,
283
+ t,
284
+ showToast,
285
+ state.attachments,
286
+ state.attachmentsFiles,
287
+ workspace === null || workspace === void 0 ? void 0 : workspace.id,
288
+ ]);
245
289
  // ============================================================================
246
290
  // 1.4.6 HEADER & ROW ACTIONS
247
291
  // ============================================================================
@@ -66,8 +66,10 @@ export const ExpenseForm = () => {
66
66
  value: opt.value,
67
67
  label: paymentMethodLabelMap[opt.value] || opt.label,
68
68
  })), placeholder: t("formPaymentMethodPlaceholder"), searchPlaceholder: t("formPaymentMethodPlaceholder"), value: paymentMethod || "" }), _jsx(EnhancedInput, { error: getTranslatedError({ errors, key: "paymentDate", t }), id: "paymentDate", info: t("formPaymentDateInfo"), label: t("formPaymentDateLabel"), onChange: (e) => handleChange("paymentDate", e.target.value), onClick: (e) => { var _a, _b; return (_b = (_a = e.currentTarget).showPicker) === null || _b === void 0 ? void 0 : _b.call(_a); }, type: "date", value: paymentDate || "" }), _jsx(EnhancedInput, { error: getTranslatedError({ errors, key: "receiptNumber", t }), id: "receiptNumber", info: t("formReceiptNumberInfo"), label: t("formReceiptNumberLabel"), maxLength: 100, onChange: (e) => handleChange("receiptNumber", e.target.value), placeholder: t("formReceiptNumberPlaceholder"), type: "text", value: receiptNumber || "" }), _jsx(EnhancedInput, { error: getTranslatedError({ errors, key: "transactionId", t }), id: "transactionId", info: t("formTransactionIdInfo"), label: t("formTransactionIdLabel"), maxLength: 100, onChange: (e) => handleChange("transactionId", e.target.value), placeholder: t("formTransactionIdPlaceholder"), type: "text", value: transactionId || "" })] })) : null, _jsx(EnhancedTextarea, { error: getTranslatedError({ errors, key: "remarks", t }), id: "remarks", label: t("formRemarksLabel"), maxLength: 1000, onChange: (e) => handleChange("remarks", e.target.value), placeholder: t("formRemarksPlaceholder"), rows: 3, value: remarks || "" }), _jsx(EnhancedDropzone, { accept: ["image/*", "application/pdf"], error: getTranslatedError({ errors, key: "attachments", t }), id: "attachments", info: t("formAttachmentsInfo"), label: t("formAttachmentsLabel"), maxFiles: 10, onChange: (files) => {
69
- const urls = files.map((f) => URL.createObjectURL(f));
70
- handleChange("attachments", urls);
69
+ const remoteUrls = (attachments || []).filter((url) => !url.startsWith("blob:"));
70
+ const previewUrls = files.map((file) => URL.createObjectURL(file));
71
+ handleChange("attachments", [...remoteUrls, ...previewUrls]);
72
+ handleChange("attachmentsFiles", files);
71
73
  }, onRemoveRemote: (url) => {
72
74
  const newList = (attachments || []).filter((a) => a !== url);
73
75
  handleChange("attachments", newList);
@@ -23,7 +23,8 @@ export declare const SCHOOL_ACTION_TYPES: {
23
23
  email: null;
24
24
  enabled: boolean;
25
25
  id: string;
26
- logo: null;
26
+ logo: string | null;
27
+ logoFile: File | null;
27
28
  name: string;
28
29
  phone: null;
29
30
  postalCode: null;
@@ -43,7 +44,8 @@ export declare const SCHOOL_ACTION_TYPES: {
43
44
  email: null;
44
45
  enabled: boolean;
45
46
  id: string;
46
- logo: null;
47
+ logo: string | null;
48
+ logoFile: File | null;
47
49
  name: string;
48
50
  phone: null;
49
51
  postalCode: null;
@@ -65,7 +67,8 @@ export declare const SCHOOL_ACTION_TYPES: {
65
67
  email: null;
66
68
  enabled: boolean;
67
69
  id: string;
68
- logo: null;
70
+ logo: string | null;
71
+ logoFile: File | null;
69
72
  name: string;
70
73
  phone: null;
71
74
  postalCode: null;
@@ -85,7 +88,8 @@ export declare const SCHOOL_ACTION_TYPES: {
85
88
  email: null;
86
89
  enabled: boolean;
87
90
  id: string;
88
- logo: null;
91
+ logo: string | null;
92
+ logoFile: File | null;
89
93
  name: string;
90
94
  phone: null;
91
95
  postalCode: null;
@@ -105,7 +109,8 @@ export declare const SCHOOL_ACTION_TYPES: {
105
109
  email: null;
106
110
  enabled: boolean;
107
111
  id: string;
108
- logo: null;
112
+ logo: string | null;
113
+ logoFile: File | null;
109
114
  name: string;
110
115
  phone: null;
111
116
  postalCode: null;
@@ -119,7 +124,7 @@ export declare const SCHOOL_ACTION_TYPES: {
119
124
  }>;
120
125
  export declare const useSchoolModule: () => {
121
126
  closeDrawer: () => void;
122
- handleChange: (field: string, value: string | number | boolean | undefined | Record<string, string>) => void;
127
+ handleChange: (field: string, value: string | number | boolean | File | null | undefined | Record<string, string>) => void;
123
128
  handleEdit: () => void;
124
129
  handleSubmit: () => void;
125
130
  handleView: () => void;
@@ -143,7 +148,8 @@ export declare const useSchoolModule: () => {
143
148
  email: null;
144
149
  enabled: boolean;
145
150
  id: string;
146
- logo: null;
151
+ logo: string | null;
152
+ logoFile: File | null;
147
153
  name: string;
148
154
  phone: null;
149
155
  postalCode: null;
@@ -30,6 +30,7 @@ import { invalidateSchoolCache } from "./cache";
30
30
  import { schoolFormValidation } from "./validate";
31
31
  import { getCachedWorkspaceSync, setCachedWorkspace } from "../workspace/cache";
32
32
  import { toE164Phone } from "../../utils/to-e164-phone";
33
+ import { uploadFile } from "../../utils/upload-file";
33
34
  // ============================================================================
34
35
  // 1.1 DRAWER TYPES
35
36
  // ============================================================================
@@ -55,6 +56,7 @@ const schoolConfig = {
55
56
  enabled: true,
56
57
  id: "",
57
58
  logo: null,
59
+ logoFile: null,
58
60
  name: "",
59
61
  phone: null,
60
62
  postalCode: null,
@@ -293,10 +295,41 @@ export const useSchoolModule = () => {
293
295
  validateForm({
294
296
  params: updateParams,
295
297
  schema: schoolFormValidation,
296
- successCallback: () => {
297
- updateFetchNow(undefined, {
298
- body: JSON.stringify(updateParams),
299
- });
298
+ successCallback: async () => {
299
+ try {
300
+ let logo = updateParams.logo;
301
+ if (state.logoFile) {
302
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id)) {
303
+ throw new Error("Workspace context is not available");
304
+ }
305
+ const uploaded = await uploadFile(state.logoFile, {
306
+ workspaceId: workspace.id,
307
+ formName: "school",
308
+ maxSizeBytes: 2 * 1024 * 1024,
309
+ });
310
+ logo = uploaded.url;
311
+ dispatch({
312
+ type: SCHOOL_ACTION_TYPES.SET_INPUT_FIELD,
313
+ payload: { key: "logo", value: logo },
314
+ });
315
+ dispatch({
316
+ type: SCHOOL_ACTION_TYPES.SET_INPUT_FIELD,
317
+ payload: { key: "logoFile", value: null },
318
+ });
319
+ }
320
+ updateFetchNow(undefined, {
321
+ body: JSON.stringify(Object.assign(Object.assign({}, updateParams), { logo })),
322
+ });
323
+ }
324
+ catch (error) {
325
+ dispatch({
326
+ type: SCHOOL_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
327
+ payload: { disableSaveButton: false },
328
+ });
329
+ showToast(error instanceof Error
330
+ ? error.message
331
+ : t("messagesLogoUploadFailed"), TOAST_VARIANT.ERROR);
332
+ }
300
333
  },
301
334
  errorCallback: (errors) => {
302
335
  dispatch({
@@ -310,7 +343,15 @@ export const useSchoolModule = () => {
310
343
  showToast(t("messagesFormErrors"), TOAST_VARIANT.ERROR);
311
344
  },
312
345
  });
313
- }, [dispatch, showToast, t, updateFetchNow, updateParams]);
346
+ }, [
347
+ dispatch,
348
+ showToast,
349
+ t,
350
+ updateFetchNow,
351
+ updateParams,
352
+ state.logoFile,
353
+ workspace === null || workspace === void 0 ? void 0 : workspace.id,
354
+ ]);
314
355
  // ============================================================================
315
356
  // 1.4.8 HEADER & ROW ACTIONS
316
357
  // ============================================================================
@@ -7,6 +7,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
7
7
  */
8
8
  import { EnhancedInput } from "@appcorp/shadcn/components/enhanced-input";
9
9
  import { EnhancedCheckbox } from "@appcorp/shadcn/components/enhanced-checkbox";
10
+ import { EnhancedDropzone } from "@appcorp/shadcn/components/enhanced-dropzone";
10
11
  import { EnhancedCombobox } from "@appcorp/shadcn/components/enhanced-combobox";
11
12
  import { useSchoolModule } from "./context";
12
13
  import { CURRENCY } from "../../type";
@@ -39,5 +40,14 @@ export const SchoolForm = () => {
39
40
  // searchPlaceholder: t("formPrincipalSearchPlaceholder"),
40
41
  // value: principalId || "",
41
42
  // });
42
- return (_jsxs("div", { className: "space-y-6", children: [_jsxs("div", { className: "grid grid-cols-1 gap-4", children: [_jsx(EnhancedInput, { id: "name", label: t("formNameLabel"), value: name, onChange: (e) => handleChange("name", e.target.value), error: getTranslatedError({ errors, key: "name", t }), placeholder: t("formNamePlaceholder"), required: true }), _jsx(EnhancedInput, { id: "code", label: t("formCodeLabel"), value: code, onChange: (e) => handleChange("code", e.target.value), error: getTranslatedError({ errors, key: "code", t }), placeholder: t("formCodePlaceholder"), required: true, info: t("formCodeInfo") }), currencyCombo, _jsx(EnhancedInput, { id: "address", label: t("formAddressLabel"), value: address || "", onChange: (e) => handleChange("address", e.target.value), error: getTranslatedError({ errors, key: "address", t }), placeholder: t("formAddressPlaceholder") }), _jsx(EnhancedInput, { id: "city", label: t("formCityLabel"), value: city || "", onChange: (e) => handleChange("city", e.target.value), error: getTranslatedError({ errors, key: "city", t }), placeholder: t("formCityPlaceholder") }), _jsx(EnhancedInput, { id: "state", label: t("formStateLabel"), value: stateField || "", onChange: (e) => handleChange("state", e.target.value), error: getTranslatedError({ errors, key: "state", t }), placeholder: t("formStatePlaceholder") }), _jsx(EnhancedInput, { id: "country", label: t("formCountryLabel"), value: country || "", onChange: (e) => handleChange("country", e.target.value), error: getTranslatedError({ errors, key: "country", t }), placeholder: t("formCountryPlaceholder") }), _jsx(EnhancedInput, { id: "postalCode", label: t("formPostalCodeLabel"), value: postalCode || "", onChange: (e) => handleChange("postalCode", e.target.value), error: getTranslatedError({ errors, key: "postalCode", t }), placeholder: t("formPostalCodePlaceholder") }), _jsx(PhoneInput, { id: "phone", label: t("formPhoneLabel"), value: phone || "", onChange: (value) => handleChange("phone", value), error: getTranslatedError({ errors, key: "phone", t }), placeholder: t("formPhonePlaceholder"), defaultCountry: country || "PK" }), _jsx(EnhancedInput, { id: "email", label: t("formEmailLabel"), value: email || "", onChange: (e) => handleChange("email", e.target.value), error: getTranslatedError({ errors, key: "email", t }), placeholder: t("formEmailPlaceholder"), type: "email" })] }), _jsx(EnhancedInput, { id: "website", label: t("formWebsiteLabel"), value: website || "", onChange: (e) => handleChange("website", e.target.value), error: getTranslatedError({ errors, key: "website", t }), placeholder: t("formWebsitePlaceholder"), type: "url" }), _jsx(EnhancedInput, { id: "logo", label: t("formLogoLabel"), value: logo || "", onChange: (e) => handleChange("logo", e.target.value), error: getTranslatedError({ errors, key: "logo", t }), placeholder: t("formLogoPlaceholder"), type: "url" }), _jsx(EnhancedCheckbox, { id: "enabled", label: t("formEnabledLabel"), checked: enabled, error: getTranslatedError({ errors, key: "enabled", t }), onCheckedChange: (checked) => handleChange("enabled", checked), info: t("formEnabledInfo") })] }));
43
+ return (_jsxs("div", { className: "space-y-6", children: [_jsxs("div", { className: "grid grid-cols-1 gap-4", children: [_jsx(EnhancedInput, { id: "name", label: t("formNameLabel"), value: name, onChange: (e) => handleChange("name", e.target.value), error: getTranslatedError({ errors, key: "name", t }), placeholder: t("formNamePlaceholder"), required: true }), _jsx(EnhancedInput, { id: "code", label: t("formCodeLabel"), value: code, onChange: (e) => handleChange("code", e.target.value), error: getTranslatedError({ errors, key: "code", t }), placeholder: t("formCodePlaceholder"), required: true, info: t("formCodeInfo") }), currencyCombo, _jsx(EnhancedInput, { id: "address", label: t("formAddressLabel"), value: address || "", onChange: (e) => handleChange("address", e.target.value), error: getTranslatedError({ errors, key: "address", t }), placeholder: t("formAddressPlaceholder") }), _jsx(EnhancedInput, { id: "city", label: t("formCityLabel"), value: city || "", onChange: (e) => handleChange("city", e.target.value), error: getTranslatedError({ errors, key: "city", t }), placeholder: t("formCityPlaceholder") }), _jsx(EnhancedInput, { id: "state", label: t("formStateLabel"), value: stateField || "", onChange: (e) => handleChange("state", e.target.value), error: getTranslatedError({ errors, key: "state", t }), placeholder: t("formStatePlaceholder") }), _jsx(EnhancedInput, { id: "country", label: t("formCountryLabel"), value: country || "", onChange: (e) => handleChange("country", e.target.value), error: getTranslatedError({ errors, key: "country", t }), placeholder: t("formCountryPlaceholder") }), _jsx(EnhancedInput, { id: "postalCode", label: t("formPostalCodeLabel"), value: postalCode || "", onChange: (e) => handleChange("postalCode", e.target.value), error: getTranslatedError({ errors, key: "postalCode", t }), placeholder: t("formPostalCodePlaceholder") }), _jsx(PhoneInput, { id: "phone", label: t("formPhoneLabel"), value: phone || "", onChange: (value) => handleChange("phone", value), error: getTranslatedError({ errors, key: "phone", t }), placeholder: t("formPhonePlaceholder"), defaultCountry: country || "PK" }), _jsx(EnhancedInput, { id: "email", label: t("formEmailLabel"), value: email || "", onChange: (e) => handleChange("email", e.target.value), error: getTranslatedError({ errors, key: "email", t }), placeholder: t("formEmailPlaceholder"), type: "email" })] }), _jsx(EnhancedInput, { id: "website", label: t("formWebsiteLabel"), value: website || "", onChange: (e) => handleChange("website", e.target.value), error: getTranslatedError({ errors, key: "website", t }), placeholder: t("formWebsitePlaceholder"), type: "url" }), _jsx(EnhancedDropzone, { accept: ["image/*"], error: getTranslatedError({ errors, key: "logo", t }), id: "logo", info: t("formLogoInfo"), label: t("formLogoLabel"), maxFiles: 1, maxSize: 2 * 1024 * 1024, onChange: (files) => {
44
+ if (!files.length)
45
+ return;
46
+ const file = files[0];
47
+ handleChange("logo", URL.createObjectURL(file));
48
+ handleChange("logoFile", file);
49
+ }, onRemoveRemote: () => {
50
+ handleChange("logo", null);
51
+ handleChange("logoFile", null);
52
+ }, value: logo ? [logo] : [] }), _jsx(EnhancedCheckbox, { id: "enabled", label: t("formEnabledLabel"), checked: enabled, error: getTranslatedError({ errors, key: "enabled", t }), onCheckedChange: (checked) => handleChange("enabled", checked), info: t("formEnabledInfo") })] }));
43
53
  };