@react-pakistan/util-functions 1.25.48 → 1.25.50
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/api/stellar-solutions/app-user/index.d.ts +1 -1
- package/api/stellar-solutions/bank/index.d.ts +1 -1
- package/api/stellar-solutions/branch/index.d.ts +1 -1
- package/api/stellar-solutions/company/index.d.ts +1 -1
- package/api/stellar-solutions/company-report/index.d.ts +1 -1
- package/api/stellar-solutions/contact/index.d.ts +1 -1
- package/api/stellar-solutions/currency/index.d.ts +1 -1
- package/api/stellar-solutions/customer/index.d.ts +1 -1
- package/api/stellar-solutions/expense/index.d.ts +1 -1
- package/api/stellar-solutions/expense-category/index.d.ts +1 -1
- package/api/stellar-solutions/lead/index.d.ts +1 -1
- package/api/stellar-solutions/menu-order/index.d.ts +1 -1
- package/api/stellar-solutions/payment/index.d.ts +1 -1
- package/api/stellar-solutions/payment/index.js +2 -2
- package/api/stellar-solutions/payment-mode/index.d.ts +1 -1
- package/api/stellar-solutions/preference/index.d.ts +1 -1
- package/api/stellar-solutions/product/index.d.ts +1 -1
- package/api/stellar-solutions/product-category/index.d.ts +1 -1
- package/api/stellar-solutions/profile/index.d.ts +1 -1
- package/api/stellar-solutions/quote-invoice/index.d.ts +1 -1
- package/api/stellar-solutions/quote-invoice-report/index.d.ts +1 -1
- package/api/stellar-solutions/tax/index.d.ts +1 -1
- package/api/stellar-solutions/types/common.d.ts +24 -0
- package/api/stellar-solutions/types/common.js +22 -0
- package/api/stellar-solutions/{type.d.ts → types/erp.d.ts} +1 -199
- package/api/stellar-solutions/types/erp.js +68 -0
- package/api/stellar-solutions/types/index.d.ts +3 -0
- package/api/stellar-solutions/types/index.js +19 -0
- package/api/stellar-solutions/types/pos.d.ts +176 -0
- package/api/stellar-solutions/types/pos.js +61 -0
- package/general/index.d.ts +0 -1
- package/general/index.js +0 -1
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/api/stellar-solutions/bank/cache.d.ts +0 -0
- package/api/stellar-solutions/bank/cache.js +0 -237
- package/api/stellar-solutions/branch/cache.d.ts +0 -0
- package/api/stellar-solutions/branch/cache.js +0 -241
- package/api/stellar-solutions/company/cache.d.ts +0 -0
- package/api/stellar-solutions/company/cache.js +0 -247
- package/api/stellar-solutions/contact/cache.d.ts +0 -0
- package/api/stellar-solutions/contact/cache.js +0 -247
- package/api/stellar-solutions/currency/cache.d.ts +0 -0
- package/api/stellar-solutions/currency/cache.js +0 -243
- package/api/stellar-solutions/customer/cache.d.ts +0 -0
- package/api/stellar-solutions/customer/cache.js +0 -249
- package/api/stellar-solutions/expense/cache.d.ts +0 -0
- package/api/stellar-solutions/expense/cache.js +0 -247
- package/api/stellar-solutions/expense-category/cache.d.ts +0 -0
- package/api/stellar-solutions/expense-category/cache.js +0 -224
- package/api/stellar-solutions/payment-mode/cache.d.ts +0 -0
- package/api/stellar-solutions/payment-mode/cache.js +0 -245
- package/api/stellar-solutions/preference/cache.d.ts +0 -0
- package/api/stellar-solutions/preference/cache.js +0 -218
- package/api/stellar-solutions/product/cache.d.ts +0 -0
- package/api/stellar-solutions/product/cache.js +0 -222
- package/api/stellar-solutions/product-category/cache.d.ts +0 -0
- package/api/stellar-solutions/product-category/cache.js +0 -224
- package/api/stellar-solutions/tax/cache.d.ts +0 -0
- package/api/stellar-solutions/tax/cache.js +0 -235
- package/api/stellar-solutions/type.js +0 -142
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { PreferenceBE, LS_KEYS } from '../type';
|
|
3
|
-
// import { API_METHODS } from '../../../constants/api-methods';
|
|
4
|
-
// import { fetchData } from '../../../general/fetch-data';
|
|
5
|
-
// import { getStorageValue } from '../../../local-storage/get-storage-value';
|
|
6
|
-
// import { setStorageValue } from '../../../local-storage/set-storage-value';
|
|
7
|
-
// import { ONE_WEEK_IN_MS } from '../../../constants';
|
|
8
|
-
// interface CachedPreferences {
|
|
9
|
-
// preferences: PreferenceBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get preferences from cache only
|
|
14
|
-
// * Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
15
|
-
// * Does not trigger any API calls
|
|
16
|
-
// *
|
|
17
|
-
// * @returns PreferenceBE[] - Array of cached preferences or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const preferences = getCachedPreferencesSync();
|
|
21
|
-
// * if (preferences.items.length > 0) {
|
|
22
|
-
// * console.log(preferences.items[0].onboarding);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedPreferencesSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: PreferenceBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.PREFERENCES
|
|
32
|
-
// ) as CachedPreferences | null;
|
|
33
|
-
// if (!cachedData) {
|
|
34
|
-
// return { count: 0, items: [] };
|
|
35
|
-
// }
|
|
36
|
-
// const currentTime = new Date().getTime();
|
|
37
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
38
|
-
// const ageInMs = currentTime - cachedTime;
|
|
39
|
-
// // If cached data is less than 1 day old, return it
|
|
40
|
-
// if (ageInMs < ONE_WEEK_IN_MS) {
|
|
41
|
-
// return {
|
|
42
|
-
// count: cachedData.preferences.length,
|
|
43
|
-
// items: cachedData.preferences,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached preferences:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get preferences from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of preferences with the following logic:
|
|
56
|
-
// * - If preferences exist in cache and are less than 1 week old, return cached version
|
|
57
|
-
// * - If preferences exist but are older than 1 week, fetch fresh data and update cache
|
|
58
|
-
// * - If preferences don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter preferences
|
|
61
|
-
// * @param pageLimit - Number of preferences to fetch (default: 100)
|
|
62
|
-
// * @returns Promise<{count:number, items: PreferenceBE[]}> - Paged preferences
|
|
63
|
-
// *
|
|
64
|
-
// * @example
|
|
65
|
-
// * const preferences = await getCachedPreferences();
|
|
66
|
-
// * console.log(preferences.items[0].onboarding);
|
|
67
|
-
// *
|
|
68
|
-
// * // With search
|
|
69
|
-
// * const filtered = await getCachedPreferences('theme');
|
|
70
|
-
// */
|
|
71
|
-
// export const getCachedPreferences = async (
|
|
72
|
-
// searchQuery?: string,
|
|
73
|
-
// pageLimit: number = 100
|
|
74
|
-
// ): Promise<{
|
|
75
|
-
// count: number;
|
|
76
|
-
// items: PreferenceBE[];
|
|
77
|
-
// }> => {
|
|
78
|
-
// try {
|
|
79
|
-
// // If there's a search query, always fetch fresh data (don't use cache)
|
|
80
|
-
// if (searchQuery && searchQuery.trim()) {
|
|
81
|
-
// const response = await fetchData({
|
|
82
|
-
// url: API_ROUTES.PREFERENCES,
|
|
83
|
-
// body: JSON.stringify({
|
|
84
|
-
// searchQuery,
|
|
85
|
-
// pageLimit,
|
|
86
|
-
// currentPage: 1,
|
|
87
|
-
// }),
|
|
88
|
-
// method: API_METHODS.POST,
|
|
89
|
-
// });
|
|
90
|
-
// return response?.data || { count: 0, items: [] };
|
|
91
|
-
// }
|
|
92
|
-
// // Get the cached data from localStorage
|
|
93
|
-
// const cachedData = getStorageValue(
|
|
94
|
-
// LS_KEYS.PREFERENCES
|
|
95
|
-
// ) as CachedPreferences | null;
|
|
96
|
-
// const currentTime = new Date().getTime();
|
|
97
|
-
// // Check if cached data exists and is still fresh
|
|
98
|
-
// if (cachedData) {
|
|
99
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
100
|
-
// const ageInMs = currentTime - cachedTime;
|
|
101
|
-
// // If cached data is less than 1 week old, return it
|
|
102
|
-
// if (ageInMs < ONE_WEEK_IN_MS) {
|
|
103
|
-
// return {
|
|
104
|
-
// count: cachedData.preferences.length,
|
|
105
|
-
// items: cachedData.preferences,
|
|
106
|
-
// };
|
|
107
|
-
// }
|
|
108
|
-
// }
|
|
109
|
-
// // If no cached data or data is older than 1 week, fetch fresh data
|
|
110
|
-
// const response = await fetchData({
|
|
111
|
-
// url: API_ROUTES.PREFERENCES,
|
|
112
|
-
// body: JSON.stringify({
|
|
113
|
-
// pageLimit,
|
|
114
|
-
// currentPage: 1,
|
|
115
|
-
// }),
|
|
116
|
-
// method: API_METHODS.POST,
|
|
117
|
-
// });
|
|
118
|
-
// if (response?.data?.items) {
|
|
119
|
-
// // Update the cache with fresh data
|
|
120
|
-
// const updatedCache: CachedPreferences = {
|
|
121
|
-
// preferences: response.data.items,
|
|
122
|
-
// cachedAt: new Date().toISOString(),
|
|
123
|
-
// };
|
|
124
|
-
// setStorageValue(LS_KEYS.PREFERENCES, updatedCache);
|
|
125
|
-
// return response.data;
|
|
126
|
-
// }
|
|
127
|
-
// return { count: 0, items: [] };
|
|
128
|
-
// } catch (error) {
|
|
129
|
-
// console.error('Error fetching preferences:', error);
|
|
130
|
-
// return { count: 0, items: [] };
|
|
131
|
-
// }
|
|
132
|
-
// };
|
|
133
|
-
// /**
|
|
134
|
-
// * Utility function to get a specific preference by ID from cache
|
|
135
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
136
|
-
// *
|
|
137
|
-
// * @param preferenceId - The ID of the preference to retrieve
|
|
138
|
-
// * @returns PreferenceBE | null - The preference or null if not found
|
|
139
|
-
// *
|
|
140
|
-
// * @example
|
|
141
|
-
// * const preference = getCachedPreferenceById('pref-123');
|
|
142
|
-
// * if (preference) {
|
|
143
|
-
// * console.log(preference.onboarding);
|
|
144
|
-
// * }
|
|
145
|
-
// */
|
|
146
|
-
// export const getCachedPreferenceById = (
|
|
147
|
-
// preferenceId: string
|
|
148
|
-
// ): PreferenceBE | null => {
|
|
149
|
-
// if (!preferenceId) {
|
|
150
|
-
// return null;
|
|
151
|
-
// }
|
|
152
|
-
// try {
|
|
153
|
-
// const preferences = getCachedPreferencesSync().items;
|
|
154
|
-
// return preferences.find((pref) => pref.id === preferenceId) || null;
|
|
155
|
-
// } catch (error) {
|
|
156
|
-
// console.error('Error getting cached preference by ID:', error);
|
|
157
|
-
// return null;
|
|
158
|
-
// }
|
|
159
|
-
// };
|
|
160
|
-
// /**
|
|
161
|
-
// * Utility function to invalidate (remove) preferences from cache
|
|
162
|
-
// * Useful when preferences have been updated and you want to force a refresh
|
|
163
|
-
// *
|
|
164
|
-
// * @example
|
|
165
|
-
// * invalidatePreferencesCache();
|
|
166
|
-
// * const freshPreferences = await getCachedPreferences();
|
|
167
|
-
// */
|
|
168
|
-
// export const invalidatePreferencesCache = (): void => {
|
|
169
|
-
// try {
|
|
170
|
-
// localStorage.removeItem(LS_KEYS.PREFERENCES);
|
|
171
|
-
// } catch (error) {
|
|
172
|
-
// console.error('Error invalidating preferences cache:', error);
|
|
173
|
-
// }
|
|
174
|
-
// };
|
|
175
|
-
// /**
|
|
176
|
-
// * Utility function to preload preferences into cache
|
|
177
|
-
// * Useful to call on app initialization or login
|
|
178
|
-
// *
|
|
179
|
-
// * @returns Promise<{count:number, items: PreferenceBE[]}> - Array of preloaded preferences
|
|
180
|
-
// *
|
|
181
|
-
// * @example
|
|
182
|
-
// * // On app initialization
|
|
183
|
-
// * await preloadPreferences();
|
|
184
|
-
// */
|
|
185
|
-
// export const preloadPreferences = async (): Promise<{
|
|
186
|
-
// count: number;
|
|
187
|
-
// items: PreferenceBE[];
|
|
188
|
-
// }> => {
|
|
189
|
-
// return getCachedPreferences();
|
|
190
|
-
// };
|
|
191
|
-
// /**
|
|
192
|
-
// * Utility function to check if preferences cache is stale
|
|
193
|
-
// * Returns true if cache is older than 1 day or doesn't exist
|
|
194
|
-
// *
|
|
195
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
196
|
-
// *
|
|
197
|
-
// * @example
|
|
198
|
-
// * if (isPreferencesCacheStale()) {
|
|
199
|
-
// * await getCachedPreferences(); // This will fetch fresh data
|
|
200
|
-
// * }
|
|
201
|
-
// */
|
|
202
|
-
// export const isPreferencesCacheStale = (): boolean => {
|
|
203
|
-
// try {
|
|
204
|
-
// const cachedData = getStorageValue(
|
|
205
|
-
// LS_KEYS.PREFERENCES
|
|
206
|
-
// ) as CachedPreferences | null;
|
|
207
|
-
// if (!cachedData) {
|
|
208
|
-
// return true;
|
|
209
|
-
// }
|
|
210
|
-
// const currentTime = new Date().getTime();
|
|
211
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
212
|
-
// const ageInMs = currentTime - cachedTime;
|
|
213
|
-
// return ageInMs >= ONE_WEEK_IN_MS;
|
|
214
|
-
// } catch (error) {
|
|
215
|
-
// console.error('Error checking preferences cache staleness:', error);
|
|
216
|
-
// return true;
|
|
217
|
-
// }
|
|
218
|
-
// };
|
|
File without changes
|
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { ProductBE, LS_KEYS } from '../type';
|
|
3
|
-
// import { API_METHODS } from '../../../constants/api-methods';
|
|
4
|
-
// import { fetchData } from '../../../general/fetch-data';
|
|
5
|
-
// import { getStorageValue } from '../../../local-storage/get-storage-value';
|
|
6
|
-
// import { setStorageValue } from '../../../local-storage/set-storage-value';
|
|
7
|
-
// import { ONE_DAY_IN_MS } from '../../../constants';
|
|
8
|
-
// interface CachedProducts {
|
|
9
|
-
// products: ProductBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get products from cache only
|
|
14
|
-
// * Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
15
|
-
// * Does not trigger any API calls
|
|
16
|
-
// *
|
|
17
|
-
// * @returns ProductBE[] - Array of cached products or empty array wrapper ({count, items})
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const products = getCachedProductsSync();
|
|
21
|
-
// * if (products.items.length > 0) {
|
|
22
|
-
// * console.log(products.items[0].name);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedProductsSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: ProductBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.PRODUCTS
|
|
32
|
-
// ) as CachedProducts | null;
|
|
33
|
-
// if (!cachedData) {
|
|
34
|
-
// return { count: 0, items: [] };
|
|
35
|
-
// }
|
|
36
|
-
// const currentTime = new Date().getTime();
|
|
37
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
38
|
-
// const ageInMs = currentTime - cachedTime;
|
|
39
|
-
// // If cached data is less than 1 day old, return it
|
|
40
|
-
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
41
|
-
// return {
|
|
42
|
-
// count: cachedData.products.length,
|
|
43
|
-
// items: cachedData.products,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached products:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get products from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of products with the following logic:
|
|
56
|
-
// * - If products exist in cache and are less than 1 day old, return cached version
|
|
57
|
-
// * - If products exist but are older than 1 day, fetch fresh data and update cache
|
|
58
|
-
// * - If products don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter products
|
|
61
|
-
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
-
// * @param pageLimit - Number of products to fetch (default: 100)
|
|
63
|
-
// * @returns Promise<{count:number, items: ProductBE[]}> - Paged products
|
|
64
|
-
// *
|
|
65
|
-
// * @example
|
|
66
|
-
// * const products = await getCachedProducts();
|
|
67
|
-
// * console.log(products.items[0].name);
|
|
68
|
-
// *
|
|
69
|
-
// * // With search
|
|
70
|
-
// * const filtered = await getCachedProducts('Samsung');
|
|
71
|
-
// *
|
|
72
|
-
// * // With filters
|
|
73
|
-
// * const filteredByCategory = await getCachedProducts(undefined, { productCategoryId: 'cat-123' });
|
|
74
|
-
// */
|
|
75
|
-
// export const getCachedProducts = async (
|
|
76
|
-
// searchQuery?: string,
|
|
77
|
-
// filters?: Record<string, unknown>,
|
|
78
|
-
// pageLimit: number = 100
|
|
79
|
-
// ): Promise<{ count: number; items: ProductBE[] }> => {
|
|
80
|
-
// try {
|
|
81
|
-
// // If there's a search query or filters, always fetch fresh data (don't use cache)
|
|
82
|
-
// if (
|
|
83
|
-
// (searchQuery && searchQuery.trim()) ||
|
|
84
|
-
// (filters && Object.keys(filters).length > 0)
|
|
85
|
-
// ) {
|
|
86
|
-
// const response = await fetchData({
|
|
87
|
-
// url: API_ROUTES.PRODUCTS,
|
|
88
|
-
// body: JSON.stringify({
|
|
89
|
-
// searchQuery,
|
|
90
|
-
// ...filters,
|
|
91
|
-
// pageLimit,
|
|
92
|
-
// currentPage: 1,
|
|
93
|
-
// }),
|
|
94
|
-
// method: API_METHODS.POST,
|
|
95
|
-
// });
|
|
96
|
-
// return response?.data || { count: 0, items: [] };
|
|
97
|
-
// }
|
|
98
|
-
// // Get the cached data from localStorage
|
|
99
|
-
// const cachedData = getStorageValue(
|
|
100
|
-
// LS_KEYS.PRODUCTS
|
|
101
|
-
// ) as CachedProducts | null;
|
|
102
|
-
// const currentTime = new Date().getTime();
|
|
103
|
-
// // Check if cached data exists and is still fresh
|
|
104
|
-
// if (cachedData) {
|
|
105
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
106
|
-
// const ageInMs = currentTime - cachedTime;
|
|
107
|
-
// // If cached data is less than 1 day old, return it
|
|
108
|
-
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
109
|
-
// return {
|
|
110
|
-
// count: cachedData.products.length,
|
|
111
|
-
// items: cachedData.products,
|
|
112
|
-
// };
|
|
113
|
-
// }
|
|
114
|
-
// }
|
|
115
|
-
// // If no cached data or data is older than 1 day, fetch fresh data
|
|
116
|
-
// const response = await fetchData({
|
|
117
|
-
// url: API_ROUTES.PRODUCTS,
|
|
118
|
-
// body: JSON.stringify({
|
|
119
|
-
// pageLimit,
|
|
120
|
-
// currentPage: 1,
|
|
121
|
-
// }),
|
|
122
|
-
// method: API_METHODS.POST,
|
|
123
|
-
// });
|
|
124
|
-
// if (response?.data?.items) {
|
|
125
|
-
// // Update the cache with fresh data
|
|
126
|
-
// const updatedCache: CachedProducts = {
|
|
127
|
-
// products: response.data.items,
|
|
128
|
-
// cachedAt: new Date().toISOString(),
|
|
129
|
-
// };
|
|
130
|
-
// setStorageValue(LS_KEYS.PRODUCTS, updatedCache);
|
|
131
|
-
// return response.data;
|
|
132
|
-
// }
|
|
133
|
-
// return { count: 0, items: [] };
|
|
134
|
-
// } catch (error) {
|
|
135
|
-
// console.error('Error fetching products:', error);
|
|
136
|
-
// return { count: 0, items: [] };
|
|
137
|
-
// }
|
|
138
|
-
// };
|
|
139
|
-
// /**
|
|
140
|
-
// * Utility function to get a specific product by ID from cache
|
|
141
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
-
// *
|
|
143
|
-
// * @param productId - The ID of the product to retrieve
|
|
144
|
-
// * @returns ProductBE | null - The product or null if not found
|
|
145
|
-
// *
|
|
146
|
-
// * @example
|
|
147
|
-
// * const product = getCachedProductById('prod-123');
|
|
148
|
-
// * if (product) {
|
|
149
|
-
// * console.log(product.name);
|
|
150
|
-
// * }
|
|
151
|
-
// */
|
|
152
|
-
// export const getCachedProductById = (categoryId: string): ProductBE | null => {
|
|
153
|
-
// if (!categoryId) {
|
|
154
|
-
// return null;
|
|
155
|
-
// }
|
|
156
|
-
// try {
|
|
157
|
-
// const products = getCachedProductsSync().items;
|
|
158
|
-
// return products.find((cat) => cat.id === categoryId) || null;
|
|
159
|
-
// } catch (error) {
|
|
160
|
-
// console.error('Error getting cached product by ID:', error);
|
|
161
|
-
// return null;
|
|
162
|
-
// }
|
|
163
|
-
// };
|
|
164
|
-
// /**
|
|
165
|
-
// * Utility function to invalidate (remove) products from cache
|
|
166
|
-
// * Useful when products have been updated and you want to force a refresh
|
|
167
|
-
// *
|
|
168
|
-
// * @example
|
|
169
|
-
// * invalidateProductsCache();
|
|
170
|
-
// * const freshProducts = await getCachedProducts();
|
|
171
|
-
// */
|
|
172
|
-
// export const invalidateProductsCache = (): void => {
|
|
173
|
-
// try {
|
|
174
|
-
// localStorage.removeItem(LS_KEYS.PRODUCTS);
|
|
175
|
-
// } catch (error) {
|
|
176
|
-
// console.error('Error invalidating products cache:', error);
|
|
177
|
-
// }
|
|
178
|
-
// };
|
|
179
|
-
// /**
|
|
180
|
-
// * Utility function to preload products into cache
|
|
181
|
-
// * Useful to call on app initialization or login
|
|
182
|
-
// *
|
|
183
|
-
// * @returns Promise<{count:number, items: ProductBE[]}> - Array of preloaded products
|
|
184
|
-
// *
|
|
185
|
-
// * @example
|
|
186
|
-
// * // On app initialization
|
|
187
|
-
// * await preloadProducts();
|
|
188
|
-
// */
|
|
189
|
-
// export const preloadProducts = async (): Promise<{
|
|
190
|
-
// count: number;
|
|
191
|
-
// items: ProductBE[];
|
|
192
|
-
// }> => {
|
|
193
|
-
// return getCachedProducts();
|
|
194
|
-
// };
|
|
195
|
-
// /**
|
|
196
|
-
// * Utility function to check if products cache is stale
|
|
197
|
-
// * Returns true if cache is older than 1 day or doesn't exist
|
|
198
|
-
// *
|
|
199
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
200
|
-
// *
|
|
201
|
-
// * @example
|
|
202
|
-
// * if (isProductsCacheStale()) {
|
|
203
|
-
// * await getCachedProducts(); // This will fetch fresh data
|
|
204
|
-
// * }
|
|
205
|
-
// */
|
|
206
|
-
// export const isProductsCacheStale = (): boolean => {
|
|
207
|
-
// try {
|
|
208
|
-
// const cachedData = getStorageValue(
|
|
209
|
-
// LS_KEYS.PRODUCTS
|
|
210
|
-
// ) as CachedProducts | null;
|
|
211
|
-
// if (!cachedData) {
|
|
212
|
-
// return true;
|
|
213
|
-
// }
|
|
214
|
-
// const currentTime = new Date().getTime();
|
|
215
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
216
|
-
// const ageInMs = currentTime - cachedTime;
|
|
217
|
-
// return ageInMs >= ONE_DAY_IN_MS;
|
|
218
|
-
// } catch (error) {
|
|
219
|
-
// console.error('Error checking products cache staleness:', error);
|
|
220
|
-
// return true;
|
|
221
|
-
// }
|
|
222
|
-
// };
|
|
File without changes
|
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
// import { API_ROUTES } from '../constants';
|
|
2
|
-
// import { ProductCategoryBE, LS_KEYS } from '../type';
|
|
3
|
-
// import { API_METHODS } from '../../../constants/api-methods';
|
|
4
|
-
// import { fetchData } from '../../../general/fetch-data';
|
|
5
|
-
// import { getStorageValue } from '../../../local-storage/get-storage-value';
|
|
6
|
-
// import { setStorageValue } from '../../../local-storage/set-storage-value';
|
|
7
|
-
// import { ONE_DAY_IN_MS } from '../../../constants';
|
|
8
|
-
// interface CachedProductCategories {
|
|
9
|
-
// productCategories: ProductCategoryBE[];
|
|
10
|
-
// cachedAt: string;
|
|
11
|
-
// }
|
|
12
|
-
// /**
|
|
13
|
-
// * Synchronous utility function to get product categories from cache only
|
|
14
|
-
// * Returns cached data immediately if available and fresh, otherwise returns empty array
|
|
15
|
-
// * Does not trigger any API calls
|
|
16
|
-
// *
|
|
17
|
-
// * @returns ProductCategoryBE[] - Array of cached product categories or empty array
|
|
18
|
-
// *
|
|
19
|
-
// * @example
|
|
20
|
-
// * const categories = getCachedProductCategoriesSync();
|
|
21
|
-
// * if (categories.length > 0) {
|
|
22
|
-
// * console.log(categories[0].name);
|
|
23
|
-
// * }
|
|
24
|
-
// */
|
|
25
|
-
// export const getCachedProductCategoriesSync = (): {
|
|
26
|
-
// count: number;
|
|
27
|
-
// items: ProductCategoryBE[];
|
|
28
|
-
// } => {
|
|
29
|
-
// try {
|
|
30
|
-
// const cachedData = getStorageValue(
|
|
31
|
-
// LS_KEYS.PRODUCT_CATEGORIES
|
|
32
|
-
// ) as CachedProductCategories | null;
|
|
33
|
-
// if (!cachedData) {
|
|
34
|
-
// return { count: 0, items: [] };
|
|
35
|
-
// }
|
|
36
|
-
// const currentTime = new Date().getTime();
|
|
37
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
38
|
-
// const ageInMs = currentTime - cachedTime;
|
|
39
|
-
// // If cached data is less than 1 week old, return it
|
|
40
|
-
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
41
|
-
// return {
|
|
42
|
-
// count: cachedData.productCategories.length,
|
|
43
|
-
// items: cachedData.productCategories,
|
|
44
|
-
// };
|
|
45
|
-
// }
|
|
46
|
-
// return { count: 0, items: [] };
|
|
47
|
-
// } catch (error) {
|
|
48
|
-
// console.error('Error getting cached product categories:', error);
|
|
49
|
-
// return { count: 0, items: [] };
|
|
50
|
-
// }
|
|
51
|
-
// };
|
|
52
|
-
// /**
|
|
53
|
-
// * Utility function to get product categories from cache or fetch from API
|
|
54
|
-
// *
|
|
55
|
-
// * This function manages a localStorage cache of product categories with the following logic:
|
|
56
|
-
// * - If categories exist in cache and are less than 1 week old, return cached version
|
|
57
|
-
// * - If categories exist but are older than 1 week, fetch fresh data and update cache
|
|
58
|
-
// * - If categories don't exist in cache, fetch from API and cache them
|
|
59
|
-
// *
|
|
60
|
-
// * @param searchQuery - Optional search query to filter categories
|
|
61
|
-
// * @param filters - Optional filters object to apply additional filtering (bypasses cache)
|
|
62
|
-
// * @param pageLimit - Number of categories to fetch (default: 100)
|
|
63
|
-
// * @returns Promise<ProductCategoryBE[]> - Array of product categories
|
|
64
|
-
// *
|
|
65
|
-
// * @example
|
|
66
|
-
// * const categories = await getCachedProductCategories();
|
|
67
|
-
// * console.log(categories[0].name);
|
|
68
|
-
// *
|
|
69
|
-
// * // With search
|
|
70
|
-
// * const filtered = await getCachedProductCategories('Electronics');
|
|
71
|
-
// *
|
|
72
|
-
// * // With filters
|
|
73
|
-
// * const filteredCategories = await getCachedProductCategories(undefined, { enabled: true });
|
|
74
|
-
// */
|
|
75
|
-
// export const getCachedProductCategories = async (
|
|
76
|
-
// searchQuery?: string,
|
|
77
|
-
// filters?: Record<string, unknown>,
|
|
78
|
-
// pageLimit: number = 100
|
|
79
|
-
// ): Promise<{ count: number; items: ProductCategoryBE[] }> => {
|
|
80
|
-
// try {
|
|
81
|
-
// // If there's a search query or filters, always fetch fresh data (don't use cache)
|
|
82
|
-
// if (
|
|
83
|
-
// (searchQuery && searchQuery.trim()) ||
|
|
84
|
-
// (filters && Object.keys(filters).length > 0)
|
|
85
|
-
// ) {
|
|
86
|
-
// const response = await fetchData({
|
|
87
|
-
// url: API_ROUTES.PRODUCT_CATEGORIES,
|
|
88
|
-
// body: JSON.stringify({
|
|
89
|
-
// searchQuery,
|
|
90
|
-
// ...filters,
|
|
91
|
-
// pageLimit,
|
|
92
|
-
// currentPage: 1,
|
|
93
|
-
// }),
|
|
94
|
-
// method: API_METHODS.POST,
|
|
95
|
-
// });
|
|
96
|
-
// return response?.data || { count: 0, items: [] };
|
|
97
|
-
// }
|
|
98
|
-
// // Get the cached data from localStorage
|
|
99
|
-
// const cachedData = getStorageValue(
|
|
100
|
-
// LS_KEYS.PRODUCT_CATEGORIES
|
|
101
|
-
// ) as CachedProductCategories | null;
|
|
102
|
-
// const currentTime = new Date().getTime();
|
|
103
|
-
// // Check if cached data exists and is still fresh
|
|
104
|
-
// if (cachedData) {
|
|
105
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
106
|
-
// const ageInMs = currentTime - cachedTime;
|
|
107
|
-
// // If cached data is less than 1 week old, return it
|
|
108
|
-
// if (ageInMs < ONE_DAY_IN_MS) {
|
|
109
|
-
// return {
|
|
110
|
-
// count: cachedData.productCategories.length,
|
|
111
|
-
// items: cachedData.productCategories,
|
|
112
|
-
// };
|
|
113
|
-
// }
|
|
114
|
-
// }
|
|
115
|
-
// // If no cached data or data is older than 1 week, fetch fresh data
|
|
116
|
-
// const response = await fetchData({
|
|
117
|
-
// url: API_ROUTES.PRODUCT_CATEGORIES,
|
|
118
|
-
// body: JSON.stringify({
|
|
119
|
-
// pageLimit,
|
|
120
|
-
// currentPage: 1,
|
|
121
|
-
// }),
|
|
122
|
-
// method: API_METHODS.POST,
|
|
123
|
-
// });
|
|
124
|
-
// if (response?.data?.items) {
|
|
125
|
-
// // Update the cache with fresh data
|
|
126
|
-
// const updatedCache: CachedProductCategories = {
|
|
127
|
-
// productCategories: response.data.items,
|
|
128
|
-
// cachedAt: new Date().toISOString(),
|
|
129
|
-
// };
|
|
130
|
-
// setStorageValue(LS_KEYS.PRODUCT_CATEGORIES, updatedCache);
|
|
131
|
-
// return response.data;
|
|
132
|
-
// }
|
|
133
|
-
// return { count: 0, items: [] };
|
|
134
|
-
// } catch (error) {
|
|
135
|
-
// console.error('Error fetching product categories:', error);
|
|
136
|
-
// return { count: 0, items: [] };
|
|
137
|
-
// }
|
|
138
|
-
// };
|
|
139
|
-
// /**
|
|
140
|
-
// * Utility function to get a specific product category by ID from cache
|
|
141
|
-
// * If not found in cache, returns null (does not trigger API call)
|
|
142
|
-
// *
|
|
143
|
-
// * @param categoryId - The ID of the category to retrieve
|
|
144
|
-
// * @returns ProductCategoryBE | null - The category or null if not found
|
|
145
|
-
// *
|
|
146
|
-
// * @example
|
|
147
|
-
// * const category = getCachedProductCategoryById('cat-123');
|
|
148
|
-
// * if (category) {
|
|
149
|
-
// * console.log(category.name);
|
|
150
|
-
// * }
|
|
151
|
-
// */
|
|
152
|
-
// export const getCachedProductCategoryById = (
|
|
153
|
-
// categoryId: string
|
|
154
|
-
// ): ProductCategoryBE | null => {
|
|
155
|
-
// if (!categoryId) {
|
|
156
|
-
// return null;
|
|
157
|
-
// }
|
|
158
|
-
// try {
|
|
159
|
-
// const productCategories = getCachedProductCategoriesSync().items;
|
|
160
|
-
// return productCategories.find((cat) => cat.id === categoryId) || null;
|
|
161
|
-
// } catch (error) {
|
|
162
|
-
// console.error('Error getting cached product category by ID:', error);
|
|
163
|
-
// return null;
|
|
164
|
-
// }
|
|
165
|
-
// };
|
|
166
|
-
// /**
|
|
167
|
-
// * Utility function to invalidate (remove) product categories from cache
|
|
168
|
-
// * Useful when categories have been updated and you want to force a refresh
|
|
169
|
-
// *
|
|
170
|
-
// * @example
|
|
171
|
-
// * invalidateProductCategoriesCache();
|
|
172
|
-
// * const freshCategories = await getCachedProductCategories();
|
|
173
|
-
// */
|
|
174
|
-
// export const invalidateProductCategoriesCache = (): void => {
|
|
175
|
-
// try {
|
|
176
|
-
// localStorage.removeItem(LS_KEYS.PRODUCT_CATEGORIES);
|
|
177
|
-
// } catch (error) {
|
|
178
|
-
// console.error('Error invalidating product categories cache:', error);
|
|
179
|
-
// }
|
|
180
|
-
// };
|
|
181
|
-
// /**
|
|
182
|
-
// * Utility function to preload product categories into cache
|
|
183
|
-
// * Useful to call on app initialization or login
|
|
184
|
-
// *
|
|
185
|
-
// * @returns Promise<ProductCategoryBE[]> - Array of preloaded categories
|
|
186
|
-
// *
|
|
187
|
-
// * @example
|
|
188
|
-
// * // On app initialization
|
|
189
|
-
// * await preloadProductCategories();
|
|
190
|
-
// */
|
|
191
|
-
// export const preloadProductCategories = async (): Promise<{
|
|
192
|
-
// count: number;
|
|
193
|
-
// items: ProductCategoryBE[];
|
|
194
|
-
// }> => {
|
|
195
|
-
// return getCachedProductCategories();
|
|
196
|
-
// };
|
|
197
|
-
// /**
|
|
198
|
-
// * Utility function to check if product categories cache is stale
|
|
199
|
-
// * Returns true if cache is older than 1 week or doesn't exist
|
|
200
|
-
// *
|
|
201
|
-
// * @returns boolean - True if cache is stale or doesn't exist
|
|
202
|
-
// *
|
|
203
|
-
// * @example
|
|
204
|
-
// * if (isProductCategoriesCacheStale()) {
|
|
205
|
-
// * await getCachedProductCategories(); // This will fetch fresh data
|
|
206
|
-
// * }
|
|
207
|
-
// */
|
|
208
|
-
// export const isProductCategoriesCacheStale = (): boolean => {
|
|
209
|
-
// try {
|
|
210
|
-
// const cachedData = getStorageValue(
|
|
211
|
-
// LS_KEYS.PRODUCT_CATEGORIES
|
|
212
|
-
// ) as CachedProductCategories | null;
|
|
213
|
-
// if (!cachedData) {
|
|
214
|
-
// return true;
|
|
215
|
-
// }
|
|
216
|
-
// const currentTime = new Date().getTime();
|
|
217
|
-
// const cachedTime = new Date(cachedData.cachedAt).getTime();
|
|
218
|
-
// const ageInMs = currentTime - cachedTime;
|
|
219
|
-
// return ageInMs >= ONE_DAY_IN_MS;
|
|
220
|
-
// } catch (error) {
|
|
221
|
-
// console.error('Error checking product categories cache staleness:', error);
|
|
222
|
-
// return true;
|
|
223
|
-
// }
|
|
224
|
-
// };
|
|
File without changes
|