@appcorp/fusion-storybook 0.5.21 → 0.5.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,6 @@ import { LS_KEYS } from "../../constants";
12
12
  const CACHE_CONFIG = {
13
13
  cacheKey: LS_KEYS.ACCOUNT_SCHOOL,
14
14
  apiUrl: SCHOOL_API_ROUTES.UNIT,
15
- responseKey: "item",
16
15
  };
17
16
  // ============================================================================
18
17
  // CACHE WRAPPERS
@@ -1,7 +1,5 @@
1
1
  export declare const SCHOOL_DRAWER: {
2
- readonly FILTER_DRAWER: string;
3
2
  readonly FORM_DRAWER: string;
4
- readonly MORE_ACTIONS_DRAWER: string;
5
3
  readonly VIEW_DRAWER: string;
6
4
  };
7
5
  export declare const SCHOOL_ACTION_TYPES: {
@@ -28,16 +28,14 @@ import { generateThemeToast, TOAST_VARIANT, } from "@appcorp/shadcn/lib/toast-ut
28
28
  import { SCHOOL_API_ROUTES } from "../../constants";
29
29
  import { invalidateSchoolCache } from "./cache";
30
30
  import { schoolFormValidation } from "./validate";
31
- import { getCachedWorkspaceSync } from "../workspace/cache";
31
+ import { getCachedWorkspaceSync, setCachedWorkspace } from "../workspace/cache";
32
32
  import { formatPhoneDisplay } from "@react-pakistan/util-functions/general/format-phone-display";
33
33
  import { toE164Phone } from "../../utils/to-e164-phone";
34
34
  // ============================================================================
35
35
  // 1.1 DRAWER TYPES
36
36
  // ============================================================================
37
37
  export const SCHOOL_DRAWER = {
38
- FILTER_DRAWER: DRAWER_TYPES.FILTER_DRAWER,
39
38
  FORM_DRAWER: DRAWER_TYPES.FORM_DRAWER,
40
- MORE_ACTIONS_DRAWER: DRAWER_TYPES.MORE_ACTIONS_DRAWER,
41
39
  VIEW_DRAWER: DRAWER_TYPES.VIEW_DRAWER,
42
40
  };
43
41
  // ============================================================================
@@ -110,7 +108,7 @@ export const useSchoolModule = () => {
110
108
  principalId: state.principalId,
111
109
  state: state.state,
112
110
  website: state.website,
113
- workspaceId: (workspace === null || workspace === void 0 ? void 0 : workspace.id) || "",
111
+ workspaceId: (workspace === null || workspace === void 0 ? void 0 : workspace.id) || state.workspaceId || "",
114
112
  });
115
113
  }, [
116
114
  state.address,
@@ -128,10 +126,12 @@ export const useSchoolModule = () => {
128
126
  state.principalId,
129
127
  state.state,
130
128
  state.website,
129
+ state.workspaceId,
131
130
  workspace === null || workspace === void 0 ? void 0 : workspace.id,
132
131
  (_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.country,
133
132
  ]);
134
- const byIdParams = useMemo(() => ({}), []);
133
+ const listParams = useMemo(() => ({ workspaceId: (workspace === null || workspace === void 0 ? void 0 : workspace.id) || "" }), [workspace === null || workspace === void 0 ? void 0 : workspace.id]);
134
+ const byIdParams = useMemo(() => ({ workspaceId: (workspace === null || workspace === void 0 ? void 0 : workspace.id) || "" }), [workspace === null || workspace === void 0 ? void 0 : workspace.id]);
135
135
  const deleteParams = useMemo(() => ({}), []);
136
136
  // ============================================================================
137
137
  // 1.4.3 UTILITIES
@@ -143,8 +143,11 @@ export const useSchoolModule = () => {
143
143
  variant,
144
144
  });
145
145
  }, [theme]);
146
+ /**
147
+ * The school is a single entity, so closing the drawer must not reset the
148
+ * record — otherwise the page renders an empty profile after saving.
149
+ */
146
150
  const resetFormAndCloseDrawer = useCallback(() => {
147
- dispatch({ type: SCHOOL_ACTION_TYPES.RESET_FORM });
148
151
  dispatch({
149
152
  type: SCHOOL_ACTION_TYPES.SET_ERRORS,
150
153
  payload: { errors: {} },
@@ -161,30 +164,77 @@ export const useSchoolModule = () => {
161
164
  // ============================================================================
162
165
  // 1.4.4 API CALLBACKS
163
166
  // ============================================================================
164
- const listCallback = useCallback(() => { }, []);
167
+ const listCallback = useCallback(({ data, error }) => {
168
+ // `GET /api/v1/school?workspaceId=…` returns the school itself; hydrate
169
+ // the page from it (errors are surfaced by the edit/refresh path).
170
+ if (error || !data)
171
+ return;
172
+ const school = data;
173
+ if (!school.id)
174
+ return;
175
+ dispatch({
176
+ type: SCHOOL_ACTION_TYPES.SET_FORM_DATA,
177
+ payload: {
178
+ form: Object.assign(Object.assign({}, school), { phone: formatPhoneDisplay(school.phone) || "" }),
179
+ },
180
+ });
181
+ }, [dispatch]);
165
182
  const updateCallback = useCallback(({ data, error }) => {
183
+ var _a;
166
184
  if (error) {
167
- showToast(t("messagesNetworkError"), TOAST_VARIANT.ERROR);
185
+ dispatch({
186
+ type: SCHOOL_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
187
+ payload: { disabled: false },
188
+ });
189
+ showToast((_a = data === null || data === void 0 ? void 0 : data.error) !== null && _a !== void 0 ? _a : t("messagesNetworkError"), TOAST_VARIANT.ERROR);
168
190
  return;
169
191
  }
170
192
  if (data) {
171
- showToast(t("messagesSaveSuccess"), TOAST_VARIANT.SUCCESS);
193
+ const school = data;
194
+ // Keep the view populated after save.
195
+ dispatch({
196
+ type: SCHOOL_ACTION_TYPES.SET_FORM_DATA,
197
+ payload: {
198
+ form: Object.assign(Object.assign({}, school), { phone: formatPhoneDisplay(school.phone) || "" }),
199
+ },
200
+ });
201
+ // The sidebar and every module read the workspace cache.
202
+ const cached = getCachedWorkspaceSync();
203
+ if (cached === null || cached === void 0 ? void 0 : cached.id) {
204
+ setCachedWorkspace(Object.assign(Object.assign({}, cached), { school }));
205
+ }
172
206
  invalidateSchoolCache();
207
+ showToast(t("messagesSaveSuccess"), TOAST_VARIANT.SUCCESS);
173
208
  resetFormAndCloseDrawer();
174
209
  }
175
- }, [resetFormAndCloseDrawer, showToast, t]);
176
- const byIdCallback = useCallback(() => { }, []);
210
+ }, [dispatch, resetFormAndCloseDrawer, showToast, t]);
211
+ const byIdCallback = useCallback(({ data, error }) => {
212
+ var _a;
213
+ if (error) {
214
+ showToast((_a = data === null || data === void 0 ? void 0 : data.error) !== null && _a !== void 0 ? _a : t("messagesNetworkError"), TOAST_VARIANT.ERROR);
215
+ return;
216
+ }
217
+ if (data) {
218
+ dispatch({
219
+ type: SCHOOL_ACTION_TYPES.SET_FORM_DATA,
220
+ payload: {
221
+ form: Object.assign(Object.assign({}, data), { phone: formatPhoneDisplay(data.phone) ||
222
+ "" }),
223
+ },
224
+ });
225
+ }
226
+ }, [dispatch, showToast, t]);
177
227
  const deleteCallback = useCallback(() => { }, []);
178
228
  // ============================================================================
179
229
  // 1.4.5 API HOOKS
180
230
  // ============================================================================
181
- const { updateFetchNow, updateLoading } = useModuleEntityV2({
231
+ const { byIdFetchNow, updateFetchNow, updateLoading } = useModuleEntityV2({
182
232
  byIdCallback,
183
233
  byIdParams,
184
234
  deleteCallback,
185
235
  deleteParams,
186
236
  listCallback,
187
- listParams: {},
237
+ listParams,
188
238
  listUrl: SCHOOL_API_ROUTES.UNIT,
189
239
  searchQuery: "",
190
240
  unitByIdUrl: SCHOOL_API_ROUTES.UNIT,
@@ -193,7 +243,6 @@ export const useSchoolModule = () => {
193
243
  updateParams,
194
244
  headers: {
195
245
  "Content-Type": "application/json",
196
- // "x-api-token": process.env.NEXT_PUBLIC_API_KEY!,
197
246
  },
198
247
  });
199
248
  // ============================================================================
@@ -216,18 +265,25 @@ export const useSchoolModule = () => {
216
265
  const closeDrawer = useCallback(() => {
217
266
  resetFormAndCloseDrawer();
218
267
  }, [resetFormAndCloseDrawer]);
268
+ const refreshSchool = useCallback(() => {
269
+ if (!(workspace === null || workspace === void 0 ? void 0 : workspace.id))
270
+ return;
271
+ byIdFetchNow === null || byIdFetchNow === void 0 ? void 0 : byIdFetchNow(undefined, { params: { workspaceId: workspace.id } });
272
+ }, [byIdFetchNow, workspace === null || workspace === void 0 ? void 0 : workspace.id]);
219
273
  const handleEdit = useCallback(() => {
274
+ refreshSchool();
220
275
  dispatch({
221
276
  type: SCHOOL_ACTION_TYPES.SET_DRAWER,
222
277
  payload: { drawer: SCHOOL_DRAWER.FORM_DRAWER },
223
278
  });
224
- }, [dispatch]);
279
+ }, [dispatch, refreshSchool]);
225
280
  const handleView = useCallback(() => {
281
+ refreshSchool();
226
282
  dispatch({
227
283
  type: SCHOOL_ACTION_TYPES.SET_DRAWER,
228
284
  payload: { drawer: SCHOOL_DRAWER.VIEW_DRAWER },
229
285
  });
230
- }, [dispatch]);
286
+ }, [dispatch, refreshSchool]);
231
287
  // ============================================================================
232
288
  // 1.4.7 NETWORK ACTIONS
233
289
  // ============================================================================
@@ -271,7 +327,7 @@ export const useSchoolModule = () => {
271
327
  // ============================================================================
272
328
  // 1.4.9 EFFECTS
273
329
  // ============================================================================
274
- // Load school data from workspace cache on mount
330
+ // Hydrate from the workspace cache immediately, then refresh from the API.
275
331
  useEffect(() => {
276
332
  if (workspace === null || workspace === void 0 ? void 0 : workspace.school) {
277
333
  dispatch({
@@ -281,7 +337,10 @@ export const useSchoolModule = () => {
281
337
  },
282
338
  });
283
339
  }
284
- }, []); // eslint-disable-line react-hooks/exhaustive-deps
340
+ // The list hook fetches `/school?workspaceId=…` on mount and hydrates
341
+ // through `listCallback`; later refreshes go through `refreshSchool`.
342
+ // eslint-disable-next-line react-hooks/exhaustive-deps
343
+ }, []);
285
344
  // ============================================================================
286
345
  // 1.4.10 RETURN
287
346
  // ============================================================================
@@ -12,15 +12,13 @@ import { useSchoolModule, SCHOOL_DRAWER } from "./context";
12
12
  import { useTranslations } from "next-intl";
13
13
  import { SchoolForm } from "./form";
14
14
  import { SchoolView } from "./view";
15
- import { SchoolMoreActions } from "./more-actions";
16
15
  export const SchoolDrawer = () => {
17
16
  const { closeDrawer, handleSubmit, state, updateLoading } = useSchoolModule();
18
17
  const t = useTranslations("school");
19
18
  const { disableSaveButton, drawer, errors } = state;
20
19
  const isFormDrawer = drawer === SCHOOL_DRAWER.FORM_DRAWER;
21
20
  const isViewDrawer = drawer === SCHOOL_DRAWER.VIEW_DRAWER;
22
- const isMoreActionsDrawer = drawer === SCHOOL_DRAWER.MORE_ACTIONS_DRAWER;
23
21
  const applyFilters = () => { };
24
22
  const clearFilters = () => { };
25
- return (_jsx(DrawerGeneric, { filterDrawer: null, filterDrawerHeader: null, filterDrawerFooter: null, formDrawer: isFormDrawer ? _jsx(SchoolForm, {}) : null, formDrawerHeader: isFormDrawer ? (_jsx(EnhancedDrawerHeader, { title: t("drawerFormTitle"), description: t("drawerFormDescription") })) : null, formDrawerFooter: isFormDrawer ? (_jsx(EnhancedDrawerFooter, { applyFilters: applyFilters, clearFilters: clearFilters, closeDrawer: closeDrawer, disableSaveButton: disableSaveButton, drawerType: DRAWER_FOOTER_COMPONENT_TYPE.FORM_DRAWER, errors: errors, handleSubmit: handleSubmit, updateLoading: updateLoading })) : null, viewDrawer: isViewDrawer ? _jsx(SchoolView, {}) : null, viewDrawerHeader: isViewDrawer ? (_jsx(EnhancedDrawerHeader, { title: t("drawerViewTitle"), description: t("drawerViewDescription") })) : null, viewDrawerFooter: isViewDrawer ? (_jsx(EnhancedDrawerFooter, { applyFilters: applyFilters, clearFilters: clearFilters, closeDrawer: closeDrawer, disableSaveButton: disableSaveButton, drawerType: DRAWER_FOOTER_COMPONENT_TYPE.VIEW_DRAWER, errors: errors, handleSubmit: handleSubmit, updateLoading: updateLoading })) : null, moreActionsDrawer: isMoreActionsDrawer ? _jsx(SchoolMoreActions, {}) : null, moreActionsDrawerHeader: isMoreActionsDrawer ? (_jsx(EnhancedDrawerHeader, { title: t("drawerMoreActionsTitle"), description: t("drawerMoreActionsDescription") })) : null, moreActionsDrawerFooter: isMoreActionsDrawer ? (_jsx(EnhancedDrawerFooter, { applyFilters: applyFilters, clearFilters: clearFilters, closeDrawer: closeDrawer, disableSaveButton: disableSaveButton, drawerType: DRAWER_FOOTER_COMPONENT_TYPE.MORE_ACTIONS_DRAWER, errors: errors, handleSubmit: handleSubmit, updateLoading: updateLoading })) : null, onOpenChange: (open) => !open && closeDrawer(), open: isFormDrawer || isViewDrawer || isMoreActionsDrawer }));
23
+ return (_jsx(DrawerGeneric, { filterDrawer: null, filterDrawerHeader: null, filterDrawerFooter: null, formDrawer: isFormDrawer ? _jsx(SchoolForm, {}) : null, formDrawerHeader: isFormDrawer ? (_jsx(EnhancedDrawerHeader, { title: t("drawerFormTitle"), description: t("drawerFormDescription") })) : null, formDrawerFooter: isFormDrawer ? (_jsx(EnhancedDrawerFooter, { applyFilters: applyFilters, clearFilters: clearFilters, closeDrawer: closeDrawer, disableSaveButton: disableSaveButton, drawerType: DRAWER_FOOTER_COMPONENT_TYPE.FORM_DRAWER, errors: errors, handleSubmit: handleSubmit, updateLoading: updateLoading })) : null, viewDrawer: isViewDrawer ? _jsx(SchoolView, {}) : null, viewDrawerHeader: isViewDrawer ? (_jsx(EnhancedDrawerHeader, { title: t("drawerViewTitle"), description: t("drawerViewDescription") })) : null, viewDrawerFooter: isViewDrawer ? (_jsx(EnhancedDrawerFooter, { applyFilters: applyFilters, clearFilters: clearFilters, closeDrawer: closeDrawer, disableSaveButton: disableSaveButton, drawerType: DRAWER_FOOTER_COMPONENT_TYPE.VIEW_DRAWER, errors: errors, handleSubmit: handleSubmit, updateLoading: updateLoading })) : null, moreActionsDrawer: null, moreActionsDrawerHeader: null, moreActionsDrawerFooter: null, onOpenChange: (open) => !open && closeDrawer(), open: isFormDrawer || isViewDrawer }));
26
24
  };
@@ -7,10 +7,9 @@ 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 { useEnhancedCombobox } from "@appcorp/shadcn/hooks/use-enhanced-combobox";
10
+ import { EnhancedCombobox } from "@appcorp/shadcn/components/enhanced-combobox";
11
11
  import { useSchoolModule } from "./context";
12
12
  import { CURRENCY } from "../../type";
13
- import { SCHOOL_API_ROUTES } from "../../constants";
14
13
  import { useTranslations } from "next-intl";
15
14
  import { formatPhone } from "@react-pakistan/util-functions/general/format-phone";
16
15
  import { getTranslatedError } from "../../utils/get-translated-error";
@@ -20,29 +19,11 @@ export const SchoolForm = () => {
20
19
  const { address, city, code, country, currency, email, enabled, errors, logo, name, phone, postalCode,
21
20
  // principalId,
22
21
  state: stateField, website, } = state;
23
- const { enhancedComboboxElement: currencyCombo } = useEnhancedCombobox({
24
- emptyText: t("formCurrencyEmptyText"),
25
- id: "currency",
26
- info: t("formCurrencyInfo"),
27
- label: t("formCurrencyLabel"),
28
- onValueChange: (value) => handleChange("currency", value),
29
- options: Object.values(CURRENCY).map((c) => ({
22
+ // Currency is a static enum — no remote search endpoint.
23
+ const currencyCombo = (_jsx(EnhancedCombobox, { emptyText: t("formCurrencyEmptyText"), error: getTranslatedError({ errors, key: "currency", t }), id: "currency", info: t("formCurrencyInfo"), label: t("formCurrencyLabel"), onValueChange: (value) => handleChange("currency", value), options: Object.values(CURRENCY).map((c) => ({
30
24
  value: c,
31
25
  label: c,
32
- })),
33
- placeholder: t("formCurrencyPlaceholder"),
34
- required: true,
35
- searchEndpoint: SCHOOL_API_ROUTES.UNIT,
36
- searchPlaceholder: t("formCurrencySearchPlaceholder"),
37
- formatSearchResult: (item) => {
38
- var _a, _b;
39
- return ({
40
- label: String((_a = item.label) !== null && _a !== void 0 ? _a : ""),
41
- value: String((_b = item.value) !== null && _b !== void 0 ? _b : ""),
42
- });
43
- },
44
- value: currency,
45
- });
26
+ })), placeholder: t("formCurrencyPlaceholder"), required: true, value: currency }));
46
27
  // const { enhancedComboboxElement: principalIdCombo } = useEnhancedCombobox({
47
28
  // emptyText: t("formPrincipalEmptyText"),
48
29
  // id: "principalId",
@@ -28,10 +28,10 @@ const SchoolPageInner = ({ pageTitle, pageDescription, labelEdit, userRole, }) =
28
28
  const { handleEdit } = useSchoolModule();
29
29
  const hasPermission = resolveRbacPermissions({
30
30
  userRole,
31
- moduleName: "Workspace",
31
+ moduleName: "School",
32
32
  });
33
33
  if (!hasPermission) {
34
- return _jsx(RbacNoAccess, { moduleName: "Workspace" });
34
+ return _jsx(RbacNoAccess, { moduleName: "School" });
35
35
  }
36
36
  return (_jsx("div", { className: "container mx-auto px-4 py-8", children: _jsxs("div", { className: "mx-auto max-w-6xl", children: [_jsxs("div", { className: "mb-8", children: [_jsx("h1", { className: "text-3xl font-bold text-gray-900", children: pageTitle }), _jsx("p", { className: "mt-2 text-gray-600", children: pageDescription })] }), _jsx("div", { className: "mb-6 flex justify-end", children: _jsxs(Button, { onClick: handleEdit, className: "flex items-center space-x-2", children: [_jsx(Edit, { className: "h-4 w-4" }), _jsx("span", { children: labelEdit })] }) }), _jsx(SchoolView, {}), _jsx(SchoolDrawer, {})] }) }));
37
37
  };
@@ -33,6 +33,7 @@ export declare const SECTION_ACTION_TYPES: {
33
33
  name: string;
34
34
  schoolId: string;
35
35
  filterEnabled: boolean | undefined;
36
+ appliedFilterEnabled: boolean | undefined;
36
37
  }>, initialSectionState: {
37
38
  items: SectionBE[];
38
39
  count: number;
@@ -48,6 +49,7 @@ export declare const SECTION_ACTION_TYPES: {
48
49
  name: string;
49
50
  schoolId: string;
50
51
  filterEnabled: boolean | undefined;
52
+ appliedFilterEnabled: boolean | undefined;
51
53
  }, SectionProvider: import("react").FC<{
52
54
  children: React.ReactNode;
53
55
  }>, sectionReducer: (state: {
@@ -65,6 +67,7 @@ export declare const SECTION_ACTION_TYPES: {
65
67
  name: string;
66
68
  schoolId: string;
67
69
  filterEnabled: boolean | undefined;
70
+ appliedFilterEnabled: boolean | undefined;
68
71
  }, action: any) => {
69
72
  items: SectionBE[];
70
73
  count: number;
@@ -80,6 +83,7 @@ export declare const SECTION_ACTION_TYPES: {
80
83
  name: string;
81
84
  schoolId: string;
82
85
  filterEnabled: boolean | undefined;
86
+ appliedFilterEnabled: boolean | undefined;
83
87
  }, useSectionContext: () => import("@react-pakistan/util-functions/factory/generic-module-factory").GenericModuleContextWithHandlers<{
84
88
  items: SectionBE[];
85
89
  count: number;
@@ -95,6 +99,7 @@ export declare const SECTION_ACTION_TYPES: {
95
99
  name: string;
96
100
  schoolId: string;
97
101
  filterEnabled: boolean | undefined;
102
+ appliedFilterEnabled: boolean | undefined;
98
103
  }>;
99
104
  export declare const useSectionModule: () => {
100
105
  applyFilters: () => void;
@@ -143,6 +148,7 @@ export declare const useSectionModule: () => {
143
148
  name: string;
144
149
  schoolId: string;
145
150
  filterEnabled: boolean | undefined;
151
+ appliedFilterEnabled: boolean | undefined;
146
152
  };
147
153
  dispatch: React.Dispatch<any>;
148
154
  };
@@ -63,7 +63,8 @@ const sectionConfig = {
63
63
  name: "",
64
64
  schoolId: "",
65
65
  // Filter State
66
- filterEnabled: undefined,
66
+ filterEnabled: true,
67
+ appliedFilterEnabled: true,
67
68
  },
68
69
  };
69
70
  // ============================================================================
@@ -79,7 +80,7 @@ export const useSectionModule = () => {
79
80
  // 1.4.1 STATE & CORE HOOKS
80
81
  // ============================================================================
81
82
  const context = useSectionContext();
82
- const { dispatch } = context;
83
+ const { dispatch, handleCloseDrawer } = context;
83
84
  const state = context.state;
84
85
  const t = useTranslations("section");
85
86
  const { theme } = useTheme();
@@ -90,11 +91,11 @@ export const useSectionModule = () => {
90
91
  // ============================================================================
91
92
  // 1.4.2 API PARAMETERS
92
93
  // ============================================================================
93
- const listParams = useMemo(() => (Object.assign(Object.assign({ currentPage: state.currentPage, pageLimit: state.pageLimit, schoolId }, (debouncedQuery ? { searchQuery: debouncedQuery } : {})), (state.filterEnabled !== undefined
94
- ? { filterEnabled: String(state.filterEnabled) }
94
+ const listParams = useMemo(() => (Object.assign(Object.assign({ currentPage: state.currentPage, pageLimit: state.pageLimit, schoolId }, (debouncedQuery ? { searchQuery: debouncedQuery } : {})), (state.appliedFilterEnabled !== undefined
95
+ ? { filterEnabled: String(state.appliedFilterEnabled) }
95
96
  : {}))), [
97
+ state.appliedFilterEnabled,
96
98
  state.currentPage,
97
- state.filterEnabled,
98
99
  state.pageLimit,
99
100
  debouncedQuery,
100
101
  schoolId,
@@ -106,8 +107,8 @@ export const useSectionModule = () => {
106
107
  name: state.name,
107
108
  schoolId: state.schoolId || schoolId,
108
109
  }), [state, schoolId]);
109
- const byIdParams = useMemo(() => ({ id: state.id }), [state.id]);
110
- const deleteParams = useMemo(() => ({ id: state.id }), [state.id]);
110
+ const byIdParams = useMemo(() => ({ id: state.id, schoolId }), [state.id, schoolId]);
111
+ const deleteParams = useMemo(() => ({ id: state.id, schoolId }), [state.id, schoolId]);
111
112
  // ============================================================================
112
113
  // 1.4.3 UTILITIES
113
114
  // ============================================================================
@@ -119,8 +120,19 @@ export const useSectionModule = () => {
119
120
  });
120
121
  }, [theme]);
121
122
  const resetFormAndCloseDrawer = useCallback(() => {
123
+ // `RESET_FORM` restores the initial state, which would wipe the list
124
+ // filter; preserve the staged/applied values.
125
+ const applied = state.appliedFilterEnabled;
126
+ const staged = state.filterEnabled;
127
+ dispatch({ type: SECTION_ACTION_TYPES.RESET_FORM });
122
128
  dispatch({
123
- type: SECTION_ACTION_TYPES.RESET_FORM,
129
+ type: SECTION_ACTION_TYPES.SET_FILTERS,
130
+ payload: {
131
+ filters: {
132
+ appliedFilterEnabled: applied,
133
+ filterEnabled: staged,
134
+ },
135
+ },
124
136
  });
125
137
  dispatch({
126
138
  type: SECTION_ACTION_TYPES.SET_ERRORS,
@@ -134,7 +146,7 @@ export const useSectionModule = () => {
134
146
  type: SECTION_ACTION_TYPES.SET_DRAWER,
135
147
  payload: { drawer: null },
136
148
  });
137
- }, [dispatch]);
149
+ }, [dispatch, state.appliedFilterEnabled, state.filterEnabled]);
138
150
  const resetRecordFormState = useCallback(() => {
139
151
  dispatch({
140
152
  type: SECTION_ACTION_TYPES.SET_ERRORS,
@@ -150,7 +162,6 @@ export const useSectionModule = () => {
150
162
  form: {
151
163
  capacity: null,
152
164
  enabled: true,
153
- filterEnabled: undefined,
154
165
  id: "",
155
166
  name: "",
156
167
  schoolId,
@@ -161,54 +172,61 @@ export const useSectionModule = () => {
161
172
  // ============================================================================
162
173
  // 1.4.4 API CALLBACKS
163
174
  // ============================================================================
175
+ const apiError = (data) => data === null || data === void 0 ? void 0 : data.error;
164
176
  const listCallback = useCallback(({ data, error }) => {
165
- var _a, _b;
177
+ var _a, _b, _c;
166
178
  if (error) {
167
- showToast(t("messagesFetchFailed"), TOAST_VARIANT.ERROR);
179
+ showToast((_a = apiError(data)) !== null && _a !== void 0 ? _a : t("messagesFetchFailed"), TOAST_VARIANT.ERROR);
168
180
  return;
169
181
  }
170
182
  if (data) {
171
183
  const response = data;
172
184
  dispatch({
173
185
  type: SECTION_ACTION_TYPES.SET_ITEMS,
174
- payload: { items: (_a = response.items) !== null && _a !== void 0 ? _a : [], count: (_b = response.count) !== null && _b !== void 0 ? _b : 0 },
186
+ payload: { items: (_b = response.items) !== null && _b !== void 0 ? _b : [], count: (_c = response.count) !== null && _c !== void 0 ? _c : 0 },
175
187
  });
176
188
  }
177
189
  }, [dispatch, showToast, t]);
178
190
  const updateCallback = useCallback(({ data, error }) => {
179
- var _a;
191
+ var _a, _b;
180
192
  if (error) {
181
- showToast(t("messagesUpdateFailed"), TOAST_VARIANT.ERROR);
193
+ dispatch({
194
+ type: SECTION_ACTION_TYPES.SET_DISABLE_SAVE_BUTTON,
195
+ payload: { disabled: false },
196
+ });
197
+ showToast((_a = apiError(data)) !== null && _a !== void 0 ? _a : t("messagesUpdateFailed"), TOAST_VARIANT.ERROR);
182
198
  return;
183
199
  }
184
200
  if (data) {
185
201
  const isCreated = isCreatedOrUpdated(data);
186
202
  showToast(isCreated ? t("messagesCreateSuccess") : t("messagesSaveSuccess"), TOAST_VARIANT.SUCCESS);
187
203
  resetFormAndCloseDrawer();
188
- (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
204
+ (_b = listFetchNowRef.current) === null || _b === void 0 ? void 0 : _b.call(listFetchNowRef);
189
205
  }
190
- }, [resetFormAndCloseDrawer, showToast, t]);
206
+ }, [dispatch, resetFormAndCloseDrawer, showToast, t]);
191
207
  const byIdCallback = useCallback(({ data, error }) => {
208
+ var _a;
192
209
  if (error) {
193
- showToast(t("messagesDetailsFetchFailed"), TOAST_VARIANT.ERROR);
210
+ showToast((_a = apiError(data)) !== null && _a !== void 0 ? _a : t("messagesDetailsFetchFailed"), TOAST_VARIANT.ERROR);
194
211
  return;
195
212
  }
196
213
  if (data) {
214
+ // Keep the staged filter untouched when opening a record.
197
215
  dispatch({
198
216
  type: SECTION_ACTION_TYPES.SET_FORM_DATA,
199
- payload: { form: Object.assign(Object.assign({}, data), { filterEnabled: undefined }) },
217
+ payload: { form: data },
200
218
  });
201
219
  }
202
220
  }, [dispatch, showToast, t]);
203
221
  const deleteCallback = useCallback(({ data, error }) => {
204
- var _a;
222
+ var _a, _b;
205
223
  if (error) {
206
- showToast(t("messagesDeleteFailed"), TOAST_VARIANT.ERROR);
224
+ showToast((_a = apiError(data)) !== null && _a !== void 0 ? _a : t("messagesDeleteFailed"), TOAST_VARIANT.ERROR);
207
225
  return;
208
226
  }
209
227
  if (data) {
210
228
  showToast(t("messagesDeleteSuccess"), TOAST_VARIANT.SUCCESS);
211
- (_a = listFetchNowRef.current) === null || _a === void 0 ? void 0 : _a.call(listFetchNowRef);
229
+ (_b = listFetchNowRef.current) === null || _b === void 0 ? void 0 : _b.call(listFetchNowRef);
212
230
  }
213
231
  }, [showToast, t]);
214
232
  // ============================================================================
@@ -253,11 +271,14 @@ export const useSectionModule = () => {
253
271
  resetFormAndCloseDrawer();
254
272
  }, [resetFormAndCloseDrawer]);
255
273
  const handleCreate = useCallback(() => {
274
+ // Reset first: opening Add after View/Edit must not update the previous
275
+ // record with its stale id.
276
+ resetRecordFormState();
256
277
  dispatch({
257
278
  type: SECTION_ACTION_TYPES.SET_DRAWER,
258
279
  payload: { drawer: SECTION_DRAWER.FORM_DRAWER },
259
280
  });
260
- }, [dispatch]);
281
+ }, [dispatch, resetRecordFormState]);
261
282
  const handleView = useCallback((row) => {
262
283
  resetRecordFormState();
263
284
  byIdFetchNow === null || byIdFetchNow === void 0 ? void 0 : byIdFetchNow(undefined, { params: { id: row === null || row === void 0 ? void 0 : row.id } });
@@ -278,9 +299,9 @@ export const useSectionModule = () => {
278
299
  if (!confirm(t("messagesDeleteConfirmation")))
279
300
  return;
280
301
  deleteFetchNow === null || deleteFetchNow === void 0 ? void 0 : deleteFetchNow(undefined, {
281
- body: JSON.stringify({ id: row === null || row === void 0 ? void 0 : row.id }),
302
+ body: JSON.stringify({ id: row === null || row === void 0 ? void 0 : row.id, schoolId }),
282
303
  });
283
- }, [deleteFetchNow, t]);
304
+ }, [deleteFetchNow, schoolId, t]);
284
305
  const handleFilters = useCallback(() => {
285
306
  dispatch({
286
307
  type: SECTION_ACTION_TYPES.SET_DRAWER,
@@ -337,27 +358,38 @@ export const useSectionModule = () => {
337
358
  });
338
359
  }, [dispatch]);
339
360
  const applyFilters = useCallback(() => {
361
+ dispatch({
362
+ type: SECTION_ACTION_TYPES.SET_FILTERS,
363
+ payload: { filters: { appliedFilterEnabled: state.filterEnabled } },
364
+ });
340
365
  dispatch({
341
366
  type: SECTION_ACTION_TYPES.SET_CURRENT_PAGE,
342
367
  payload: { currentPage: 1 },
343
368
  });
344
- closeDrawer();
345
- }, [dispatch, closeDrawer]);
369
+ handleCloseDrawer === null || handleCloseDrawer === void 0 ? void 0 : handleCloseDrawer();
370
+ }, [dispatch, handleCloseDrawer, state.filterEnabled]);
346
371
  const clearFilters = useCallback(() => {
347
372
  dispatch({
348
373
  type: SECTION_ACTION_TYPES.SET_FILTERS,
349
- payload: { filters: { filterEnabled: undefined } },
374
+ payload: {
375
+ filters: { filterEnabled: undefined, appliedFilterEnabled: undefined },
376
+ },
350
377
  });
351
378
  dispatch({
352
379
  type: SECTION_ACTION_TYPES.SET_CURRENT_PAGE,
353
380
  payload: { currentPage: 1 },
354
381
  });
355
- }, [dispatch]);
382
+ handleCloseDrawer === null || handleCloseDrawer === void 0 ? void 0 : handleCloseDrawer();
383
+ }, [dispatch, handleCloseDrawer]);
356
384
  const toggleStatus = useCallback((row) => {
357
385
  updateFetchNow(undefined, {
358
- body: JSON.stringify(Object.assign(Object.assign({}, row), { enabled: !(row === null || row === void 0 ? void 0 : row.enabled) })),
386
+ body: JSON.stringify({
387
+ id: row === null || row === void 0 ? void 0 : row.id,
388
+ schoolId,
389
+ enabled: !(row === null || row === void 0 ? void 0 : row.enabled),
390
+ }),
359
391
  });
360
- }, [updateFetchNow]);
392
+ }, [schoolId, updateFetchNow]);
361
393
  // ============================================================================
362
394
  // 1.4.7 SUBMIT
363
395
  // ============================================================================
@@ -418,20 +450,20 @@ export const useSectionModule = () => {
418
450
  order: 1,
419
451
  },
420
452
  {
421
- enabled: (row) => (row === null || row === void 0 ? void 0 : row.enabled) === true,
453
+ enabled: true,
422
454
  handleOnClick: handleEdit,
423
455
  label: t("actionsButtonEdit"),
424
456
  order: 2,
425
457
  },
426
458
  {
427
- enabled: (row) => (row === null || row === void 0 ? void 0 : row.enabled) === false,
459
+ enabled: (row) => (row === null || row === void 0 ? void 0 : row.enabled) === true,
428
460
  handleOnClick: handleDelete,
429
461
  label: t("actionsButtonDelete"),
430
462
  order: 3,
431
463
  variant: "destructive",
432
464
  },
433
465
  {
434
- enabled: false,
466
+ enabled: true,
435
467
  handleOnClick: toggleStatus,
436
468
  label: t("actionsButtonToggleStatus"),
437
469
  order: 4,
@@ -17,6 +17,6 @@ export const SectionForm = () => {
17
17
  const t = useTranslations("section");
18
18
  return (_jsxs("div", { className: "grid grid-cols-1 gap-4", children: [_jsx(EnhancedInput, { error: getTranslatedError({ errors, key: "name", t }), id: "name", info: t("formSectionNameInfo"), label: t("formSectionNameLabel"), onChange: (e) => handleChange("name", e.target.value), placeholder: t("formSectionNamePlaceholder"), required: true, value: name }), _jsx(EnhancedInput, { error: getTranslatedError({ errors, key: "capacity", t }), id: "capacity", info: t("formCapacityInfo"), label: t("formCapacityLabel"), onChange: (e) => {
19
19
  const value = e.target.value;
20
- handleChange("capacity", value ? Number(value) : 0);
21
- }, placeholder: t("formCapacityPlaceholder"), type: "number", value: (capacity === null || capacity === void 0 ? void 0 : capacity.toString()) || "" }), _jsx(EnhancedCheckbox, { label: t("formEnabledLabel"), defaultChecked: enabled, onCheckedChange: (checked) => handleChange("enabled", checked), info: t("formEnabledInfo") })] }));
20
+ handleChange("capacity", value ? Number(value) : null);
21
+ }, placeholder: t("formCapacityPlaceholder"), type: "number", value: (capacity === null || capacity === void 0 ? void 0 : capacity.toString()) || "" }), _jsx(EnhancedCheckbox, { checked: enabled, label: t("formEnabledLabel"), onCheckedChange: (checked) => handleChange("enabled", checked), info: t("formEnabledInfo") })] }));
22
22
  };