@eztrak/shared 0.9.0 → 0.11.0

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/index.js CHANGED
@@ -42,6 +42,7 @@ __export(src_exports, {
42
42
  TableLayoutToolbarControls: () => TableLayoutToolbarControls,
43
43
  ToolTip: () => ToolTip,
44
44
  TooltipText: () => TooltipText,
45
+ USER_STATE_STORAGE_KEY: () => USER_STATE_STORAGE_KEY,
45
46
  capitalizeText: () => capitalizeText,
46
47
  checkIfExistsInArray: () => checkIfExistsInArray,
47
48
  cn: () => cn,
@@ -54,16 +55,26 @@ __export(src_exports, {
54
55
  formatDateTime: () => formatDateTime,
55
56
  formatNumber: () => formatNumber,
56
57
  formatValidationErrorsHtml: () => formatValidationErrorsHtml,
58
+ get: () => get,
57
59
  getApiError: () => getApiError,
58
60
  getErrorMessages: () => getErrorMessages,
59
61
  getFieldsByCategory: () => getFieldsByCategory,
60
62
  getFieldsByInputType: () => getFieldsByInputType,
61
63
  getFieldsByName: () => getFieldsByName,
64
+ getItem: () => getItem,
65
+ getItemFromLocalStorage: () => getItemFromLocalStorage,
62
66
  handleApiError: () => handleApiError,
63
67
  isEmpty: () => isEmpty,
68
+ loadUserState: () => loadUserState,
64
69
  normalizeApiListItems: () => normalizeApiListItems,
65
70
  parseCurrencyValueToNumber: () => parseCurrencyValueToNumber,
66
71
  removeByPropertyValues: () => removeByPropertyValues,
72
+ removeItem: () => removeItem,
73
+ saveItemLocalStorage: () => saveItemLocalStorage,
74
+ saveUserState: () => saveUserState,
75
+ set: () => set,
76
+ setItem: () => setItem,
77
+ storeItemInLocalStorage: () => storeItemInLocalStorage,
67
78
  toDateInputValue: () => toDateInputValue,
68
79
  toLowerCaseText: () => toLowerCaseText,
69
80
  truncateFileName: () => truncateFileName,
@@ -278,6 +289,48 @@ var getFieldsByInputType = (fields = [], key) => {
278
289
  return fields.filter(({ inputType = "" }) => inputType === key);
279
290
  };
280
291
 
292
+ // src/utils/localStorage.ts
293
+ var USER_STATE_STORAGE_KEY = "authState";
294
+ function getStorage() {
295
+ if (typeof window === "undefined") return null;
296
+ return window.localStorage;
297
+ }
298
+ function getItem(key, fallback = null) {
299
+ try {
300
+ const raw = getStorage()?.getItem(key);
301
+ if (raw === null || raw === void 0) {
302
+ return fallback;
303
+ }
304
+ return JSON.parse(raw);
305
+ } catch {
306
+ return fallback;
307
+ }
308
+ }
309
+ function setItem(key, value) {
310
+ try {
311
+ getStorage()?.setItem(key, JSON.stringify(value));
312
+ } catch {
313
+ }
314
+ }
315
+ function removeItem(key) {
316
+ try {
317
+ getStorage()?.removeItem(key);
318
+ } catch {
319
+ }
320
+ }
321
+ function loadUserState(key = USER_STATE_STORAGE_KEY) {
322
+ const value = getItem(key, void 0);
323
+ return value === null ? void 0 : value;
324
+ }
325
+ function saveUserState(state, key = USER_STATE_STORAGE_KEY) {
326
+ setItem(key, state);
327
+ }
328
+ var get = getItem;
329
+ var set = setItem;
330
+ var getItemFromLocalStorage = getItem;
331
+ var storeItemInLocalStorage = setItem;
332
+ var saveItemLocalStorage = setItem;
333
+
281
334
  // src/utils/handleApiError.ts
282
335
  var import_react_hot_toast = __toESM(require("react-hot-toast"));
283
336
  var import_sweetalert2 = __toESM(require("sweetalert2"));
@@ -330,18 +383,92 @@ function handleApiError(error, { showAlert = false, fallbackMessage } = {}) {
330
383
  const message = fallbackMessage || data?.message || data?.title || error.error || "An error occurred. Please try again.";
331
384
  import_react_hot_toast.default.error(message);
332
385
  }
386
+ var BRAND_ORANGE = "#FF7335";
387
+ var CANCEL_RED = "#ff0000";
388
+ function getConfirmErrorMessage(error) {
389
+ if (error && typeof error === "object") {
390
+ const err = error;
391
+ return err.data?.message || err.data?.title || err.message || "Something went wrong. Please try again.";
392
+ }
393
+ return "Something went wrong. Please try again.";
394
+ }
333
395
  function confirmationAlert(onConfirm, options = {}) {
334
- void import_sweetalert2.default.fire({
335
- title: options.title ?? "Are you sure?",
336
- text: options.text,
337
- icon: options.icon ?? "warning",
396
+ const {
397
+ title = options.inputExpectedValue ? "Confirmation Required" : "Are you sure?",
398
+ text,
399
+ html,
400
+ icon = "warning",
401
+ confirmButtonText = "Yes",
402
+ cancelButtonText = "Cancel",
403
+ confirmButtonColor = BRAND_ORANGE,
404
+ cancelButtonColor = CANCEL_RED,
405
+ showLoaderOnConfirm = false,
406
+ inputExpectedValue,
407
+ inputPlaceholder = "Type confirmation text to proceed",
408
+ reverseButtons,
409
+ focusCancel,
410
+ zIndex
411
+ } = options;
412
+ const hasInput = Boolean(inputExpectedValue);
413
+ const useLoader = showLoaderOnConfirm || hasInput;
414
+ const inputHtml = html ?? (hasInput ? `
415
+ ${text ? `<p style="margin-bottom: 15px;">${text}</p>` : ""}
416
+ <p style="margin-bottom: 10px; font-weight: bold; color: #666;">
417
+ Please type "<span style="color: ${BRAND_ORANGE}; font-weight: bold;">${inputExpectedValue}</span>" to confirm:
418
+ </p>
419
+ ` : void 0);
420
+ const swalOptions = {
421
+ title,
422
+ icon,
338
423
  showCancelButton: true,
339
- confirmButtonText: options.confirmButtonText ?? "Yes",
340
- cancelButtonText: options.cancelButtonText ?? "Cancel",
341
- confirmButtonColor: "#FF7335"
342
- }).then((result) => {
343
- if (result.isConfirmed) {
344
- onConfirm();
424
+ confirmButtonText,
425
+ cancelButtonText,
426
+ confirmButtonColor,
427
+ cancelButtonColor,
428
+ reverseButtons,
429
+ focusCancel,
430
+ allowOutsideClick: () => !import_sweetalert2.default.isLoading(),
431
+ allowEscapeKey: () => !import_sweetalert2.default.isLoading(),
432
+ didOpen: () => {
433
+ if (zIndex == null) return;
434
+ const container = import_sweetalert2.default.getContainer();
435
+ if (container) container.style.zIndex = String(zIndex);
436
+ }
437
+ };
438
+ if (hasInput) {
439
+ swalOptions.html = inputHtml;
440
+ swalOptions.input = "text";
441
+ swalOptions.inputPlaceholder = inputPlaceholder;
442
+ swalOptions.inputValidator = (value) => {
443
+ if (!value?.trim()) return "You need to enter the confirmation text!";
444
+ if (value.trim() !== inputExpectedValue.trim()) {
445
+ return `Please type "${inputExpectedValue}" exactly as shown!`;
446
+ }
447
+ return null;
448
+ };
449
+ } else if (html) {
450
+ swalOptions.html = html;
451
+ } else {
452
+ swalOptions.text = text;
453
+ }
454
+ if (useLoader) {
455
+ swalOptions.showLoaderOnConfirm = true;
456
+ swalOptions.preConfirm = (inputValue) => {
457
+ if (hasInput && inputValue?.trim() !== inputExpectedValue.trim()) {
458
+ import_sweetalert2.default.showValidationMessage(
459
+ `Please type "${inputExpectedValue}" exactly as shown!`
460
+ );
461
+ return false;
462
+ }
463
+ return Promise.resolve().then(() => onConfirm()).catch((error) => {
464
+ import_sweetalert2.default.showValidationMessage(getConfirmErrorMessage(error));
465
+ throw error;
466
+ });
467
+ };
468
+ }
469
+ void import_sweetalert2.default.fire(swalOptions).then((result) => {
470
+ if (result.isConfirmed && !useLoader) {
471
+ void Promise.resolve(onConfirm());
345
472
  }
346
473
  });
347
474
  }
@@ -1753,6 +1880,7 @@ CustomCellEditor.displayName = "CustomCellEditor";
1753
1880
  TableLayoutToolbarControls,
1754
1881
  ToolTip,
1755
1882
  TooltipText,
1883
+ USER_STATE_STORAGE_KEY,
1756
1884
  capitalizeText,
1757
1885
  checkIfExistsInArray,
1758
1886
  cn,
@@ -1765,16 +1893,26 @@ CustomCellEditor.displayName = "CustomCellEditor";
1765
1893
  formatDateTime,
1766
1894
  formatNumber,
1767
1895
  formatValidationErrorsHtml,
1896
+ get,
1768
1897
  getApiError,
1769
1898
  getErrorMessages,
1770
1899
  getFieldsByCategory,
1771
1900
  getFieldsByInputType,
1772
1901
  getFieldsByName,
1902
+ getItem,
1903
+ getItemFromLocalStorage,
1773
1904
  handleApiError,
1774
1905
  isEmpty,
1906
+ loadUserState,
1775
1907
  normalizeApiListItems,
1776
1908
  parseCurrencyValueToNumber,
1777
1909
  removeByPropertyValues,
1910
+ removeItem,
1911
+ saveItemLocalStorage,
1912
+ saveUserState,
1913
+ set,
1914
+ setItem,
1915
+ storeItemInLocalStorage,
1778
1916
  toDateInputValue,
1779
1917
  toLowerCaseText,
1780
1918
  truncateFileName,