@appcorp/fusion-storybook 0.2.13 → 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.
- package/base-modules/admission/context/use-admission-module.d.ts +88 -0
- package/base-modules/admission/context/use-admission-module.js +771 -0
- package/base-modules/admission/context.d.ts +1 -87
- package/base-modules/admission/context.js +3 -769
- package/base-modules/campus/more-actions.js +0 -1
- package/base-modules/class/more-actions.js +1 -6
- package/base-modules/expense/more-actions.js +1 -6
- package/base-modules/fee-structure/more-actions.js +1 -6
- package/base-modules/student-fee/context/shared.d.ts +178 -0
- package/base-modules/student-fee/context/shared.js +59 -0
- package/base-modules/student-fee/context/use-student-fee-module.d.ts +64 -0
- package/base-modules/student-fee/context/use-student-fee-module.js +610 -0
- package/base-modules/student-fee/context.d.ts +21 -235
- package/base-modules/student-fee/context.js +2 -674
- package/base-modules/student-fee/more-actions.js +1 -6
- package/base-modules/student-profile/context/module-base.d.ts +187 -0
- package/base-modules/student-profile/context/module-base.js +50 -0
- package/base-modules/student-profile/context/use-student-profile-module.d.ts +70 -0
- package/base-modules/student-profile/context/use-student-profile-module.js +506 -0
- package/base-modules/student-profile/context.d.ts +20 -256
- package/base-modules/student-profile/context.js +2 -601
- package/base-modules/teacher/avatar-upload.js +1 -4
- package/base-modules/teacher/more-actions.js +1 -6
- package/base-modules/user/context/use-user-module.d.ts +53 -0
- package/base-modules/user/context/use-user-module.js +546 -0
- package/base-modules/user/context.d.ts +1 -52
- package/base-modules/user/context.js +5 -547
- package/base-modules/workspace-user/more-actions.js +1 -6
- package/components/module-error.d.ts +9 -0
- package/components/module-error.js +3 -0
- package/package.json +5 -4
- package/tsconfig.build.tsbuildinfo +1 -1
- package/type.d.ts +3 -1242
- package/type.js +3 -445
- package/types/academics.d.ts +264 -0
- package/types/academics.js +8 -0
- package/types/admission.d.ts +85 -0
- package/types/admission.js +6 -0
- package/types/communication.d.ts +165 -0
- package/types/communication.js +7 -0
- package/types/enums.d.ts +411 -0
- package/types/enums.js +442 -0
- package/types/finance.d.ts +126 -0
- package/types/finance.js +7 -0
- package/types/index.d.ts +12 -0
- package/types/index.js +12 -0
- package/types/user-management.d.ts +236 -0
- package/types/user-management.js +7 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { UserBE, USER_ROLE } from "../../../type";
|
|
2
|
+
import { type RowAction, type TableRow } from "@appcorp/shadcn/components/enhanced-table";
|
|
3
|
+
export declare const useUserModule: () => {
|
|
4
|
+
applyFilters: () => void;
|
|
5
|
+
byIdLoading: boolean;
|
|
6
|
+
clearFilters: () => void;
|
|
7
|
+
clearSearch: () => void;
|
|
8
|
+
closeDrawer: () => void;
|
|
9
|
+
deleteLoading: boolean;
|
|
10
|
+
handleAvatar: (files: File[]) => void;
|
|
11
|
+
handleChange: (field: string, value: string | number | boolean | undefined | null) => void;
|
|
12
|
+
handleCreate: () => void;
|
|
13
|
+
handleDelete: (row?: TableRow) => void;
|
|
14
|
+
handleEdit: (row?: TableRow) => void;
|
|
15
|
+
handleFilters: () => void;
|
|
16
|
+
handleMoreActions: () => void;
|
|
17
|
+
handlePageChange: (page: number | unknown) => void;
|
|
18
|
+
handlePageLimitChange: (k: string, value: object) => void;
|
|
19
|
+
handleSearch: (query: string) => void;
|
|
20
|
+
handleSubmit: () => Promise<void>;
|
|
21
|
+
handleView: (row?: TableRow) => void;
|
|
22
|
+
headerActions: {
|
|
23
|
+
enabled: boolean;
|
|
24
|
+
handleOnClick: () => void;
|
|
25
|
+
label: string;
|
|
26
|
+
order: number;
|
|
27
|
+
}[];
|
|
28
|
+
listFetchNow: (url?: string, config?: import("@react-pakistan/util-functions/hooks/use-fetch").FetchConfig) => void;
|
|
29
|
+
listLoading: boolean;
|
|
30
|
+
rowActions: RowAction[];
|
|
31
|
+
toggleStatus: (row?: TableRow) => void;
|
|
32
|
+
updateLoading: boolean;
|
|
33
|
+
state: {
|
|
34
|
+
items: UserBE[];
|
|
35
|
+
count: number;
|
|
36
|
+
currentPage: number;
|
|
37
|
+
pageLimit: number;
|
|
38
|
+
searchQuery: string;
|
|
39
|
+
disableSaveButton: boolean;
|
|
40
|
+
drawer: string | null;
|
|
41
|
+
errors: Record<string, string>;
|
|
42
|
+
id: string;
|
|
43
|
+
name: string;
|
|
44
|
+
email: string;
|
|
45
|
+
phone: string;
|
|
46
|
+
avatar: string;
|
|
47
|
+
password: string;
|
|
48
|
+
enabled: boolean;
|
|
49
|
+
userRole: USER_ROLE | null;
|
|
50
|
+
filterEnabled: boolean | undefined;
|
|
51
|
+
};
|
|
52
|
+
dispatch: React.Dispatch<any>;
|
|
53
|
+
};
|
|
@@ -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
|
|
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";
|