@appcorp/fusion-storybook 0.1.64 → 0.1.65

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.
@@ -18,7 +18,7 @@
18
18
  * - `FAMILY_DRAWER` — drawer type constants
19
19
  * - `useFamilyModule()` — hook that returns state, dispatch, and handlers
20
20
  */
21
- import { useCallback, useEffect, useMemo } from "react";
21
+ import { useCallback, useEffect, useMemo, useRef } from "react";
22
22
  import { isCreatedOrUpdated, validateForm, } from "@react-pakistan/util-functions";
23
23
  import { useModuleEntityV2, } from "@react-pakistan/util-functions/hooks/use-module-entity-v2";
24
24
  import { useDebounce } from "@react-pakistan/util-functions/hooks/use-debounce";
@@ -31,8 +31,9 @@ import { generateThemeToast, TOAST_VARIANT, } from "@appcorp/shadcn/lib/toast-ut
31
31
  import { useTranslations } from "next-intl";
32
32
  import { getCachedWorkspaceSync } from "../workspace/cache";
33
33
  import { Eye, Filter, MoreHorizontal, Pencil, Plus } from "lucide-react";
34
+ import { useTheme } from "next-themes";
34
35
  // ============================================================================
35
- // DRAWER TYPES
36
+ // 1.1 DRAWER TYPES
36
37
  // ============================================================================
37
38
  export const FAMILY_DRAWER = {
38
39
  FILTER_DRAWER: DRAWER_TYPES.FILTER_DRAWER,
@@ -41,7 +42,7 @@ export const FAMILY_DRAWER = {
41
42
  VIEW_DRAWER: DRAWER_TYPES.VIEW_DRAWER,
42
43
  };
43
44
  // ============================================================================
44
- // CONFIGURATION
45
+ // 1.2 MODULE CONFIGURATION
45
46
  // ============================================================================
46
47
  const familyConfig = {
47
48
  name: "Family",
@@ -73,7 +74,7 @@ const familyConfig = {
73
74
  drawerTypes: DRAWER_TYPES,
74
75
  };
75
76
  // ============================================================================
76
- // CREATE FAMILY MODULE
77
+ // 1.3 CREATE FAMILY MODULE
77
78
  // ============================================================================
78
79
  export const { actionTypes: FAMILY_ACTION_TYPES, config: familyModuleConfig, initialState: initialFamilyState, Provider: FamilyProvider, reducer: familyReducer, useContext: useFamilyContext, } = createGenericModule(familyConfig);
79
80
  /**
@@ -81,17 +82,24 @@ export const { actionTypes: FAMILY_ACTION_TYPES, config: familyModuleConfig, ini
81
82
  */
82
83
  export const FamilyStateContextProvider = FamilyProvider;
83
84
  // ============================================================================
84
- // ENHANCED FAMILY HOOK WITH API INTEGRATION
85
+ // 1.4 ENHANCED FAMILY HOOK WITH API INTEGRATION
85
86
  // ============================================================================
86
87
  export const useFamilyModule = () => {
88
+ // ==========================================================================
89
+ // 1.4.1 STATE & CORE HOOKS
90
+ // ==========================================================================
87
91
  var _a, _b;
88
- const { state, dispatch } = useFamilyContext();
92
+ const context = useFamilyContext();
93
+ const { dispatch } = context;
94
+ const state = context.state;
89
95
  const t = useTranslations("family");
96
+ const { theme } = useTheme();
90
97
  const workspace = getCachedWorkspaceSync();
98
+ const listFetchNowRef = useRef(null);
91
99
  const debouncedQuery = useDebounce(state.searchQuery, 800);
92
- // ============================================================================
93
- // API PARAMETERS
94
- // ============================================================================
100
+ // ==========================================================================
101
+ // 1.4.2 API PARAMETERS
102
+ // ==========================================================================
95
103
  const listParams = useMemo(() => {
96
104
  var _a;
97
105
  return (Object.assign(Object.assign({ currentPage: state.currentPage, pageLimit: state.pageLimit, schoolId: ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "" }, (debouncedQuery ? { searchQuery: debouncedQuery } : {})), (state.filterEnabled !== undefined
@@ -125,10 +133,40 @@ export const useFamilyModule = () => {
125
133
  ]);
126
134
  const byIdParams = useMemo(() => ({ id: state.id }), [state.id]);
127
135
  const deleteParams = useMemo(() => ({ id: state.id }), [state.id]);
128
- // ============================================================================
129
- // API CALLBACKS
130
- // ============================================================================
131
- const listCallback = ({ data, error }) => {
136
+ // ==========================================================================
137
+ // 1.4.3 UTILITIES
138
+ // ==========================================================================
139
+ const showToast = useCallback((description, variant) => {
140
+ generateThemeToast({
141
+ description,
142
+ theme: theme,
143
+ variant,
144
+ });
145
+ }, [theme]);
146
+ const resetFormAndCloseDrawer = useCallback(() => {
147
+ dispatch({
148
+ type: FAMILY_ACTION_TYPES.SET_ERRORS,
149
+ payload: { errors: {} },
150
+ });
151
+ dispatch({
152
+ type: FAMILY_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
153
+ payload: { disabled: false },
154
+ });
155
+ dispatch({
156
+ type: FAMILY_ACTION_TYPES.SET_DRAWER,
157
+ payload: { drawer: null },
158
+ });
159
+ }, [dispatch]);
160
+ const setField = useCallback((key, value) => {
161
+ dispatch({
162
+ type: FAMILY_ACTION_TYPES.SET_INPUT_FIELD,
163
+ payload: { key, value: value === null ? undefined : value },
164
+ });
165
+ }, [dispatch]);
166
+ // ==========================================================================
167
+ // 1.4.4 API CALLBACKS
168
+ // ==========================================================================
169
+ const listCallback = useCallback(({ data, error }) => {
132
170
  if (error) {
133
171
  showToast(t("messagesFetchFailed"), TOAST_VARIANT.ERROR);
134
172
  return;
@@ -139,8 +177,9 @@ export const useFamilyModule = () => {
139
177
  payload: { items: data.items || [], count: data.count || 0 },
140
178
  });
141
179
  }
142
- };
143
- const updateCallback = ({ data, error }) => {
180
+ }, [dispatch, showToast, t]);
181
+ const updateCallback = useCallback(({ data, error }) => {
182
+ var _a;
144
183
  const isCreated = isCreatedOrUpdated(data);
145
184
  if (error) {
146
185
  showToast(isCreated ? t("messagesCreateFailed") : t("messagesUpdateFailed"), TOAST_VARIANT.ERROR);
@@ -149,11 +188,11 @@ export const useFamilyModule = () => {
149
188
  if (data) {
150
189
  showToast(t("messagesSaveSuccess"), TOAST_VARIANT.SUCCESS);
151
190
  invalidateFamiliesCache();
152
- listFetchNow();
191
+ (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
153
192
  resetFormAndCloseDrawer();
154
193
  }
155
- };
156
- const byIdCallback = ({ data, error }) => {
194
+ }, [showToast, t, resetFormAndCloseDrawer]);
195
+ const byIdCallback = useCallback(({ data, error }) => {
157
196
  if (error) {
158
197
  showToast(t("messagesDetailsFetchFailed"), TOAST_VARIANT.ERROR);
159
198
  return;
@@ -177,8 +216,9 @@ export const useFamilyModule = () => {
177
216
  },
178
217
  });
179
218
  }
180
- };
181
- const deleteCallback = ({ data, error }) => {
219
+ }, [dispatch, showToast, t, setField]);
220
+ const deleteCallback = useCallback(({ data, error }) => {
221
+ var _a;
182
222
  if (error) {
183
223
  showToast(t("messagesDeleteFailed"), TOAST_VARIANT.ERROR);
184
224
  return;
@@ -186,13 +226,13 @@ export const useFamilyModule = () => {
186
226
  if (data) {
187
227
  showToast(t("messagesDeleteSuccess"), TOAST_VARIANT.SUCCESS);
188
228
  invalidateFamiliesCache();
189
- listFetchNow();
229
+ (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
190
230
  }
191
- };
192
- // ============================================================================
193
- // API HOOKS
194
- // ============================================================================
195
- const { listFetchNow, listLoading, updateFetchNow, updateLoading, byIdFetchNow, deleteFetchNow, deleteLoading, byIdLoading, } = useModuleEntityV2({
231
+ }, [showToast, t]);
232
+ // ==========================================================================
233
+ // 1.4.5 API HOOKS
234
+ // ==========================================================================
235
+ const { byIdFetchNow, byIdLoading, deleteFetchNow, deleteLoading, listFetchNow, listLoading, updateFetchNow, updateLoading, } = useModuleEntityV2({
196
236
  byIdCallback,
197
237
  byIdParams,
198
238
  deleteCallback,
@@ -210,35 +250,9 @@ export const useFamilyModule = () => {
210
250
  "x-api-token": process.env.NEXT_PUBLIC_API_KEY,
211
251
  },
212
252
  });
213
- // ============================================================================
214
- // UTILITIES
215
- // ============================================================================
216
- const showToast = useCallback((message, variant) => {
217
- generateThemeToast({ description: message, variant });
218
- }, []);
219
- const resetFormAndCloseDrawer = useCallback(() => {
220
- dispatch({
221
- type: FAMILY_ACTION_TYPES.SET_ERRORS,
222
- payload: { errors: {} },
223
- });
224
- dispatch({
225
- type: FAMILY_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
226
- payload: { disabled: false },
227
- });
228
- dispatch({
229
- type: FAMILY_ACTION_TYPES.SET_DRAWER,
230
- payload: { drawer: null },
231
- });
232
- }, [dispatch]);
233
- const setField = useCallback((key, value) => {
234
- dispatch({
235
- type: FAMILY_ACTION_TYPES.SET_INPUT_FIELD,
236
- payload: { key, value: value === null ? undefined : value },
237
- });
238
- }, [dispatch]);
239
- // ============================================================================
240
- // HANDLERS
241
- // ============================================================================
253
+ // ==========================================================================
254
+ // 1.4.6 HANDLERS
255
+ // ==========================================================================
242
256
  const handleChange = useCallback((field, value) => {
243
257
  dispatch({
244
258
  type: FAMILY_ACTION_TYPES.SET_ERRORS,
@@ -250,18 +264,6 @@ export const useFamilyModule = () => {
250
264
  });
251
265
  setField(field, value);
252
266
  }, [setField, dispatch]);
253
- const handlePageChange = useCallback((page) => {
254
- dispatch({
255
- type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
256
- payload: { currentPage: page },
257
- });
258
- }, [dispatch]);
259
- const handlePageLimitChange = useCallback((limit) => {
260
- dispatch({
261
- type: FAMILY_ACTION_TYPES.SET_PAGE_LIMIT,
262
- payload: { pageLimit: limit },
263
- });
264
- }, [dispatch]);
265
267
  const handleCloseDrawer = useCallback(() => {
266
268
  resetFormAndCloseDrawer();
267
269
  }, [resetFormAndCloseDrawer]);
@@ -304,10 +306,17 @@ export const useFamilyModule = () => {
304
306
  payload: { drawer: FAMILY_DRAWER.MORE_ACTIONS_DRAWER },
305
307
  });
306
308
  }, [dispatch]);
307
- const clearFilters = useCallback(() => {
309
+ const handlePageChange = useCallback((page) => {
308
310
  dispatch({
309
- type: FAMILY_ACTION_TYPES.SET_FILTERS,
310
- payload: { filters: { filterEnabled: undefined } },
311
+ type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
312
+ payload: { currentPage: page },
313
+ });
314
+ }, [dispatch]);
315
+ const handlePageLimitChange = useCallback((_k, value) => {
316
+ const val = Object.assign({}, value);
317
+ dispatch({
318
+ type: FAMILY_ACTION_TYPES.SET_PAGE_LIMIT,
319
+ payload: { pageLimit: Number(val.option) },
311
320
  });
312
321
  dispatch({
313
322
  type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
@@ -320,6 +329,33 @@ export const useFamilyModule = () => {
320
329
  payload: { searchQuery: query },
321
330
  });
322
331
  }, [dispatch]);
332
+ const clearSearch = useCallback(() => {
333
+ dispatch({
334
+ type: FAMILY_ACTION_TYPES.SET_SEARCH_QUERY,
335
+ payload: { searchQuery: "" },
336
+ });
337
+ }, [dispatch]);
338
+ const applyFilters = useCallback(() => {
339
+ dispatch({
340
+ type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
341
+ payload: { currentPage: 1 },
342
+ });
343
+ listFetchNow();
344
+ handleCloseDrawer();
345
+ }, [dispatch, listFetchNow, handleCloseDrawer]);
346
+ const clearFilters = useCallback(() => {
347
+ dispatch({
348
+ type: FAMILY_ACTION_TYPES.SET_FILTERS,
349
+ payload: { filters: { filterEnabled: undefined } },
350
+ });
351
+ dispatch({
352
+ type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
353
+ payload: { currentPage: 1 },
354
+ });
355
+ }, [dispatch]);
356
+ // ==========================================================================
357
+ // 1.4.7 NETWORK ACTIONS
358
+ // ==========================================================================
323
359
  const handleSubmit = useCallback(() => {
324
360
  dispatch({
325
361
  type: FAMILY_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
@@ -346,14 +382,9 @@ export const useFamilyModule = () => {
346
382
  },
347
383
  });
348
384
  }, [dispatch, updateParams, t, showToast, updateFetchNow]);
349
- const applyFilters = useCallback(() => {
350
- dispatch({
351
- type: FAMILY_ACTION_TYPES.SET_CURRENT_PAGE,
352
- payload: { currentPage: 1 },
353
- });
354
- listFetchNow();
355
- handleCloseDrawer();
356
- }, [dispatch, listFetchNow, handleCloseDrawer]);
385
+ // ==========================================================================
386
+ // 1.4.8 HEADER & ROW ACTIONS
387
+ // ==========================================================================
357
388
  const headerActions = useMemo(() => [
358
389
  {
359
390
  enabled: false,
@@ -393,9 +424,9 @@ export const useFamilyModule = () => {
393
424
  icon: Pencil,
394
425
  },
395
426
  ], [handleView, handleEdit, t]);
396
- // ============================================================================
397
- // EFFECTS
398
- // ============================================================================
427
+ // ==========================================================================
428
+ // 1.4.9 EFFECTS
429
+ // ==========================================================================
399
430
  useEffect(() => {
400
431
  var _a;
401
432
  if (!((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id))
@@ -415,15 +446,16 @@ export const useFamilyModule = () => {
415
446
  }
416
447
  })();
417
448
  }, [listParams, (_b = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _b === void 0 ? void 0 : _b.id, dispatch, showToast, t]);
418
- // ============================================================================
419
- // RETURN
420
- // ============================================================================
421
- return {
422
- state,
423
- dispatch,
424
- applyFilters,
449
+ useEffect(() => {
450
+ listFetchNowRef.current = listFetchNow;
451
+ });
452
+ // ==========================================================================
453
+ // 1.4.10 RETURN
454
+ // ==========================================================================
455
+ return Object.assign(Object.assign({}, context), { applyFilters,
425
456
  byIdLoading,
426
457
  clearFilters,
458
+ clearSearch,
427
459
  deleteLoading,
428
460
  handleChange,
429
461
  handleCloseDrawer,
@@ -438,8 +470,8 @@ export const useFamilyModule = () => {
438
470
  handleSubmit,
439
471
  handleView,
440
472
  headerActions,
473
+ listFetchNow,
441
474
  listLoading,
442
475
  rowActions,
443
- updateLoading,
444
- };
476
+ updateLoading });
445
477
  };
@@ -8,6 +8,9 @@
8
8
  * component type produced by createGenericModulePage never changes at
9
9
  * runtime, preventing the infinite-remount re-render loop)
10
10
  * - permission guard
11
+ *
12
+ * Exported utilities:
13
+ * - `FamilyPage` — root component that wraps with FamilyProvider
11
14
  */
12
15
  import { FC } from "react";
13
16
  import { USER_ROLE } from "../../type";
@@ -1,5 +1,3 @@
1
- "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
1
  /**
4
2
  * Family Page Component
5
3
  *
@@ -10,7 +8,12 @@ import { jsx as _jsx } from "react/jsx-runtime";
10
8
  * component type produced by createGenericModulePage never changes at
11
9
  * runtime, preventing the infinite-remount re-render loop)
12
10
  * - permission guard
11
+ *
12
+ * Exported utilities:
13
+ * - `FamilyPage` — root component that wraps with FamilyProvider
13
14
  */
15
+ "use client";
16
+ import { jsx as _jsx } from "react/jsx-runtime";
14
17
  import { useMemo } from "react";
15
18
  import { COMPONENT_TYPE } from "@appcorp/shadcn/components/enhanced-table";
16
19
  import { createGenericModulePage, } from "@react-pakistan/util-functions/factory/generic-component-factory";
@@ -74,15 +77,11 @@ const createFamilyConfig = ({ cancelLabel, dispatch, drawerTitle, labelActions,
74
77
  // STABLE PAGE COMPONENT (created once, outside render)
75
78
  // ============================================================================
76
79
  const GenericFamilyPage = createGenericModulePage();
77
- // ============================================================================
78
- // INNER PAGE (requires FamilyProvider context)
79
- // ============================================================================
80
80
  const FamilyPageInner = (props) => {
81
81
  const context = useFamilyModule();
82
82
  // Memoize config creation - destructure props to avoid object reference changes
83
83
  const familyConfig = useMemo(() => createFamilyConfig({
84
84
  dispatch: context.dispatch,
85
- drawer: context.state.drawer,
86
85
  cancelLabel: props.cancelLabel,
87
86
  drawerTitle: props.drawerTitle,
88
87
  labelActions: props.labelActions,
@@ -98,7 +97,6 @@ const FamilyPageInner = (props) => {
98
97
  tableTitle: props.tableTitle,
99
98
  }), [
100
99
  context.dispatch,
101
- context.state.drawer,
102
100
  props.cancelLabel,
103
101
  props.drawerTitle,
104
102
  props.labelActions,
@@ -122,4 +120,7 @@ const FamilyPageInner = (props) => {
122
120
  }
123
121
  return (_jsx("div", { className: "p-4", children: _jsx(GenericFamilyPage, { overrideConfig: familyConfig, context: context, tableBodyCols: tableBodyCols }) }));
124
122
  };
123
+ // ============================================================================
124
+ // PAGE EXPORTS
125
+ // ============================================================================
125
126
  export const FamilyPage = (props) => (_jsx(FamilyProvider, { children: _jsx(FamilyPageInner, Object.assign({}, props)) }));
@@ -1,5 +1,5 @@
1
1
  import { FamilyMemberBE } from "../../type";
2
- import { type RowAction } from "@appcorp/shadcn/components/enhanced-table";
2
+ import { type RowAction, type TableRow } from "@appcorp/shadcn/components/enhanced-table";
3
3
  export declare const FAMILY_MEMBER_DRAWER: {
4
4
  readonly FILTER_DRAWER: string;
5
5
  readonly FORM_DRAWER: string;
@@ -160,19 +160,20 @@ export declare const useFamilyMemberModule: () => {
160
160
  applyFilters: () => void;
161
161
  byIdLoading: boolean;
162
162
  clearFilters: () => void;
163
+ clearSearch: () => void;
163
164
  deleteLoading: boolean;
164
165
  handleChange: (key: string, value: string | number | boolean | Date | undefined | null) => void;
165
166
  handleCloseDrawer: () => void;
166
167
  handleCreate: () => void;
167
- handleDelete: (row?: unknown) => void;
168
- handleEdit: (row?: unknown) => void;
168
+ handleDelete: (row?: TableRow) => void;
169
+ handleEdit: (row?: TableRow) => void;
169
170
  handleFilters: () => void;
170
171
  handleMoreActions: () => void;
171
172
  handlePageChange: (page: number) => void;
172
- handlePageLimitChange: (limit: number) => void;
173
+ handlePageLimitChange: (_k: string, value: object) => void;
173
174
  handleSearch: (query: string) => void;
174
175
  handleSubmit: () => void;
175
- handleView: (row?: unknown) => void;
176
+ handleView: (row?: TableRow) => void;
176
177
  headerActions: {
177
178
  enabled: boolean;
178
179
  handleOnClick: () => void;
@@ -180,6 +181,7 @@ export declare const useFamilyMemberModule: () => {
180
181
  order: number;
181
182
  icon: import("react").ForwardRefExoticComponent<Omit<import("lucide-react").LucideProps, "ref"> & import("react").RefAttributes<SVGSVGElement>>;
182
183
  }[];
184
+ listFetchNow: (url?: string, config?: import("@react-pakistan/util-functions/hooks/use-fetch").FetchConfig) => void;
183
185
  listLoading: boolean;
184
186
  rowActions: RowAction[];
185
187
  updateLoading: boolean;