@appcorp/fusion-storybook 0.1.86 → 0.1.87
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.
|
@@ -9,20 +9,94 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
9
9
|
import { EnhancedInput } from "@appcorp/shadcn/components/enhanced-input";
|
|
10
10
|
import { EnhancedCombobox } from "@appcorp/shadcn/components/enhanced-combobox";
|
|
11
11
|
import { EnhancedCheckbox } from "@appcorp/shadcn/components/enhanced-checkbox";
|
|
12
|
+
import { useDebounce } from "@react-pakistan/util-functions/hooks/use-debounce";
|
|
13
|
+
import { useEffect, useMemo, useState } from "react";
|
|
12
14
|
import { useSectionModule } from "./context";
|
|
13
15
|
import { useTranslations } from "next-intl";
|
|
14
|
-
import { getCachedClassesSync } from "../class/cache";
|
|
16
|
+
import { getCachedClasses, getCachedClassesSync } from "../class/cache";
|
|
17
|
+
import { getCachedWorkspaceSync } from "../workspace/cache";
|
|
15
18
|
export const SectionForm = () => {
|
|
19
|
+
var _a;
|
|
16
20
|
const { state, handleChange } = useSectionModule();
|
|
17
21
|
const { capacity, classId, enabled, errors, name } = state;
|
|
18
22
|
const t = useTranslations("section");
|
|
19
|
-
const
|
|
20
|
-
const
|
|
23
|
+
const workspace = getCachedWorkspaceSync();
|
|
24
|
+
const schoolId = ((_a = workspace === null || workspace === void 0 ? void 0 : workspace.school) === null || _a === void 0 ? void 0 : _a.id) || "";
|
|
25
|
+
const cachedClasses = getCachedClassesSync();
|
|
26
|
+
const [classSearchQuery, setClassSearchQuery] = useState("");
|
|
27
|
+
const [remoteClassOptions, setRemoteClassOptions] = useState(() => cachedClasses.items.map((cls) => ({
|
|
21
28
|
label: `${cls.name} (${cls.code})`,
|
|
22
29
|
value: cls.id,
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
})));
|
|
31
|
+
const [classOptionsLoading, setClassOptionsLoading] = useState(false);
|
|
32
|
+
const debouncedClassSearchQuery = useDebounce(classSearchQuery, 300);
|
|
33
|
+
const trimmedClassSearchQuery = debouncedClassSearchQuery.trim();
|
|
34
|
+
const cachedClassOptions = useMemo(() => cachedClasses.items.map((cls) => ({
|
|
35
|
+
label: `${cls.name} (${cls.code})`,
|
|
36
|
+
value: cls.id,
|
|
37
|
+
})), [cachedClasses.items]);
|
|
38
|
+
const selectedClassOption = useMemo(() => {
|
|
39
|
+
const selectedClass = cachedClasses.items.find((item) => item.id === classId);
|
|
40
|
+
return selectedClass
|
|
41
|
+
? [
|
|
42
|
+
{
|
|
43
|
+
label: `${selectedClass.name} (${selectedClass.code})`,
|
|
44
|
+
value: selectedClass.id,
|
|
45
|
+
},
|
|
46
|
+
]
|
|
47
|
+
: [];
|
|
48
|
+
}, [cachedClasses.items, classId]);
|
|
49
|
+
const displayedClassOptions = useMemo(() => {
|
|
50
|
+
const sourceOptions = trimmedClassSearchQuery
|
|
51
|
+
? remoteClassOptions
|
|
52
|
+
: cachedClassOptions;
|
|
53
|
+
const mergedOptions = [...selectedClassOption, ...sourceOptions];
|
|
54
|
+
const uniqueOptions = new Map(mergedOptions.map((option) => [option.value, option]));
|
|
55
|
+
return [...uniqueOptions.values()];
|
|
56
|
+
}, [
|
|
57
|
+
cachedClassOptions,
|
|
58
|
+
remoteClassOptions,
|
|
59
|
+
selectedClassOption,
|
|
60
|
+
trimmedClassSearchQuery,
|
|
61
|
+
]);
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
if (!trimmedClassSearchQuery || !schoolId)
|
|
64
|
+
return;
|
|
65
|
+
let isActive = true;
|
|
66
|
+
const fetchClasses = async () => {
|
|
67
|
+
setClassOptionsLoading(true);
|
|
68
|
+
try {
|
|
69
|
+
const { items } = await getCachedClasses({
|
|
70
|
+
params: {
|
|
71
|
+
schoolId,
|
|
72
|
+
searchQuery: trimmedClassSearchQuery,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
if (!isActive)
|
|
76
|
+
return;
|
|
77
|
+
setRemoteClassOptions((items || []).map((cls) => ({
|
|
78
|
+
label: `${cls.name} (${cls.code})`,
|
|
79
|
+
value: cls.id,
|
|
80
|
+
})));
|
|
81
|
+
}
|
|
82
|
+
catch (_a) {
|
|
83
|
+
if (!isActive)
|
|
84
|
+
return;
|
|
85
|
+
setRemoteClassOptions([]);
|
|
86
|
+
}
|
|
87
|
+
finally {
|
|
88
|
+
if (isActive) {
|
|
89
|
+
setClassOptionsLoading(false);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
void fetchClasses();
|
|
94
|
+
return () => {
|
|
95
|
+
isActive = false;
|
|
96
|
+
};
|
|
97
|
+
}, [schoolId, trimmedClassSearchQuery]);
|
|
98
|
+
return (_jsxs("div", { className: "grid grid-cols-1 gap-4", children: [_jsx(EnhancedCombobox, { error: errors.classId, id: "classId", info: t("classInfo"), label: t("class"), loading: classOptionsLoading && Boolean(trimmedClassSearchQuery), onValueChange: (value) => handleChange("classId", value), onSearchChange: setClassSearchQuery, options: displayedClassOptions, searchPlaceholder: t("searchClasses"), emptyText: t("noClassFound"), required: true, value: classId }), _jsx(EnhancedInput, { error: errors.name, id: "name", info: t("sectionNameInfo"), label: t("sectionName"), onChange: (e) => handleChange("name", e.target.value), placeholder: t("sectionNamePlaceholder"), required: true, value: name }), _jsx(EnhancedInput, { error: errors.capacity, id: "capacity", info: t("capacityInfo"), label: t("capacity"), onChange: (e) => {
|
|
99
|
+
const value = e.target.value;
|
|
100
|
+
handleChange("capacity", value ? Number(value) : 0);
|
|
101
|
+
}, placeholder: t("capacityPlaceholder"), type: "number", value: (capacity === null || capacity === void 0 ? void 0 : capacity.toString()) || "" }), _jsx(EnhancedCheckbox, { label: t("activeSection"), defaultChecked: enabled, onCheckedChange: (checked) => handleChange("enabled", checked), info: t("toggleToActivateOrDeactivateSection") })] }));
|
|
28
102
|
};
|