local-storage.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { isArray, isObject } from "@/utils/type-guards";
  2. /**
  3. * Storage keys
  4. */
  5. export const CURRENCY_CODE = "current_currency";
  6. export const STORE_CODE = "current_store";
  7. export const STORE_CONFIG = "store_config";
  8. export const SELECTED_LOCATION = "elected_location";
  9. export const GUEST_CART = "guest_cart";
  10. export const CART_DATA = "cart_data";
  11. export const COUNTRIES = "countries";
  12. export const LAST_ORDER_ID = "last_order_id";
  13. export const MULTI_CHECKOUT = "ulti_checkout";
  14. export const SHIPPING_ADDRESS = "hipping_address";
  15. export const COMPARE_LIST = "recently_compared_product";
  16. export const COMPARE_ID = "compare_list_uid";
  17. export const IS_GUEST = "is_guest";
  18. export const CUSTOMER = "customer";
  19. export const IS_VIRTUAL_CART = "isVirtualCart";
  20. export const CURRENCY_RATES = "currency_rates";
  21. export const itemsArray = new Array(10).fill(0);
  22. export const SELECTED_STORE = "Store";
  23. export const DEFAULT_CURRENCY_CODE = "DEFAULT_CURRENCY_CODE";
  24. export const IS_AUTO_CURRENCY_SET = "IS_AUTO_CURRENCY_SET";
  25. export const EMAIL = "email";
  26. export const ORDER_DETAILS = "order-details";
  27. export const CACHED_KEYS = {
  28. MEGA_MENU: "MEGA_MENU",
  29. ROOT_CATEGORIES: "ROOT_CATEGORIES",
  30. };
  31. /**
  32. * Set local storage
  33. *
  34. * @param {string} key - Key for the storage
  35. * @param {any} data - Data to be stored
  36. * @returns void
  37. */
  38. export const setLocalStorage = (key: string, data: any) => {
  39. if (typeof window !== "undefined") {
  40. if (isArray(data) || isObject(data)) {
  41. data = JSON.stringify(data);
  42. }
  43. if (typeof data === "string") {
  44. localStorage.setItem(key, data);
  45. }
  46. }
  47. };
  48. /**
  49. * Get data from local storage
  50. *
  51. * @param {string} key - Key for the storage
  52. * @param {boolean} needParsedData - Whether to parse the data or not
  53. * @returns any
  54. */
  55. export const getLocalStorage = (key: string, needParsedData = false) => {
  56. try {
  57. if (typeof window !== "undefined" && window) {
  58. const data = localStorage.getItem(key);
  59. if (!data || typeof data === "undefined") return null;
  60. if (needParsedData) return JSON.parse(data);
  61. return data;
  62. }
  63. } catch (error) {
  64. console.error('Error:' , error)
  65. return null;
  66. }
  67. };
  68. /**
  69. * Get a specific key from local storage
  70. *
  71. * @param {string} storageKey - Key for the storage
  72. * @param {string} requiredKey - Required key
  73. * @returns any
  74. */
  75. export const getKeyFromStorage = (
  76. storageKey: string ,
  77. requiredKey: string
  78. ) => {
  79. const data = getLocalStorage(storageKey, true);
  80. return data?.[requiredKey] || null;
  81. };
  82. /**
  83. * Remove data from local storage
  84. *
  85. * @param {string} storageKey - Key for the storage
  86. * @returns void
  87. */
  88. export const removeFromLocalStorage = (storageKey: string) => {
  89. if (typeof window !== "undefined") {
  90. localStorage.removeItem(storageKey);
  91. }
  92. };
  93. /**
  94. * Reset cart storage
  95. *
  96. * @returns void
  97. */
  98. export const resetCartStorage = () => {
  99. removeFromLocalStorage(CART_DATA);
  100. removeFromLocalStorage(GUEST_CART);
  101. removeFromLocalStorage(SHIPPING_ADDRESS);
  102. };