@appcorp/fusion-storybook 0.2.12 → 0.2.14

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 (173) hide show
  1. package/base-modules/admission/context/use-admission-module.d.ts +88 -0
  2. package/base-modules/admission/context/use-admission-module.js +771 -0
  3. package/base-modules/admission/context.d.ts +1 -87
  4. package/base-modules/admission/context.js +3 -769
  5. package/base-modules/admission/filter.d.ts +1 -1
  6. package/base-modules/admission/form.d.ts +1 -1
  7. package/base-modules/admission/more-actions.d.ts +1 -1
  8. package/base-modules/admission/page.d.ts +8 -1
  9. package/base-modules/admission/page.js +25 -4
  10. package/base-modules/admission/view.d.ts +1 -1
  11. package/base-modules/attendance/form.d.ts +1 -1
  12. package/base-modules/attendance/page.d.ts +8 -1
  13. package/base-modules/attendance/page.js +25 -4
  14. package/base-modules/attendance/view.d.ts +1 -1
  15. package/base-modules/campus/filter.d.ts +1 -1
  16. package/base-modules/campus/form.d.ts +1 -1
  17. package/base-modules/campus/more-actions.d.ts +1 -1
  18. package/base-modules/campus/more-actions.js +0 -1
  19. package/base-modules/campus/page.d.ts +8 -1
  20. package/base-modules/campus/page.js +25 -4
  21. package/base-modules/campus/view.d.ts +1 -1
  22. package/base-modules/class/filter.d.ts +1 -1
  23. package/base-modules/class/form.d.ts +1 -1
  24. package/base-modules/class/more-actions.d.ts +1 -1
  25. package/base-modules/class/more-actions.js +1 -6
  26. package/base-modules/class/page.d.ts +8 -1
  27. package/base-modules/class/page.js +25 -4
  28. package/base-modules/class/view.d.ts +1 -1
  29. package/base-modules/course/filter.d.ts +1 -1
  30. package/base-modules/course/form.d.ts +1 -1
  31. package/base-modules/course/more-actions.d.ts +1 -1
  32. package/base-modules/course/page.d.ts +8 -1
  33. package/base-modules/course/page.js +25 -4
  34. package/base-modules/course/view.d.ts +1 -1
  35. package/base-modules/discount-code/filter.d.ts +2 -1
  36. package/base-modules/discount-code/form.d.ts +1 -1
  37. package/base-modules/discount-code/page.d.ts +8 -1
  38. package/base-modules/discount-code/page.js +25 -4
  39. package/base-modules/discount-code/view.d.ts +1 -1
  40. package/base-modules/enrollment/filter.d.ts +1 -1
  41. package/base-modules/enrollment/form.d.ts +1 -1
  42. package/base-modules/enrollment/more-actions.d.ts +1 -1
  43. package/base-modules/enrollment/page.d.ts +8 -1
  44. package/base-modules/enrollment/page.js +25 -4
  45. package/base-modules/enrollment/view.d.ts +1 -1
  46. package/base-modules/expense/filter.d.ts +1 -1
  47. package/base-modules/expense/form.d.ts +1 -1
  48. package/base-modules/expense/more-actions.d.ts +1 -1
  49. package/base-modules/expense/more-actions.js +1 -6
  50. package/base-modules/expense/page.d.ts +8 -1
  51. package/base-modules/expense/page.js +25 -4
  52. package/base-modules/expense/view.d.ts +1 -1
  53. package/base-modules/family/filter.d.ts +1 -1
  54. package/base-modules/family/form.d.ts +1 -1
  55. package/base-modules/family/more-actions.d.ts +1 -1
  56. package/base-modules/family/page.d.ts +8 -1
  57. package/base-modules/family/page.js +25 -4
  58. package/base-modules/family/view.d.ts +1 -1
  59. package/base-modules/family-member/filter.d.ts +1 -1
  60. package/base-modules/family-member/form.d.ts +1 -1
  61. package/base-modules/family-member/more-actions.d.ts +1 -1
  62. package/base-modules/family-member/page.d.ts +8 -1
  63. package/base-modules/family-member/page.js +25 -4
  64. package/base-modules/family-member/view.d.ts +1 -1
  65. package/base-modules/fee-structure/filter.d.ts +1 -1
  66. package/base-modules/fee-structure/form.d.ts +1 -1
  67. package/base-modules/fee-structure/more-actions.d.ts +1 -1
  68. package/base-modules/fee-structure/more-actions.js +1 -6
  69. package/base-modules/fee-structure/page.d.ts +8 -1
  70. package/base-modules/fee-structure/page.js +25 -4
  71. package/base-modules/fee-structure/view.d.ts +1 -1
  72. package/base-modules/rbac/assign-permissions.d.ts +1 -1
  73. package/base-modules/rbac/filter.d.ts +1 -1
  74. package/base-modules/rbac/form.d.ts +1 -1
  75. package/base-modules/rbac/more-actions.d.ts +1 -1
  76. package/base-modules/rbac/page.d.ts +8 -1
  77. package/base-modules/rbac/page.js +25 -4
  78. package/base-modules/rbac/view.d.ts +7 -1
  79. package/base-modules/school/drawer.d.ts +1 -1
  80. package/base-modules/school/filter.d.ts +1 -1
  81. package/base-modules/school/form.d.ts +1 -1
  82. package/base-modules/school/more-actions.d.ts +1 -1
  83. package/base-modules/school/view.d.ts +1 -1
  84. package/base-modules/section/filter.d.ts +1 -1
  85. package/base-modules/section/form.d.ts +1 -1
  86. package/base-modules/section/more-actions.d.ts +1 -1
  87. package/base-modules/section/page.d.ts +8 -1
  88. package/base-modules/section/page.js +25 -4
  89. package/base-modules/section/view.d.ts +1 -1
  90. package/base-modules/student-fee/context/shared.d.ts +178 -0
  91. package/base-modules/student-fee/context/shared.js +59 -0
  92. package/base-modules/student-fee/context/use-student-fee-module.d.ts +64 -0
  93. package/base-modules/student-fee/context/use-student-fee-module.js +610 -0
  94. package/base-modules/student-fee/context.d.ts +21 -235
  95. package/base-modules/student-fee/context.js +2 -674
  96. package/base-modules/student-fee/filter.d.ts +1 -1
  97. package/base-modules/student-fee/form.d.ts +1 -1
  98. package/base-modules/student-fee/more-actions.d.ts +1 -1
  99. package/base-modules/student-fee/more-actions.js +1 -6
  100. package/base-modules/student-fee/page.d.ts +8 -1
  101. package/base-modules/student-fee/page.js +25 -4
  102. package/base-modules/student-fee/view.d.ts +1 -1
  103. package/base-modules/student-profile/context/module-base.d.ts +187 -0
  104. package/base-modules/student-profile/context/module-base.js +50 -0
  105. package/base-modules/student-profile/context/use-student-profile-module.d.ts +70 -0
  106. package/base-modules/student-profile/context/use-student-profile-module.js +506 -0
  107. package/base-modules/student-profile/context.d.ts +20 -256
  108. package/base-modules/student-profile/context.js +2 -601
  109. package/base-modules/student-profile/filter.d.ts +1 -1
  110. package/base-modules/student-profile/form.d.ts +1 -1
  111. package/base-modules/student-profile/more-actions.d.ts +1 -1
  112. package/base-modules/student-profile/page.d.ts +8 -1
  113. package/base-modules/student-profile/page.js +25 -4
  114. package/base-modules/student-profile/view.d.ts +1 -1
  115. package/base-modules/subject/filter.d.ts +1 -1
  116. package/base-modules/subject/form.d.ts +1 -1
  117. package/base-modules/subject/more-actions.d.ts +1 -1
  118. package/base-modules/subject/page.d.ts +8 -1
  119. package/base-modules/subject/page.js +25 -4
  120. package/base-modules/subject/view.d.ts +1 -1
  121. package/base-modules/teacher/avatar-upload.js +1 -4
  122. package/base-modules/teacher/filter.d.ts +1 -1
  123. package/base-modules/teacher/form.d.ts +1 -1
  124. package/base-modules/teacher/more-actions.d.ts +1 -1
  125. package/base-modules/teacher/more-actions.js +1 -6
  126. package/base-modules/teacher/page.d.ts +8 -1
  127. package/base-modules/teacher/page.js +25 -4
  128. package/base-modules/teacher/view.d.ts +1 -1
  129. package/base-modules/user/context/use-user-module.d.ts +53 -0
  130. package/base-modules/user/context/use-user-module.js +546 -0
  131. package/base-modules/user/context.d.ts +1 -52
  132. package/base-modules/user/context.js +5 -547
  133. package/base-modules/user/drawer.d.ts +1 -1
  134. package/base-modules/user/filter.d.ts +1 -1
  135. package/base-modules/user/form.d.ts +1 -1
  136. package/base-modules/user/more-actions.d.ts +1 -1
  137. package/base-modules/user/page.d.ts +8 -1
  138. package/base-modules/user/page.js +25 -4
  139. package/base-modules/user/view.d.ts +1 -1
  140. package/base-modules/workspace/context.d.ts +1 -1
  141. package/base-modules/workspace/drawer.d.ts +1 -1
  142. package/base-modules/workspace/filter.d.ts +1 -1
  143. package/base-modules/workspace/form.d.ts +1 -1
  144. package/base-modules/workspace/more-actions.d.ts +1 -1
  145. package/base-modules/workspace/view.d.ts +1 -1
  146. package/base-modules/workspace-user/filter.d.ts +1 -1
  147. package/base-modules/workspace-user/form.d.ts +1 -1
  148. package/base-modules/workspace-user/more-actions.d.ts +1 -1
  149. package/base-modules/workspace-user/more-actions.js +1 -6
  150. package/base-modules/workspace-user/page.d.ts +8 -1
  151. package/base-modules/workspace-user/page.js +25 -4
  152. package/base-modules/workspace-user/view.d.ts +1 -1
  153. package/components/module-error.d.ts +9 -0
  154. package/components/module-error.js +3 -0
  155. package/components/timeline.d.ts +1 -1
  156. package/package.json +6 -5
  157. package/tsconfig.build.tsbuildinfo +1 -1
  158. package/type.d.ts +3 -1242
  159. package/type.js +3 -445
  160. package/types/academics.d.ts +264 -0
  161. package/types/academics.js +8 -0
  162. package/types/admission.d.ts +85 -0
  163. package/types/admission.js +6 -0
  164. package/types/communication.d.ts +165 -0
  165. package/types/communication.js +7 -0
  166. package/types/enums.d.ts +411 -0
  167. package/types/enums.js +442 -0
  168. package/types/finance.d.ts +126 -0
  169. package/types/finance.js +7 -0
  170. package/types/index.d.ts +12 -0
  171. package/types/index.js +12 -0
  172. package/types/user-management.d.ts +236 -0
  173. package/types/user-management.js +7 -0
@@ -0,0 +1,546 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useMemo, useRef } from "react";
3
+ import { useTheme } from "next-themes";
4
+ import { useTranslations } from "next-intl";
5
+ import { isCreatedOrUpdated, validateForm, API_METHODS, fetchData, } from "@react-pakistan/util-functions";
6
+ import { useModuleEntityV2, } from "@react-pakistan/util-functions/hooks/use-module-entity-v2";
7
+ import { useDebounce } from "@react-pakistan/util-functions/hooks/use-debounce";
8
+ import { generateThemeToast, TOAST_VARIANT, } from "@appcorp/shadcn/lib/toast-utils";
9
+ import { USER_API_ROUTES, pageLimit } from "../constants";
10
+ import { userFormValidation } from "../validate";
11
+ import { getCachedUsers, invalidateUsersCache } from "../cache";
12
+ import { getCachedWorkspaceSync } from "../../workspace/cache";
13
+ import { invalidateWorkspaceUsersCache } from "../../workspace-user/cache";
14
+ import { blobToWebP } from "webp-converter-browser";
15
+ import { supabasePublicStorageUrl } from "../../../constants";
16
+ import { toastNetworkError } from "../../../utils/toast-network-error";
17
+ import { formatNumber } from "@react-pakistan/util-functions/general/format-number";
18
+ import { formatPhoneDisplay } from "@react-pakistan/util-functions/general/format-phone-display";
19
+ import { useUserContext, USER_ACTION_TYPES, USER_DRAWER } from "../context";
20
+ export const useUserModule = () => {
21
+ // ============================================================================
22
+ // 1.4.1 STATE & CORE HOOKS
23
+ // ============================================================================
24
+ const context = useUserContext();
25
+ const { dispatch } = context;
26
+ const state = context.state;
27
+ const t = useTranslations("user");
28
+ const { theme } = useTheme();
29
+ const workspace = getCachedWorkspaceSync();
30
+ const listFetchNowRef = useRef(null);
31
+ const debouncedQuery = useDebounce(state.searchQuery, 800);
32
+ // ============================================================================
33
+ // 1.4.2 API PARAMETERS
34
+ // ============================================================================
35
+ const listParams = useMemo(() => (Object.assign(Object.assign({ currentPage: state.currentPage, pageLimit: state.pageLimit, workspaceId: (workspace === null || workspace === void 0 ? void 0 : workspace.id) || "" }, (debouncedQuery ? { searchQuery: debouncedQuery } : {})), (state.filterEnabled !== undefined
36
+ ? { filterEnabled: String(state.filterEnabled) }
37
+ : {}))), [
38
+ state.currentPage,
39
+ state.filterEnabled,
40
+ state.pageLimit,
41
+ debouncedQuery,
42
+ workspace === null || workspace === void 0 ? void 0 : workspace.id,
43
+ ]);
44
+ const updateParams = useMemo(() => ({
45
+ avatar: state.avatar,
46
+ email: (state.email || "").trim(),
47
+ enabled: state.enabled,
48
+ id: state.id,
49
+ name: (state.name || "").trim(),
50
+ password: state.password,
51
+ phone: formatNumber(state.phone),
52
+ userRole: state.userRole,
53
+ workspaceId: workspace === null || workspace === void 0 ? void 0 : workspace.id,
54
+ }), [state, workspace === null || workspace === void 0 ? void 0 : workspace.id]);
55
+ const byIdParams = useMemo(() => ({ id: state.id }), [state.id]);
56
+ const deleteParams = useMemo(() => ({ id: state.id }), [state.id]);
57
+ const isDefaultListState = state.currentPage === 1 &&
58
+ state.pageLimit === pageLimit &&
59
+ !debouncedQuery &&
60
+ state.filterEnabled === undefined;
61
+ // ============================================================================
62
+ // 1.4.3 UTILITIES
63
+ // ============================================================================
64
+ const showToast = useCallback((description, variant) => {
65
+ generateThemeToast({
66
+ description,
67
+ theme: theme,
68
+ variant,
69
+ });
70
+ }, [theme]);
71
+ const resetFormAndCloseDrawer = useCallback(() => {
72
+ dispatch({ type: USER_ACTION_TYPES.RESET_FORM });
73
+ dispatch({
74
+ type: USER_ACTION_TYPES.SET_ERRORS,
75
+ payload: { errors: {} },
76
+ });
77
+ dispatch({
78
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
79
+ payload: { disabled: false },
80
+ });
81
+ dispatch({
82
+ type: USER_ACTION_TYPES.SET_DRAWER,
83
+ payload: { drawer: null },
84
+ });
85
+ }, [dispatch]);
86
+ // ============================================================================
87
+ // 1.4.4 API CALLBACKS
88
+ // ============================================================================
89
+ const listCallback = useCallback(({ data, error }) => {
90
+ var _a, _b;
91
+ if (error) {
92
+ showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
93
+ return;
94
+ }
95
+ if (data) {
96
+ const response = data;
97
+ dispatch({
98
+ type: USER_ACTION_TYPES.SET_ITEMS,
99
+ payload: { items: (_a = response.items) !== null && _a !== void 0 ? _a : [], count: (_b = response.count) !== null && _b !== void 0 ? _b : 0 },
100
+ });
101
+ }
102
+ }, [dispatch, showToast, t]);
103
+ const updateCallback = useCallback(({ data, error }) => {
104
+ var _a;
105
+ if (error) {
106
+ showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
107
+ return;
108
+ }
109
+ if (data) {
110
+ const isCreated = isCreatedOrUpdated(data);
111
+ invalidateUsersCache();
112
+ invalidateWorkspaceUsersCache();
113
+ showToast(isCreated ? t("messagesUserCreated") : t("messagesUserUpdated"), TOAST_VARIANT.SUCCESS);
114
+ resetFormAndCloseDrawer();
115
+ (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
116
+ }
117
+ }, [resetFormAndCloseDrawer, showToast, t]);
118
+ const byIdCallback = useCallback(({ data, error }) => {
119
+ var _a;
120
+ if (error) {
121
+ showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
122
+ return;
123
+ }
124
+ if (data) {
125
+ const user = data;
126
+ const maybeWithWorkspaces = data;
127
+ const wsEntry = (_a = maybeWithWorkspaces.workspaces) === null || _a === void 0 ? void 0 : _a.find((ws) => ws.workspaceId === (workspace === null || workspace === void 0 ? void 0 : workspace.id));
128
+ let roleValue = null;
129
+ if (wsEntry && wsEntry.role !== undefined && wsEntry.role !== null) {
130
+ const r = wsEntry.role;
131
+ if (typeof r === "string") {
132
+ roleValue = r;
133
+ }
134
+ else if (typeof r === "object" && r !== null) {
135
+ roleValue =
136
+ r.userRole ||
137
+ r.name ||
138
+ null;
139
+ }
140
+ }
141
+ const parsedUserRole = roleValue || null;
142
+ dispatch({
143
+ type: USER_ACTION_TYPES.SET_FORM_DATA,
144
+ payload: {
145
+ form: {
146
+ id: user.id,
147
+ name: user.name || "",
148
+ email: user.email,
149
+ phone: formatPhoneDisplay(user.phone) || "",
150
+ avatar: user.avatar || "",
151
+ enabled: user.enabled,
152
+ userRole: parsedUserRole,
153
+ password: "",
154
+ },
155
+ },
156
+ });
157
+ }
158
+ }, [dispatch, showToast, t, workspace === null || workspace === void 0 ? void 0 : workspace.id]);
159
+ const deleteCallback = useCallback(({ data, error }) => {
160
+ var _a;
161
+ if (error) {
162
+ showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
163
+ return;
164
+ }
165
+ if (data) {
166
+ invalidateUsersCache();
167
+ invalidateWorkspaceUsersCache();
168
+ showToast(t("messagesUserDeleted"), TOAST_VARIANT.SUCCESS);
169
+ (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
170
+ }
171
+ }, [showToast, t]);
172
+ // ============================================================================
173
+ // 1.4.5 API HOOKS
174
+ // ============================================================================
175
+ const { byIdFetchNow, byIdLoading, deleteFetchNow, deleteLoading, listFetchNow, listLoading, updateFetchNow, updateLoading, } = useModuleEntityV2({
176
+ byIdCallback,
177
+ byIdParams,
178
+ deleteCallback,
179
+ deleteParams,
180
+ listCallback,
181
+ listParams,
182
+ listUrl: USER_API_ROUTES.LIST,
183
+ searchQuery: debouncedQuery,
184
+ unitByIdUrl: USER_API_ROUTES.UNIT,
185
+ unitUrl: USER_API_ROUTES.UNIT,
186
+ updateCallback,
187
+ updateParams,
188
+ headers: {
189
+ "Content-Type": "application/json",
190
+ "x-api-token": process.env.NEXT_PUBLIC_API_KEY,
191
+ },
192
+ });
193
+ // ============================================================================
194
+ // 1.4.6 HANDLERS
195
+ // ============================================================================
196
+ const handleChange = useCallback((field, value) => {
197
+ dispatch({
198
+ type: USER_ACTION_TYPES.SET_ERRORS,
199
+ payload: { errors: {} },
200
+ });
201
+ dispatch({
202
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
203
+ payload: { disabled: false },
204
+ });
205
+ const normalized = field === "email" && typeof value === "string"
206
+ ? value.toLowerCase()
207
+ : value;
208
+ dispatch({
209
+ type: USER_ACTION_TYPES.SET_INPUT_FIELD,
210
+ payload: {
211
+ key: field,
212
+ value: normalized,
213
+ },
214
+ });
215
+ }, [dispatch]);
216
+ const closeDrawer = useCallback(() => {
217
+ resetFormAndCloseDrawer();
218
+ }, [resetFormAndCloseDrawer]);
219
+ const handleCreate = useCallback(() => {
220
+ dispatch({
221
+ type: USER_ACTION_TYPES.SET_DRAWER,
222
+ payload: { drawer: USER_DRAWER.FORM_DRAWER },
223
+ });
224
+ }, [dispatch]);
225
+ const handleView = useCallback((row) => {
226
+ byIdFetchNow === null || byIdFetchNow === void 0 ? void 0 : byIdFetchNow(undefined, { params: { id: row === null || row === void 0 ? void 0 : row.id } });
227
+ dispatch({
228
+ type: USER_ACTION_TYPES.SET_DRAWER,
229
+ payload: { drawer: USER_DRAWER.VIEW_DRAWER },
230
+ });
231
+ }, [byIdFetchNow, dispatch]);
232
+ const handleEdit = useCallback((row) => {
233
+ byIdFetchNow === null || byIdFetchNow === void 0 ? void 0 : byIdFetchNow(undefined, { params: { id: row === null || row === void 0 ? void 0 : row.id } });
234
+ dispatch({
235
+ type: USER_ACTION_TYPES.SET_DRAWER,
236
+ payload: { drawer: USER_DRAWER.FORM_DRAWER },
237
+ });
238
+ }, [byIdFetchNow, dispatch]);
239
+ const handleDelete = useCallback((row) => {
240
+ if (!confirm(t("messagesDeleteConfirmation")))
241
+ return;
242
+ deleteFetchNow === null || deleteFetchNow === void 0 ? void 0 : deleteFetchNow(undefined, {
243
+ body: JSON.stringify({ id: row === null || row === void 0 ? void 0 : row.id }),
244
+ });
245
+ }, [deleteFetchNow, t]);
246
+ const handleFilters = useCallback(() => {
247
+ dispatch({
248
+ type: USER_ACTION_TYPES.SET_DRAWER,
249
+ payload: { drawer: USER_DRAWER.FILTER_DRAWER },
250
+ });
251
+ }, [dispatch]);
252
+ const handleMoreActions = useCallback(() => {
253
+ dispatch({
254
+ type: USER_ACTION_TYPES.SET_DRAWER,
255
+ payload: { drawer: USER_DRAWER.MORE_ACTIONS_DRAWER },
256
+ });
257
+ }, [dispatch]);
258
+ const handlePageChange = useCallback((page) => {
259
+ const nextPage = typeof page === "number" ? page : state.currentPage + 1;
260
+ dispatch({
261
+ type: USER_ACTION_TYPES.SET_CURRENT_PAGE,
262
+ payload: { currentPage: nextPage },
263
+ });
264
+ }, [dispatch, state.currentPage]);
265
+ const handlePageLimitChange = useCallback((k, value) => {
266
+ const val = Object.assign({}, value);
267
+ dispatch({
268
+ type: USER_ACTION_TYPES.SET_PAGE_LIMIT,
269
+ payload: { pageLimit: Number(val.option) },
270
+ });
271
+ dispatch({
272
+ type: USER_ACTION_TYPES.SET_CURRENT_PAGE,
273
+ payload: { currentPage: 1 },
274
+ });
275
+ }, [dispatch]);
276
+ const handleSearch = useCallback((query) => {
277
+ dispatch({
278
+ type: USER_ACTION_TYPES.SET_SEARCH_QUERY,
279
+ payload: { searchQuery: query },
280
+ });
281
+ }, [dispatch]);
282
+ const clearSearch = useCallback(() => {
283
+ dispatch({
284
+ type: USER_ACTION_TYPES.SET_SEARCH_QUERY,
285
+ payload: { searchQuery: "" },
286
+ });
287
+ }, [dispatch]);
288
+ const handleAvatar = useCallback((files) => {
289
+ if (!files || files.length === 0)
290
+ return;
291
+ const file = files[0];
292
+ const blob = new Blob([file], { type: file.type });
293
+ const url = URL.createObjectURL(blob);
294
+ dispatch({
295
+ type: USER_ACTION_TYPES.SET_INPUT_FIELD,
296
+ payload: { key: "avatar", value: url },
297
+ });
298
+ }, [dispatch]);
299
+ const applyFilters = useCallback(() => {
300
+ dispatch({
301
+ type: USER_ACTION_TYPES.SET_CURRENT_PAGE,
302
+ payload: { currentPage: 1 },
303
+ });
304
+ closeDrawer();
305
+ }, [dispatch, closeDrawer]);
306
+ const clearFilters = useCallback(() => {
307
+ dispatch({
308
+ type: USER_ACTION_TYPES.SET_FILTERS,
309
+ payload: { filters: { filterEnabled: undefined } },
310
+ });
311
+ dispatch({
312
+ type: USER_ACTION_TYPES.SET_CURRENT_PAGE,
313
+ payload: { currentPage: 1 },
314
+ });
315
+ }, [dispatch]);
316
+ const toggleStatus = useCallback((row) => {
317
+ updateFetchNow(undefined, {
318
+ body: JSON.stringify(Object.assign(Object.assign({}, row), { workspaceId: workspace === null || workspace === void 0 ? void 0 : workspace.id, enabled: !(row === null || row === void 0 ? void 0 : row.enabled) })),
319
+ });
320
+ }, [updateFetchNow, workspace === null || workspace === void 0 ? void 0 : workspace.id]);
321
+ // ============================================================================
322
+ // 1.4.7 NETWORK ACTIONS
323
+ // ============================================================================
324
+ const handleUploadImage = useCallback(async () => {
325
+ var _a;
326
+ if (!((_a = state.avatar) === null || _a === void 0 ? void 0 : _a.startsWith("blob:")))
327
+ return undefined;
328
+ const fileRef = `${workspace === null || workspace === void 0 ? void 0 : workspace.id}/${updateParams.id}/avatar/${updateParams.id}.webp`;
329
+ try {
330
+ const blob = await fetch(state.avatar).then((r) => r.blob());
331
+ const webpBlob = await blobToWebP(blob, { quality: 0.8 });
332
+ const arrayBuffer = await webpBlob.arrayBuffer();
333
+ let binary = "";
334
+ const bytes = new Uint8Array(arrayBuffer);
335
+ const chunkSize = 0x8000;
336
+ for (let i = 0; i < bytes.length; i += chunkSize) {
337
+ const sub = bytes.subarray(i, i + chunkSize);
338
+ for (let j = 0; j < sub.length; j++) {
339
+ binary += String.fromCharCode(sub[j]);
340
+ }
341
+ }
342
+ const base64 = btoa(binary);
343
+ const res = await fetch("/api/uploads/avatar", {
344
+ method: "POST",
345
+ headers: { "Content-Type": "application/json" },
346
+ body: JSON.stringify({
347
+ fileBase64: base64,
348
+ fileRef,
349
+ contentType: webpBlob.type,
350
+ }),
351
+ });
352
+ if (!res.ok)
353
+ throw new Error("Upload failed");
354
+ const responseData = await res.json();
355
+ if (responseData === null || responseData === void 0 ? void 0 : responseData.path) {
356
+ const url = `${supabasePublicStorageUrl}/${responseData.path}`;
357
+ dispatch({
358
+ type: USER_ACTION_TYPES.SET_INPUT_FIELD,
359
+ payload: { key: "avatar", value: url },
360
+ });
361
+ return url;
362
+ }
363
+ }
364
+ catch (_b) {
365
+ toastNetworkError();
366
+ }
367
+ return undefined;
368
+ }, [state.avatar, dispatch, workspace === null || workspace === void 0 ? void 0 : workspace.id, updateParams.id]);
369
+ const checkUserEmailExists = useCallback(async (emailParam) => {
370
+ var _a;
371
+ const targetEmail = (typeof emailParam === "string" && emailParam) || state.email;
372
+ if (!targetEmail)
373
+ return null;
374
+ const result = await fetchData({
375
+ url: `${USER_API_ROUTES.UNIT_BY_EMAIL}?email=${encodeURIComponent(String(targetEmail))}`,
376
+ method: API_METHODS.GET,
377
+ headers: {
378
+ "x-api-token": process.env.NEXT_PUBLIC_API_KEY,
379
+ },
380
+ });
381
+ return (_a = result === null || result === void 0 ? void 0 : result.data) !== null && _a !== void 0 ? _a : null;
382
+ }, [state.email]);
383
+ const handleSubmit = useCallback(async () => {
384
+ dispatch({
385
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
386
+ payload: { disabled: true },
387
+ });
388
+ const uploadedUrl = await handleUploadImage();
389
+ const finalParams = uploadedUrl
390
+ ? Object.assign(Object.assign({}, updateParams), { avatar: uploadedUrl }) : updateParams;
391
+ validateForm({
392
+ params: finalParams,
393
+ schema: userFormValidation,
394
+ successCallback: async () => {
395
+ try {
396
+ const existing = await checkUserEmailExists(state.email);
397
+ if (existing &&
398
+ existing.id &&
399
+ existing.id !== state.id) {
400
+ dispatch({
401
+ type: USER_ACTION_TYPES.SET_ERRORS,
402
+ payload: { errors: { email: t("messagesEmailAlreadyInUse") } },
403
+ });
404
+ dispatch({
405
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
406
+ payload: { disabled: false },
407
+ });
408
+ return;
409
+ }
410
+ updateFetchNow(undefined, { body: JSON.stringify(finalParams) });
411
+ }
412
+ catch (_a) {
413
+ toastNetworkError();
414
+ dispatch({
415
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
416
+ payload: { disabled: false },
417
+ });
418
+ }
419
+ },
420
+ errorCallback: (errors) => {
421
+ dispatch({
422
+ type: USER_ACTION_TYPES.SET_ERRORS,
423
+ payload: { errors },
424
+ });
425
+ dispatch({
426
+ type: USER_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
427
+ payload: { disabled: false },
428
+ });
429
+ showToast(t("messagesFormErrors"), TOAST_VARIANT.ERROR);
430
+ },
431
+ });
432
+ }, [
433
+ checkUserEmailExists,
434
+ dispatch,
435
+ handleUploadImage,
436
+ showToast,
437
+ state.email,
438
+ state.id,
439
+ t,
440
+ updateFetchNow,
441
+ updateParams,
442
+ ]);
443
+ // ============================================================================
444
+ // 1.4.8 HEADER & ROW ACTIONS
445
+ // ============================================================================
446
+ const headerActions = useMemo(() => [
447
+ {
448
+ enabled: false,
449
+ handleOnClick: handleMoreActions,
450
+ label: t("actionsButtonMoreActions"),
451
+ order: 0,
452
+ },
453
+ {
454
+ enabled: false,
455
+ handleOnClick: handleFilters,
456
+ label: t("actionsButtonFilters"),
457
+ order: 1,
458
+ },
459
+ {
460
+ enabled: true,
461
+ handleOnClick: handleCreate,
462
+ label: t("actionsButtonAdd"),
463
+ order: 2,
464
+ },
465
+ ], [handleCreate, handleFilters, handleMoreActions, t]);
466
+ const rowActions = useMemo(() => [
467
+ {
468
+ enabled: true,
469
+ handleOnClick: handleView,
470
+ label: t("actionsButtonView"),
471
+ order: 1,
472
+ },
473
+ {
474
+ enabled: true,
475
+ handleOnClick: handleEdit,
476
+ label: t("actionsButtonEdit"),
477
+ order: 2,
478
+ },
479
+ {
480
+ enabled: false,
481
+ handleOnClick: handleDelete,
482
+ label: t("actionsButtonDelete"),
483
+ order: 3,
484
+ },
485
+ {
486
+ enabled: false,
487
+ handleOnClick: toggleStatus,
488
+ label: t("actionsButtonToggleStatus"),
489
+ order: 4,
490
+ },
491
+ ], [handleDelete, handleEdit, handleView, t, toggleStatus]);
492
+ // ============================================================================
493
+ // 1.4.9 EFFECTS
494
+ // ============================================================================
495
+ useEffect(() => {
496
+ var _a;
497
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id))
498
+ return;
499
+ if (isDefaultListState) {
500
+ (async () => {
501
+ try {
502
+ const { count, items } = await getCachedUsers({ params: listParams });
503
+ dispatch({
504
+ type: USER_ACTION_TYPES.SET_ITEMS,
505
+ payload: { items: items || [], count: count || 0 },
506
+ });
507
+ }
508
+ catch (_a) {
509
+ showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
510
+ }
511
+ })();
512
+ return;
513
+ }
514
+ (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
515
+ }, [dispatch, isDefaultListState, listParams, workspace === null || workspace === void 0 ? void 0 : workspace.id, showToast, t]);
516
+ useEffect(() => {
517
+ listFetchNowRef.current = listFetchNow;
518
+ }, [listFetchNow]);
519
+ // ============================================================================
520
+ // 1.4.10 RETURN
521
+ // ============================================================================
522
+ return Object.assign(Object.assign({}, context), { applyFilters,
523
+ byIdLoading,
524
+ clearFilters,
525
+ clearSearch,
526
+ closeDrawer,
527
+ deleteLoading,
528
+ handleAvatar,
529
+ handleChange,
530
+ handleCreate,
531
+ handleDelete,
532
+ handleEdit,
533
+ handleFilters,
534
+ handleMoreActions,
535
+ handlePageChange,
536
+ handlePageLimitChange,
537
+ handleSearch,
538
+ handleSubmit,
539
+ handleView,
540
+ headerActions,
541
+ listFetchNow,
542
+ listLoading,
543
+ rowActions,
544
+ toggleStatus,
545
+ updateLoading });
546
+ };
@@ -1,5 +1,4 @@
1
1
  import { UserBE, USER_ROLE } from "../../type";
2
- import { type RowAction, type TableRow } from "@appcorp/shadcn/components/enhanced-table";
3
2
  export declare const USER_DRAWER: {
4
3
  readonly FILTER_DRAWER: string;
5
4
  readonly FORM_DRAWER: string;
@@ -111,54 +110,4 @@ export declare const USER_ACTION_TYPES: {
111
110
  userRole: USER_ROLE | null;
112
111
  filterEnabled: boolean | undefined;
113
112
  }>;
114
- export declare const useUserModule: () => {
115
- applyFilters: () => void;
116
- byIdLoading: boolean;
117
- clearFilters: () => void;
118
- clearSearch: () => void;
119
- closeDrawer: () => void;
120
- deleteLoading: boolean;
121
- handleAvatar: (files: File[]) => void;
122
- handleChange: (field: string, value: string | number | boolean | undefined | null) => void;
123
- handleCreate: () => void;
124
- handleDelete: (row?: TableRow) => void;
125
- handleEdit: (row?: TableRow) => void;
126
- handleFilters: () => void;
127
- handleMoreActions: () => void;
128
- handlePageChange: (page: number | unknown) => void;
129
- handlePageLimitChange: (k: string, value: object) => void;
130
- handleSearch: (query: string) => void;
131
- handleSubmit: () => Promise<void>;
132
- handleView: (row?: TableRow) => void;
133
- headerActions: {
134
- enabled: boolean;
135
- handleOnClick: () => void;
136
- label: string;
137
- order: number;
138
- }[];
139
- listFetchNow: (url?: string, config?: import("@react-pakistan/util-functions/hooks/use-fetch").FetchConfig) => void;
140
- listLoading: boolean;
141
- rowActions: RowAction[];
142
- toggleStatus: (row?: TableRow) => void;
143
- updateLoading: boolean;
144
- state: {
145
- items: UserBE[];
146
- count: number;
147
- currentPage: number;
148
- pageLimit: number;
149
- searchQuery: string;
150
- disableSaveButton: boolean;
151
- drawer: string | null;
152
- errors: Record<string, string>;
153
- id: string;
154
- name: string;
155
- email: string;
156
- phone: string;
157
- avatar: string;
158
- password: string;
159
- enabled: boolean;
160
- userRole: USER_ROLE | null;
161
- filterEnabled: boolean | undefined;
162
- };
163
- dispatch: React.Dispatch<any>;
164
- };
113
+ export { useUserModule } from "./context/use-user-module";