shelflife-react-hooks 1.0.16 → 1.0.17
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.cjs.js +62 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.esm.js +62 -2
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/context/StorageItemContext.tsx +22 -1
- package/src/context/api/storageItemApi.ts +63 -0
- package/src/context/api/toPurchaseApi.ts +1 -1
|
@@ -14,6 +14,9 @@ import {
|
|
|
14
14
|
deleteItemRequest,
|
|
15
15
|
editItemRequest,
|
|
16
16
|
fetchAboutToExpireRequest,
|
|
17
|
+
fetchAggregatedAboutToExpireRequest,
|
|
18
|
+
fetchAggregatedExpiredRequest,
|
|
19
|
+
fetchAggregatedRunningLowRequest,
|
|
17
20
|
fetchExpiredRequest,
|
|
18
21
|
fetchItemsRequest,
|
|
19
22
|
fetchRunningLowRequest
|
|
@@ -32,8 +35,11 @@ type StorageItemContextValue = {
|
|
|
32
35
|
editItem: (storageId: number, itemId: number, dto: EditItemRequest) => Promise<StorageItem>;
|
|
33
36
|
deleteItem: (storageId: number, itemId: number) => Promise<void>;
|
|
34
37
|
fetchExpired: (storageId: number) => Promise<StorageItem[]>;
|
|
38
|
+
fetchExpiredAggregated: () => Promise<StorageItem[]>;
|
|
35
39
|
fetchAboutToExpire: (storageId: number) => Promise<StorageItem[]>;
|
|
40
|
+
fetchAboutToExpireAggregated: () => Promise<StorageItem[]>;
|
|
36
41
|
fetchRunningLow: (storageId: number) => Promise<RunningLowNotification[]>;
|
|
42
|
+
fetchRunningLowAggregated: () => Promise<RunningLowNotification[]>;
|
|
37
43
|
};
|
|
38
44
|
|
|
39
45
|
type StorageItemProviderProps = {
|
|
@@ -82,14 +88,26 @@ export const StorageItemProvider = ({ baseUrl, children }: StorageItemProviderPr
|
|
|
82
88
|
(storageId: number) => fetchExpiredRequest(apiConfig, storageId),
|
|
83
89
|
[apiConfig]
|
|
84
90
|
);
|
|
91
|
+
const fetchExpiredAggregated = useCallback(
|
|
92
|
+
() => fetchAggregatedExpiredRequest(apiConfig),
|
|
93
|
+
[apiConfig]
|
|
94
|
+
);
|
|
85
95
|
const fetchAboutToExpire = useCallback(
|
|
86
96
|
(storageId: number) => fetchAboutToExpireRequest(apiConfig, storageId),
|
|
87
97
|
[apiConfig]
|
|
88
98
|
);
|
|
99
|
+
const fetchAboutToExpireAggregated = useCallback(
|
|
100
|
+
() => fetchAggregatedAboutToExpireRequest(apiConfig),
|
|
101
|
+
[apiConfig]
|
|
102
|
+
);
|
|
89
103
|
const fetchRunningLow = useCallback(
|
|
90
104
|
(storageId: number) => fetchRunningLowRequest(apiConfig, storageId),
|
|
91
105
|
[apiConfig]
|
|
92
106
|
);
|
|
107
|
+
const fetchRunningLowAggregated = useCallback(
|
|
108
|
+
() => fetchAggregatedRunningLowRequest(apiConfig),
|
|
109
|
+
[apiConfig]
|
|
110
|
+
);
|
|
93
111
|
|
|
94
112
|
const value = useMemo<StorageItemContextValue>(() => ({
|
|
95
113
|
items,
|
|
@@ -104,8 +122,11 @@ export const StorageItemProvider = ({ baseUrl, children }: StorageItemProviderPr
|
|
|
104
122
|
editItem,
|
|
105
123
|
deleteItem,
|
|
106
124
|
fetchExpired,
|
|
125
|
+
fetchExpiredAggregated,
|
|
107
126
|
fetchAboutToExpire,
|
|
108
|
-
|
|
127
|
+
fetchAboutToExpireAggregated,
|
|
128
|
+
fetchRunningLow,
|
|
129
|
+
fetchRunningLowAggregated
|
|
109
130
|
}), [
|
|
110
131
|
aboutToExpireItems,
|
|
111
132
|
addItem,
|
|
@@ -175,6 +175,48 @@ export const fetchAboutToExpireRequest = async (
|
|
|
175
175
|
return [];
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
+
export const fetchAggregatedExpiredRequest = async (
|
|
179
|
+
config: StorageItemApiConfig
|
|
180
|
+
): Promise<StorageItem[]> => runWithRequestState(config, async () => {
|
|
181
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
182
|
+
const response = await fetch(`${normalizedBaseUrl}/api/expired`, {
|
|
183
|
+
headers: buildAuthHeaders(config.token)
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (!response.ok) {
|
|
187
|
+
throw new Error('Failed to fetch expired items');
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const payload = await readJson<StorageItem[]>(response);
|
|
191
|
+
if (payload) {
|
|
192
|
+
config.setExpiredItems(payload);
|
|
193
|
+
return payload;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return [];
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
export const fetchAggregatedAboutToExpireRequest = async (
|
|
200
|
+
config: StorageItemApiConfig
|
|
201
|
+
): Promise<StorageItem[]> => runWithRequestState(config, async () => {
|
|
202
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
203
|
+
const response = await fetch(`${normalizedBaseUrl}/api/abouttoexpire`, {
|
|
204
|
+
headers: buildAuthHeaders(config.token)
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
if (!response.ok) {
|
|
208
|
+
throw new Error('Failed to fetch items about to expire');
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const payload = await readJson<StorageItem[]>(response);
|
|
212
|
+
if (payload) {
|
|
213
|
+
config.setAboutToExpireItems(payload);
|
|
214
|
+
return payload;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return [];
|
|
218
|
+
});
|
|
219
|
+
|
|
178
220
|
export const fetchRunningLowRequest = async (
|
|
179
221
|
config: StorageItemApiConfig,
|
|
180
222
|
storageId: number
|
|
@@ -196,3 +238,24 @@ export const fetchRunningLowRequest = async (
|
|
|
196
238
|
|
|
197
239
|
return [];
|
|
198
240
|
});
|
|
241
|
+
|
|
242
|
+
export const fetchAggregatedRunningLowRequest = async (
|
|
243
|
+
config: StorageItemApiConfig
|
|
244
|
+
): Promise<RunningLowNotification[]> => runWithRequestState(config, async () => {
|
|
245
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
246
|
+
const response = await fetch(`${normalizedBaseUrl}/api/runninglow`, {
|
|
247
|
+
headers: buildAuthHeaders(config.token)
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
if (!response.ok) {
|
|
251
|
+
throw new Error('Failed to fetch running low items');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const payload = await readJson<RunningLowNotification[]>(response);
|
|
255
|
+
if (payload) {
|
|
256
|
+
config.setRunningLow(payload);
|
|
257
|
+
return payload;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return [];
|
|
261
|
+
});
|
|
@@ -12,7 +12,7 @@ export const fetchAggregatedShoppingRequest = async (
|
|
|
12
12
|
config: ToPurchaseApiConfig
|
|
13
13
|
): Promise<ToPurchaseItem[]> => runWithRequestState(config, async () => {
|
|
14
14
|
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
15
|
-
const response = await fetch(`${normalizedBaseUrl}/api/
|
|
15
|
+
const response = await fetch(`${normalizedBaseUrl}/api/shoppinglist`, {
|
|
16
16
|
headers: buildAuthHeaders(config.token)
|
|
17
17
|
});
|
|
18
18
|
|