@fctc/widget-logic 5.3.0 → 5.3.1

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 CHANGED
@@ -13,6 +13,9 @@ var countSum = (data, field) => {
13
13
  0
14
14
  );
15
15
  };
16
+ var isObjectEmpty = (obj) => {
17
+ return Object.keys(obj).length === 0;
18
+ };
16
19
  function mergeButtons(fields) {
17
20
  const buttons = fields?.filter((f) => f.type_co === "button");
18
21
  const others = fields?.filter((f) => f.type_co !== "button");
@@ -24,103 +27,6 @@ function mergeButtons(fields) {
24
27
  }
25
28
  return others;
26
29
  }
27
- var getDateRange = (currentDate, unit) => {
28
- const date = new Date(currentDate);
29
- let dateStart, dateEnd;
30
- function formatDate(d) {
31
- 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");
32
- }
33
- switch (unit) {
34
- case "month":
35
- dateStart = new Date(
36
- date.getFullYear(),
37
- date.getMonth() + 1,
38
- date.getDate(),
39
- 23,
40
- 59,
41
- 59
42
- );
43
- dateStart.setHours(dateStart.getHours() - 7);
44
- dateEnd = new Date(date.getFullYear(), date.getMonth(), 0, 0, 0, 0);
45
- dateEnd.setHours(dateEnd.getHours() - 7);
46
- break;
47
- case "day":
48
- dateStart = new Date(
49
- date.getFullYear(),
50
- date.getMonth(),
51
- date.getDate(),
52
- 23,
53
- 59,
54
- 59
55
- );
56
- dateStart.setHours(dateStart.getHours() - 7);
57
- dateEnd = new Date(
58
- date.getFullYear(),
59
- date.getMonth(),
60
- date.getDate(),
61
- 0,
62
- 0,
63
- 0
64
- );
65
- dateEnd.setHours(dateEnd.getHours() - 7);
66
- break;
67
- case "week":
68
- const dayOfWeek = date.getDay();
69
- const daysToMonday = dayOfWeek === 0 ? -6 : 1 - dayOfWeek;
70
- const daysToSunday = dayOfWeek === 0 ? 0 : 7 - dayOfWeek;
71
- dateStart = new Date(
72
- date.getFullYear(),
73
- date.getMonth(),
74
- date.getDate() + daysToSunday,
75
- 23,
76
- 59,
77
- 59
78
- );
79
- dateStart.setHours(dateStart.getHours() - 7);
80
- dateEnd = new Date(
81
- date.getFullYear(),
82
- date.getMonth(),
83
- date.getDate() + daysToMonday,
84
- 0,
85
- 0,
86
- 0
87
- );
88
- dateEnd.setHours(dateEnd.getHours() - 7);
89
- break;
90
- case "year":
91
- dateStart = new Date(date.getFullYear(), 11, 31, 23, 59, 59);
92
- dateStart.setHours(dateStart.getHours() - 7);
93
- dateEnd = new Date(date.getFullYear() - 1, 11, 31, 0, 0, 0);
94
- dateEnd.setHours(dateEnd.getHours() - 7);
95
- break;
96
- default:
97
- throw new Error(
98
- "\u0110\u01A1n v\u1ECB kh\xF4ng h\u1EE3p l\u1EC7. Ch\u1EC9 ch\u1EA5p nh\u1EADn: week, day, month, year"
99
- );
100
- }
101
- return [
102
- ["date_start", "<=", formatDate(dateStart)],
103
- ["date_end", ">=", formatDate(dateEnd)]
104
- ];
105
- };
106
- var convertFieldsToArray = (fields) => {
107
- const defaultFields = ["display_name", "date_start", "date_end"];
108
- if (!fields || !Array.isArray(fields)) {
109
- return defaultFields;
110
- }
111
- const inputFields = fields.filter((field) => field && field.type_co === "field").map((field) => field.name);
112
- return [...defaultFields, ...inputFields];
113
- };
114
- function combineContexts(contexts) {
115
- if (contexts.some((context) => !context)) {
116
- return void 0;
117
- } else {
118
- const res = contexts.reduce((acc, context) => {
119
- return { ...acc, ...context };
120
- }, {});
121
- return res;
122
- }
123
- }
124
30
  var STORAGES = {
125
31
  TOKEN: "accessToken",
126
32
  USER_INFO: "USER_INFO"
@@ -161,17 +67,323 @@ function useStorageState(key) {
161
67
  );
162
68
  return [state, setValue];
163
69
  }
70
+ var guessTypeFromUrl = (url) => {
71
+ const ext = url.split(".").pop()?.toLowerCase();
72
+ if (!ext) return null;
73
+ const map = {
74
+ jpg: "image/jpeg",
75
+ jpeg: "image/jpeg",
76
+ png: "image/png",
77
+ webp: "image/webp",
78
+ gif: "image/gif",
79
+ svg: "image/svg+xml",
80
+ bmp: "image/bmp",
81
+ tiff: "image/tiff",
82
+ pdf: "application/pdf",
83
+ zip: "application/zip",
84
+ rar: "application/x-rar-compressed",
85
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
86
+ xls: "application/vnd.ms-excel",
87
+ mp4: "video/mp4",
88
+ mov: "video/quicktime"
89
+ };
90
+ return map[ext] || null;
91
+ };
92
+
93
+ // src/utils/format-date.ts
94
+ import moment from "moment";
95
+ var validateAndParseDate = (input, isDateTime = false) => {
96
+ if (!input || typeof input !== "string") return null;
97
+ const cleanInput = input.replace(/[^0-9-\/:\s]/g, "");
98
+ const dateFormat = "YYYY-MM-DD";
99
+ const dateTimeFormat = "YYYY-MM-DD HH:mm:ss";
100
+ const currentDay = moment().format("DD");
101
+ const currentMonth = moment().format("MM");
102
+ const currentYear = moment().format("YYYY");
103
+ const defaultTime = "00:00:00";
104
+ const maxYear = parseInt(currentYear) + 10;
105
+ const isValidDate = (day, month, year) => {
106
+ const date = moment(`${day}-${month}-${year}`, "DD-MM-YYYY", true);
107
+ return date.isValid();
108
+ };
109
+ const isValidTime = (hour, minute = "00", second = "00") => {
110
+ const h = parseInt(hour, 10);
111
+ const m = parseInt(minute, 10);
112
+ const s = parseInt(second, 10);
113
+ return h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59;
114
+ };
115
+ const formatOutput = (day, month, year, time = defaultTime) => {
116
+ let result = moment(
117
+ `${day}-${month}-${year} ${time}`,
118
+ "DD-MM-YYYY HH:mm:ss"
119
+ );
120
+ if (!result.isValid()) return null;
121
+ if (isDateTime) {
122
+ result = result.subtract(7, "hours");
123
+ return result.format(dateTimeFormat);
124
+ }
125
+ return result.format(dateFormat);
126
+ };
127
+ if (isDateTime && input.match(
128
+ /^\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}\s+\d{1,2}(:\d{1,2}(:\d{1,2})?)?$/
129
+ )) {
130
+ const [datePart, timePart] = input.split(/\s+/);
131
+ const dateParts = datePart.split(/[\/-]/);
132
+ const timeParts = timePart.split(":");
133
+ const day = dateParts[0].padStart(2, "0");
134
+ const month = dateParts[1].padStart(2, "0");
135
+ const year = dateParts[2].length <= 2 ? `20${dateParts[2].padStart(2, "0")}` : dateParts[2].padStart(4, "0");
136
+ const hour = timeParts[0].padStart(2, "0");
137
+ const minute = timeParts[1] ? timeParts[1].padStart(2, "0") : "00";
138
+ const second = timeParts[2] ? timeParts[2].padStart(2, "0") : "00";
139
+ if (isValidDate(day, month, year) && isValidTime(hour, minute, second)) {
140
+ let result = moment(
141
+ `${day}-${month}-${year} ${hour}:${minute}:${second}`,
142
+ "DD-MM-YYYY HH:mm:ss"
143
+ );
144
+ if (!result.isValid()) return null;
145
+ result = result.subtract(7, "hours");
146
+ return result.format(dateTimeFormat);
147
+ }
148
+ return null;
149
+ }
150
+ if (cleanInput.match(/^\d{4}-\d{2}-\d{2}$/)) {
151
+ const [year, month, day] = cleanInput.split("-");
152
+ if (isValidDate(day, month, year)) {
153
+ return formatOutput(day, month, year);
154
+ }
155
+ return null;
156
+ }
157
+ if (cleanInput.match(/^\d{1,2}\/\d{1,2}\/\d{2,4}$/)) {
158
+ const [day, month, year] = cleanInput.split("/");
159
+ const paddedDay = day.padStart(2, "0");
160
+ const paddedMonth = month.padStart(2, "0");
161
+ const fullYear = year.length <= 2 ? `20${year.padStart(2, "0")}` : year.padStart(4, "0");
162
+ if (isValidDate(paddedDay, paddedMonth, fullYear)) {
163
+ return formatOutput(paddedDay, paddedMonth, fullYear);
164
+ }
165
+ return null;
166
+ }
167
+ if (cleanInput.match(/^\d{1,2}-\d{1,2}-\d{2,4}$/)) {
168
+ const [day, month, year] = cleanInput.split("-");
169
+ const paddedDay = day.padStart(2, "0");
170
+ const paddedMonth = month.padStart(2, "0");
171
+ const fullYear = year.length <= 2 ? `20${year.padStart(2, "0")}` : year.padStart(4, "0");
172
+ if (isValidDate(paddedDay, paddedMonth, fullYear)) {
173
+ return formatOutput(paddedDay, paddedMonth, fullYear);
174
+ }
175
+ return null;
176
+ }
177
+ if (cleanInput.match(/^\d{1,2}[\/-]\d{1,2}$/)) {
178
+ const [day, month] = cleanInput.split(/[\/-]/);
179
+ const paddedDay = day.padStart(2, "0");
180
+ const paddedMonth = month.padStart(2, "0");
181
+ if (isValidDate(paddedDay, paddedMonth, currentYear)) {
182
+ return formatOutput(paddedDay, paddedMonth, currentYear);
183
+ }
184
+ return null;
185
+ }
186
+ if (cleanInput.match(/^\d{4}$/)) {
187
+ const num = parseInt(cleanInput, 10);
188
+ if (num >= 2e3 && num <= maxYear) {
189
+ if (isValidDate(currentDay, currentMonth, num.toString())) {
190
+ return formatOutput(currentDay, currentMonth, num.toString());
191
+ }
192
+ return null;
193
+ }
194
+ const day = cleanInput.slice(0, 2);
195
+ const month = cleanInput.slice(2, 4);
196
+ if (isValidDate(day, month, currentYear)) {
197
+ return formatOutput(day, month, currentYear);
198
+ }
199
+ return null;
200
+ }
201
+ if (cleanInput.startsWith("-") && /^\-\d+$/.test(cleanInput)) {
202
+ const daysToSubtract = Math.abs(parseInt(cleanInput, 10));
203
+ let result = moment().subtract(daysToSubtract, "days");
204
+ if (isDateTime) {
205
+ result = result.subtract(7, "hours");
206
+ }
207
+ if (result.isValid()) {
208
+ return isDateTime ? result.format(dateTimeFormat) : result.format(dateFormat);
209
+ }
210
+ return null;
211
+ }
212
+ if (input.match(/^\d{1,2}[^0-9-\/]+\d{1,2}[^0-9-\/]+\d{2,4}.*$/)) {
213
+ const parts = input.split(/[^0-9-\/]+/).filter(Boolean);
214
+ const day = parts[0].padStart(2, "0");
215
+ const month = parts[1].padStart(2, "0");
216
+ let year = parts[2];
217
+ year = year.length === 2 ? `20${year}` : year.padStart(4, "0");
218
+ if (isValidDate(day, month, year)) {
219
+ return formatOutput(day, month, year);
220
+ }
221
+ return null;
222
+ }
223
+ if (isDateTime) {
224
+ if (cleanInput.length === 9) {
225
+ const day = cleanInput.slice(0, 2);
226
+ const month = cleanInput.slice(2, 4);
227
+ const year = cleanInput.slice(4, 8);
228
+ const hour = cleanInput.slice(8, 9).padStart(2, "0");
229
+ if (isValidDate(day, month, year) && isValidTime(hour)) {
230
+ let result = moment(
231
+ `${day}-${month}-${year} ${hour}:00:00`,
232
+ "DD-MM-YYYY HH:mm:ss"
233
+ );
234
+ if (!result.isValid()) return null;
235
+ result = result.subtract(7, "hours");
236
+ return result.format(dateTimeFormat);
237
+ }
238
+ return null;
239
+ }
240
+ if (cleanInput.length === 10) {
241
+ const day = cleanInput.slice(0, 2);
242
+ const month = cleanInput.slice(2, 4);
243
+ const year = cleanInput.slice(4, 8);
244
+ const hour = cleanInput.slice(8, 10);
245
+ if (isValidDate(day, month, year) && isValidTime(hour)) {
246
+ let result = moment(
247
+ `${day}-${month}-${year} ${hour}:00:00`,
248
+ "DD-MM-YYYY HH:mm:ss"
249
+ );
250
+ if (!result.isValid()) return null;
251
+ result = result.subtract(7, "hours");
252
+ return result.format(dateTimeFormat);
253
+ }
254
+ return null;
255
+ }
256
+ if (cleanInput.length === 11) {
257
+ const day = cleanInput.slice(0, 2);
258
+ const month = cleanInput.slice(2, 4);
259
+ const year = cleanInput.slice(4, 8);
260
+ const hour = cleanInput.slice(8, 10);
261
+ const minute = cleanInput.slice(10, 11).padStart(2, "0");
262
+ if (isValidDate(day, month, year) && isValidTime(hour, minute)) {
263
+ let result = moment(
264
+ `${day}-${month}-${year} ${hour}:${minute}:00`,
265
+ "DD-MM-YYYY HH:mm:ss"
266
+ );
267
+ if (!result.isValid()) return null;
268
+ result = result.subtract(7, "hours");
269
+ return result.format(dateTimeFormat);
270
+ }
271
+ return null;
272
+ }
273
+ if (cleanInput.length === 12) {
274
+ const day = cleanInput.slice(0, 2);
275
+ const month = cleanInput.slice(2, 4);
276
+ const year = cleanInput.slice(4, 8);
277
+ const hour = cleanInput.slice(8, 10);
278
+ const minute = cleanInput.slice(10, 12);
279
+ if (isValidDate(day, month, year) && isValidTime(hour, minute)) {
280
+ let result = moment(
281
+ `${day}-${month}-${year} ${hour}:${minute}:00`,
282
+ "DD-MM-YYYY HH:mm:ss"
283
+ );
284
+ if (!result.isValid()) return null;
285
+ result = result.subtract(7, "hours");
286
+ return result.format(dateTimeFormat);
287
+ }
288
+ return null;
289
+ }
290
+ if (cleanInput.length === 13) {
291
+ const day = cleanInput.slice(0, 2);
292
+ const month = cleanInput.slice(2, 4);
293
+ const year = cleanInput.slice(4, 8);
294
+ const hour = cleanInput.slice(8, 10);
295
+ const minute = cleanInput.slice(10, 12);
296
+ const second = cleanInput.slice(12, 13).padStart(2, "0");
297
+ if (isValidDate(day, month, year) && isValidTime(hour, minute, second)) {
298
+ let result = moment(
299
+ `${day}-${month}-${year} ${hour}:${minute}:${second}`,
300
+ "DD-MM-YYYY HH:mm:ss"
301
+ );
302
+ if (!result.isValid()) return null;
303
+ result = result.subtract(7, "hours");
304
+ return result.format(dateTimeFormat);
305
+ }
306
+ return null;
307
+ }
308
+ if (cleanInput.length === 14) {
309
+ const day = cleanInput.slice(0, 2);
310
+ const month = cleanInput.slice(2, 4);
311
+ const year = cleanInput.slice(4, 8);
312
+ const hour = cleanInput.slice(8, 10);
313
+ const minute = cleanInput.slice(10, 12);
314
+ const second = cleanInput.slice(12, 14);
315
+ if (isValidDate(day, month, year) && isValidTime(hour, minute, second)) {
316
+ let result = moment(
317
+ `${day}-${month}-${year} ${hour}:${minute}:${second}`,
318
+ "DD-MM-YYYY HH:mm:ss"
319
+ );
320
+ if (!result.isValid()) return null;
321
+ result = result.subtract(7, "hours");
322
+ return result.format(dateTimeFormat);
323
+ }
324
+ return null;
325
+ }
326
+ }
327
+ const len = cleanInput.length;
328
+ if (len === 1 || len === 2) {
329
+ const paddedDay = cleanInput.padStart(2, "0");
330
+ if (isValidDate(paddedDay, currentMonth, currentYear)) {
331
+ return formatOutput(paddedDay, currentMonth, currentYear);
332
+ }
333
+ return null;
334
+ }
335
+ if (len === 3) {
336
+ const day = cleanInput.slice(0, 2);
337
+ const month = cleanInput.slice(2, 3).padStart(2, "0");
338
+ if (isValidDate(day, month, currentYear)) {
339
+ return formatOutput(day, month, currentYear);
340
+ }
341
+ return null;
342
+ }
343
+ if (len === 6) {
344
+ const day = cleanInput.slice(0, 2);
345
+ const month = cleanInput.slice(2, 4);
346
+ let year = cleanInput.slice(4, 6);
347
+ year = `20${year}`;
348
+ if (parseInt(month) > 12) {
349
+ if (isValidDate(day, currentMonth, currentYear)) {
350
+ return formatOutput(day, currentMonth, currentYear);
351
+ }
352
+ return null;
353
+ }
354
+ if (isValidDate(day, month, year)) {
355
+ return formatOutput(day, month, year);
356
+ }
357
+ return null;
358
+ }
359
+ if (len === 7) {
360
+ return null;
361
+ }
362
+ if (len === 8) {
363
+ const day = cleanInput.slice(0, 2);
364
+ const month = cleanInput.slice(2, 4);
365
+ const year = cleanInput.slice(4, 8);
366
+ if (isValidDate(day, month, year)) {
367
+ return formatOutput(day, month, year);
368
+ }
369
+ return null;
370
+ }
371
+ if (len > 8 && !isDateTime) {
372
+ return null;
373
+ }
374
+ return null;
375
+ };
164
376
 
165
377
  // src/utils.ts
166
378
  export * from "@fctc/interface-logic/utils";
167
379
  export {
168
380
  STORAGES,
169
- combineContexts,
170
- convertFieldsToArray,
171
381
  countSum,
172
- getDateRange,
382
+ guessTypeFromUrl,
383
+ isObjectEmpty,
173
384
  languages,
174
385
  mergeButtons,
175
386
  setStorageItemAsync,
176
- useStorageState
387
+ useStorageState,
388
+ validateAndParseDate
177
389
  };
package/dist/widget.d.mts CHANGED
@@ -131,7 +131,6 @@ declare const many2manyTagsController: (props: IMany2ManyTagFieldProps) => {
131
131
  isUser: boolean;
132
132
  isFetching: boolean;
133
133
  fetchMoreOptions: () => void;
134
- setInputValue: react.Dispatch<react.SetStateAction<string>>;
135
134
  domainObject: any;
136
135
  setDomainObject: react.Dispatch<any>;
137
136
  handleChooseRecord: (idRecord: number) => void;
@@ -188,13 +187,11 @@ interface IDateFieldProps extends IInputFieldProps {
188
187
  }
189
188
 
190
189
  declare const dateFieldController: (props: IDateFieldProps) => {
191
- formatDate: string;
192
190
  formatDateParse: string;
193
191
  range: (start: number, end: number, step?: number) => number[];
194
192
  years: number[];
195
193
  months_vi: string[];
196
194
  months_en: string[];
197
- customValidateMinMax: (date: any) => string | false;
198
195
  minNowValue: boolean | moment.Moment | null;
199
196
  maxNowValue: boolean | moment.Moment | null;
200
197
  };
@@ -295,7 +292,6 @@ declare const tableController: ({ data }: ITableProps) => {
295
292
  };
296
293
 
297
294
  declare const tableGroupController: (props: any) => {
298
- onExpandChildGroup: () => void;
299
295
  colEmptyGroup: {
300
296
  fromStart: number;
301
297
  fromEnd: number;
@@ -303,7 +299,6 @@ declare const tableGroupController: (props: any) => {
303
299
  isShowGroup: boolean;
304
300
  isDataGroupFetched: boolean;
305
301
  isDataPlaceHolder: boolean;
306
- nameGroupWithCount: string;
307
302
  columnsGroup: any;
308
303
  rowsGroup: any[];
309
304
  dataGroup: any;
package/dist/widget.d.ts CHANGED
@@ -131,7 +131,6 @@ declare const many2manyTagsController: (props: IMany2ManyTagFieldProps) => {
131
131
  isUser: boolean;
132
132
  isFetching: boolean;
133
133
  fetchMoreOptions: () => void;
134
- setInputValue: react.Dispatch<react.SetStateAction<string>>;
135
134
  domainObject: any;
136
135
  setDomainObject: react.Dispatch<any>;
137
136
  handleChooseRecord: (idRecord: number) => void;
@@ -188,13 +187,11 @@ interface IDateFieldProps extends IInputFieldProps {
188
187
  }
189
188
 
190
189
  declare const dateFieldController: (props: IDateFieldProps) => {
191
- formatDate: string;
192
190
  formatDateParse: string;
193
191
  range: (start: number, end: number, step?: number) => number[];
194
192
  years: number[];
195
193
  months_vi: string[];
196
194
  months_en: string[];
197
- customValidateMinMax: (date: any) => string | false;
198
195
  minNowValue: boolean | moment.Moment | null;
199
196
  maxNowValue: boolean | moment.Moment | null;
200
197
  };
@@ -295,7 +292,6 @@ declare const tableController: ({ data }: ITableProps) => {
295
292
  };
296
293
 
297
294
  declare const tableGroupController: (props: any) => {
298
- onExpandChildGroup: () => void;
299
295
  colEmptyGroup: {
300
296
  fromStart: number;
301
297
  fromEnd: number;
@@ -303,7 +299,6 @@ declare const tableGroupController: (props: any) => {
303
299
  isShowGroup: boolean;
304
300
  isDataGroupFetched: boolean;
305
301
  isDataPlaceHolder: boolean;
306
- nameGroupWithCount: string;
307
302
  columnsGroup: any;
308
303
  rowsGroup: any[];
309
304
  dataGroup: any;