@fctc/widget-logic 1.1.4 → 1.1.5

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/dist/utils.mjs ADDED
@@ -0,0 +1,239 @@
1
+ // src/utils/constants.ts
2
+ var languages = [
3
+ { id: "vi_VN", name: "VIE" },
4
+ { id: "en_US", name: "ENG" }
5
+ ];
6
+ var API_PRESCHOOL_URL = {
7
+ baseURL: "https://preschool.vitrust.app"
8
+ };
9
+ var API_APP_URL = {
10
+ baseUrl: "https://api.vitrust.app",
11
+ c2: "https://api.vitrust.app/c2",
12
+ apiV2: "https://api.vitrust.app/c2/api/v2"
13
+ };
14
+
15
+ // src/utils/function.ts
16
+ import { useCallback, useEffect, useReducer, useRef, useState } from "react";
17
+ var countSum = (data, field) => {
18
+ if (!data || !field) return 0;
19
+ return data.reduce(
20
+ (total, item) => total + (item?.[`${field}_count`] || 0),
21
+ 0
22
+ );
23
+ };
24
+ function mergeButtons(fields) {
25
+ const buttons = fields?.filter((f) => f.type_co === "button");
26
+ const others = fields?.filter((f) => f.type_co !== "button");
27
+ if (buttons?.length) {
28
+ others.push({
29
+ type_co: "buttons",
30
+ buttons
31
+ });
32
+ }
33
+ return others;
34
+ }
35
+ function isElementVisible(el) {
36
+ const style = window.getComputedStyle(el);
37
+ return style.display !== "none" && style.visibility !== "hidden" && style.opacity !== "0";
38
+ }
39
+ function arraysAreEqual(a, b) {
40
+ if (a.length !== b.length) return false;
41
+ const setA = new Set(a);
42
+ const setB = new Set(b);
43
+ if (setA.size !== setB.size) return false;
44
+ for (const val of setA) {
45
+ if (!setB.has(val)) return false;
46
+ }
47
+ return true;
48
+ }
49
+ function useGetRowIds(tableRef) {
50
+ const [rowIds, setRowIds] = useState([]);
51
+ const lastRowIdsRef = useRef([]);
52
+ const updateVisibleRowIds = useCallback(() => {
53
+ const table = tableRef?.current;
54
+ if (!table) return;
55
+ const rows = table.querySelectorAll("tr[data-row-id]");
56
+ const ids = [];
57
+ rows.forEach((row) => {
58
+ const el = row;
59
+ if (isElementVisible(el)) {
60
+ const id = el.getAttribute("data-row-id");
61
+ if (id) ids.push(id);
62
+ }
63
+ });
64
+ const uniqueIds = Array.from(new Set(ids));
65
+ if (!arraysAreEqual(lastRowIdsRef.current, uniqueIds)) {
66
+ lastRowIdsRef.current = uniqueIds;
67
+ setRowIds(uniqueIds);
68
+ }
69
+ }, [tableRef]);
70
+ useEffect(() => {
71
+ const table = tableRef?.current;
72
+ if (!table) return;
73
+ const observer = new MutationObserver(() => {
74
+ updateVisibleRowIds();
75
+ });
76
+ observer.observe(table, {
77
+ childList: true,
78
+ subtree: true,
79
+ attributes: true,
80
+ attributeFilter: ["style", "class"]
81
+ });
82
+ updateVisibleRowIds();
83
+ return () => {
84
+ observer.disconnect();
85
+ };
86
+ }, [updateVisibleRowIds, tableRef]);
87
+ return { rowIds, refresh: updateVisibleRowIds };
88
+ }
89
+ var getDateRange = (currentDate, unit) => {
90
+ const date = new Date(currentDate);
91
+ let dateStart, dateEnd;
92
+ function formatDate(d) {
93
+ return d.getFullYear() + "-" + String(d.getMonth() + 1).padStart(2, "0") + "-" + String(d.getDate()).padStart(2, "0") + " " + String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0") + ":" + String(d.getSeconds()).padStart(2, "0");
94
+ }
95
+ switch (unit) {
96
+ case "month":
97
+ dateStart = new Date(
98
+ date.getFullYear(),
99
+ date.getMonth() + 1,
100
+ date.getDate(),
101
+ 23,
102
+ 59,
103
+ 59
104
+ );
105
+ dateStart.setHours(dateStart.getHours() - 7);
106
+ dateEnd = new Date(date.getFullYear(), date.getMonth(), 0, 0, 0, 0);
107
+ dateEnd.setHours(dateEnd.getHours() - 7);
108
+ break;
109
+ case "day":
110
+ dateStart = new Date(
111
+ date.getFullYear(),
112
+ date.getMonth(),
113
+ date.getDate(),
114
+ 23,
115
+ 59,
116
+ 59
117
+ );
118
+ dateStart.setHours(dateStart.getHours() - 7);
119
+ dateEnd = new Date(
120
+ date.getFullYear(),
121
+ date.getMonth(),
122
+ date.getDate(),
123
+ 0,
124
+ 0,
125
+ 0
126
+ );
127
+ dateEnd.setHours(dateEnd.getHours() - 7);
128
+ break;
129
+ case "week":
130
+ const dayOfWeek = date.getDay();
131
+ const daysToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
132
+ const daysToSunday = dayOfWeek === 0 ? 0 : 7 - dayOfWeek;
133
+ dateStart = new Date(
134
+ date.getFullYear(),
135
+ date.getMonth(),
136
+ date.getDate() + daysToSunday,
137
+ 23,
138
+ 59,
139
+ 59
140
+ );
141
+ dateStart.setHours(dateStart.getHours() - 7);
142
+ dateEnd = new Date(
143
+ date.getFullYear(),
144
+ date.getMonth(),
145
+ date.getDate() + daysToMonday,
146
+ 0,
147
+ 0,
148
+ 0
149
+ );
150
+ dateEnd.setHours(dateEnd.getHours() - 7);
151
+ break;
152
+ case "year":
153
+ dateStart = new Date(date.getFullYear(), 11, 31, 23, 59, 59);
154
+ dateStart.setHours(dateStart.getHours() - 7);
155
+ dateEnd = new Date(date.getFullYear() - 1, 11, 31, 0, 0, 0);
156
+ dateEnd.setHours(dateEnd.getHours() - 7);
157
+ break;
158
+ default:
159
+ throw new Error(
160
+ "\u0110\u01A1n v\u1ECB kh\xF4ng h\u1EE3p l\u1EC7. Ch\u1EC9 ch\u1EA5p nh\u1EADn: week, day, month, year"
161
+ );
162
+ }
163
+ return [
164
+ ["date_start", "<=", formatDate(dateStart)],
165
+ ["date_end", ">=", formatDate(dateEnd)]
166
+ ];
167
+ };
168
+ var convertFieldsToArray = (fields) => {
169
+ const defaultFields = ["display_name", "date_start", "date_end"];
170
+ if (!fields || !Array.isArray(fields)) {
171
+ return defaultFields;
172
+ }
173
+ const inputFields = fields.filter((field) => field && field.type_co === "field").map((field) => field.name);
174
+ return [...defaultFields, ...inputFields];
175
+ };
176
+ function combineContexts(contexts) {
177
+ if (contexts.some((context) => !context)) {
178
+ return void 0;
179
+ } else {
180
+ const res = contexts.reduce((acc, context) => {
181
+ return { ...acc, ...context };
182
+ }, {});
183
+ return res;
184
+ }
185
+ }
186
+ var STORAGES = {
187
+ TOKEN: "accessToken",
188
+ USER_INFO: "USER_INFO"
189
+ };
190
+ function useAsyncState(initialValue = [true, null]) {
191
+ return useReducer(
192
+ (_state, action = null) => [false, action],
193
+ initialValue
194
+ );
195
+ }
196
+ async function setStorageItemAsync(key, value) {
197
+ try {
198
+ if (value === null) {
199
+ localStorage.removeItem(key);
200
+ } else {
201
+ localStorage.setItem(key, value);
202
+ }
203
+ } catch (e) {
204
+ console.error("Local storage is unavailable:", e);
205
+ }
206
+ }
207
+ function useStorageState(key) {
208
+ const [state, setState] = useAsyncState();
209
+ useEffect(() => {
210
+ try {
211
+ const storedValue = localStorage.getItem(key);
212
+ setState(storedValue);
213
+ } catch (e) {
214
+ console.error("Local storage is unavailable:", e);
215
+ }
216
+ }, [key]);
217
+ const setValue = useCallback(
218
+ (value) => {
219
+ setState(value);
220
+ setStorageItemAsync(key, value);
221
+ },
222
+ [key]
223
+ );
224
+ return [state, setValue];
225
+ }
226
+ export {
227
+ API_APP_URL,
228
+ API_PRESCHOOL_URL,
229
+ STORAGES,
230
+ combineContexts,
231
+ convertFieldsToArray,
232
+ countSum,
233
+ getDateRange,
234
+ languages,
235
+ mergeButtons,
236
+ setStorageItemAsync,
237
+ useGetRowIds,
238
+ useStorageState
239
+ };
package/package.json CHANGED
@@ -1,57 +1,66 @@
1
- {
2
- "name": "@fctc/widget-logic",
3
- "version": "1.1.4",
4
- "types": "dist/index.d.ts",
5
- "main": "dist/index.cjs",
6
- "module": "dist/index.mjs",
7
- "exports": {
8
- ".": {
9
- "types": "./dist/index.d.ts",
10
- "import": "./dist/index.mjs",
11
- "require": "./dist/index.cjs"
12
- },
13
- "./hooks": {
14
- "types": "./dist/hooks.d.ts",
15
- "import": "./dist/hooks.mjs",
16
- "require": "./dist/hooks.cjs"
17
- },
18
- "./types": {
19
- "types": "./dist/types.d.ts",
20
- "import": "./dist/types.mjs",
21
- "require": "./dist/types.cjs"
22
- },
23
- "./widget": {
24
- "types": "./dist/widget.d.ts",
25
- "import": "./dist/widget.mjs",
26
- "require": "./dist/widget.cjs"
27
- },
28
- "./icons": {
29
- "types": "./dist/icons.d.ts",
30
- "import": "./dist/icons.mjs",
31
- "require": "./dist/icons.cjs"
32
- }
33
- },
34
- "files": [
35
- "dist"
36
- ],
37
- "scripts": {
38
- "build": "tsup",
39
- "test": "jest"
40
- },
41
- "dependencies": {
42
- "@fctc/interface-logic": "^1.0.4",
43
- "@headlessui/react": "^2.2.6",
44
- "@types/react-dom": "^19.1.7",
45
- "react-datepicker": "^8.4.0",
46
- "react-dom": "^19.1.1",
47
- "react-tooltip": "^5.29.1"
48
- },
49
- "devDependencies": {
50
- "@types/react": "18.0.0",
51
- "jest": "^29.7.0",
52
- "react": "18.0.0",
53
- "tsup": "^8.0.0",
54
- "typescript": "^5.8.2"
55
- },
56
- "packageManager": "yarn@1.22.0"
57
- }
1
+ {
2
+ "name": "@fctc/widget-logic",
3
+ "version": "1.1.5",
4
+ "types": "dist/index.d.ts",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.cjs"
12
+ },
13
+ "./hooks": {
14
+ "types": "./dist/hooks.d.ts",
15
+ "import": "./dist/hooks.mjs",
16
+ "require": "./dist/hooks.cjs"
17
+ },
18
+ "./types": {
19
+ "types": "./dist/types.d.ts",
20
+ "import": "./dist/types.mjs",
21
+ "require": "./dist/types.cjs"
22
+ },
23
+ "./widget": {
24
+ "types": "./dist/widget.d.ts",
25
+ "import": "./dist/widget.mjs",
26
+ "require": "./dist/widget.cjs"
27
+ },
28
+ "./icons": {
29
+ "types": "./dist/icons.d.ts",
30
+ "import": "./dist/icons.mjs",
31
+ "require": "./dist/icons.cjs"
32
+ },
33
+ "./utils": {
34
+ "types": "./dist/utils.d.ts",
35
+ "import": "./dist/utils.mjs",
36
+ "require": "./dist/utils.cjs"
37
+ }
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "test": "jest"
45
+ },
46
+ "dependencies": {
47
+ "@fctc/interface-logic": "^1.0.4",
48
+ "@headlessui/react": "^2.2.6",
49
+ "@tanstack/react-query": "^5.84.0",
50
+ "@types/react-dom": "^19.1.7",
51
+ "i18next": "^25.3.2",
52
+ "i18next-browser-languagedetector": "^8.2.0",
53
+ "react-datepicker": "^8.4.0",
54
+ "react-dom": "^19.1.1",
55
+ "react-i18next": "^15.6.1",
56
+ "react-tooltip": "^5.29.1"
57
+ },
58
+ "devDependencies": {
59
+ "@types/react": "18.0.0",
60
+ "jest": "^29.7.0",
61
+ "react": "18.0.0",
62
+ "tsup": "^8.0.0",
63
+ "typescript": "^5.8.2"
64
+ },
65
+ "packageManager": "yarn@1.22.0"
66
+ }