shelflife-react-hooks 1.0.16 → 1.0.18

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.
@@ -6,7 +6,9 @@ import {
6
6
  fetchShoppingListRequest,
7
7
  createShoppingItemRequest,
8
8
  editShoppingItemRequest,
9
- deleteShoppingItemRequest
9
+ deleteShoppingItemRequest,
10
+ createStorageItemsWithShoppingListItemRequest,
11
+ fetchAggregatedShoppingListRequest
10
12
  } from './api/shoppingListApi.js';
11
13
 
12
14
  type ShoppingListContextValue = {
@@ -14,6 +16,8 @@ type ShoppingListContextValue = {
14
16
  isLoading: boolean;
15
17
  isError: boolean;
16
18
  error: Error | null;
19
+ createStorageItemsWithShoppingListItem: (storageId: number, itemId: number) => Promise<void>;
20
+ fetchAggregated: () => Promise<ShoppingListItem[]>;
17
21
  fetchItems: (storageId: number) => Promise<ShoppingListItem[]>;
18
22
  createItem: (storageId: number, dto: CreateShoppingItemRequest) => Promise<ShoppingListItem>;
19
23
  editItem: (storageId: number, itemId: number, dto: EditShoppingItemRequest) => Promise<ShoppingListItem>;
@@ -40,6 +44,10 @@ export const ShoppingListProvider = ({ baseUrl, children }: ShoppingListProvider
40
44
  return fetchShoppingListRequest(apiConfig, storageId);
41
45
  }, [apiConfig]);
42
46
 
47
+ const createStorageItemsWithShoppingListItem = useCallback(async (storageId: number, itemId: number) => {
48
+ return createStorageItemsWithShoppingListItemRequest(apiConfig, storageId, itemId);
49
+ }, [apiConfig]);
50
+
43
51
  const createItem = useCallback(async (storageId: number, dto: CreateShoppingItemRequest) => {
44
52
  return createShoppingItemRequest(apiConfig, storageId, dto);
45
53
  }, [apiConfig]);
@@ -52,7 +60,11 @@ export const ShoppingListProvider = ({ baseUrl, children }: ShoppingListProvider
52
60
  return deleteShoppingItemRequest(apiConfig, storageId, itemId);
53
61
  }, [apiConfig]);
54
62
 
55
- const value = useMemo(() => ({ items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem }), [items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem]);
63
+ const fetchAggregated = useCallback(async () => {
64
+ return fetchAggregatedShoppingListRequest(apiConfig);
65
+ }, [apiConfig]);
66
+
67
+ const value = useMemo(() => ({ items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem, createStorageItemsWithShoppingListItem, fetchAggregated }), [items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem, createStorageItemsWithShoppingListItem, fetchAggregated]);
56
68
 
57
69
  return <ShoppingListContext.Provider value={value}>{children}</ShoppingListContext.Provider>;
58
70
  };
@@ -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
- fetchRunningLow
127
+ fetchAboutToExpireAggregated,
128
+ fetchRunningLow,
129
+ fetchRunningLowAggregated
109
130
  }), [
110
131
  aboutToExpireItems,
111
132
  addItem,
@@ -40,6 +40,43 @@ export const fetchShoppingListRequest = async (
40
40
  return [];
41
41
  });
42
42
 
43
+ export const fetchAggregatedShoppingListRequest = async (
44
+ config: ShoppingListApiConfig
45
+ ): Promise<ShoppingListItem[]> => runWithRequestState(config, async () => {
46
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
47
+ const response = await fetch(`${normalizedBaseUrl}/api/shoppinglist`, {
48
+ headers: buildAuthHeaders(config.token)
49
+ });
50
+
51
+ if (!response.ok) {
52
+ throw new Error('Failed to fetch aggregated shopping list');
53
+ }
54
+
55
+ const payload = await readJson<ShoppingListItem[]>(response);
56
+ if (payload) {
57
+ config.setItems(payload);
58
+ return payload;
59
+ }
60
+
61
+ return [];
62
+ });
63
+
64
+ export const createStorageItemsWithShoppingListItemRequest = async (
65
+ config: ShoppingListApiConfig,
66
+ storageId: number,
67
+ itemId: number
68
+ ) => runWithRequestState(config, async () => {
69
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
70
+ const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist/${itemId}`, {
71
+ method: 'POST',
72
+ headers: buildAuthHeaders(config.token)
73
+ });
74
+
75
+ if (!response.ok) {
76
+ throw new Error('Failed to add items');
77
+ }
78
+ });
79
+
43
80
  export const createShoppingItemRequest = async (
44
81
  config: ShoppingListApiConfig,
45
82
  storageId: number,
@@ -92,7 +129,7 @@ export const editShoppingItemRequest = async (
92
129
 
93
130
  if (err?.productId || err?.amountToBuy)
94
131
  throw err;
95
-
132
+
96
133
  throw new Error('Failed to edit shopping list item');
97
134
  }
98
135
 
@@ -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
+ });
package/src/index.ts CHANGED
@@ -19,5 +19,4 @@ export * from './context/StorageContext.js';
19
19
  export * from './context/StorageItemContext.js';
20
20
  export * from './context/StorageMemberContext.js';
21
21
  export * from './context/UserContext.js';
22
- export * from './context/ShoppingListContext.js';
23
- export * from './context/ToPurchaseContext.js';
22
+ export * from './context/ShoppingListContext.js';
@@ -22,6 +22,7 @@ export type Product = {
22
22
 
23
23
  export type StorageItem = {
24
24
  id: number;
25
+ storage: Storage;
25
26
  product: Product;
26
27
  expiresAt: string;
27
28
  createdAt: string;
@@ -53,10 +54,4 @@ export type ShoppingListItem = {
53
54
  storage: Storage;
54
55
  product: Product;
55
56
  amountToBuy: number;
56
- };
57
-
58
- export type ToPurchaseItem = {
59
- productId: number;
60
- productName: string;
61
- amountToBuy: number;
62
- };
57
+ };
@@ -1,43 +0,0 @@
1
- import React, { createContext, useCallback, useContext, useMemo, useState } from 'react';
2
- import type { ToPurchaseItem } from '../type/models.js';
3
- import { useAuth } from './AuthContext.js';
4
- import { fetchAggregatedShoppingRequest } from './api/toPurchaseApi.js';
5
-
6
- type ToPurchaseContextValue = {
7
- items: ToPurchaseItem[];
8
- isLoading: boolean;
9
- isError: boolean;
10
- error: Error | null;
11
- fetchAggregated: () => Promise<ToPurchaseItem[]>;
12
- };
13
-
14
- type ToPurchaseProviderProps = {
15
- baseUrl: string;
16
- children: React.ReactNode;
17
- };
18
-
19
- const ToPurchaseContext = createContext<ToPurchaseContextValue | undefined>(undefined);
20
-
21
- export const ToPurchaseProvider = ({ baseUrl, children }: ToPurchaseProviderProps) => {
22
- const { token } = useAuth();
23
- const [items, setItems] = useState<ToPurchaseItem[]>([]);
24
- const [isLoading, setIsLoading] = useState(false);
25
- const [isError, setIsError] = useState(false);
26
- const [error, setError] = useState<Error | null>(null);
27
-
28
- const apiConfig = { baseUrl, token, setToPurchase: setItems, setIsLoading, setIsError, setError };
29
-
30
- const fetchAggregated = useCallback(async () => {
31
- return fetchAggregatedShoppingRequest(apiConfig);
32
- }, [apiConfig]);
33
-
34
- const value = useMemo(() => ({ items, isLoading, isError, error, fetchAggregated }), [items, isLoading, isError, error, fetchAggregated]);
35
-
36
- return <ToPurchaseContext.Provider value={value}>{children}</ToPurchaseContext.Provider>;
37
- };
38
-
39
- export const useToPurchase = (): ToPurchaseContextValue => {
40
- const context = useContext(ToPurchaseContext);
41
- if (!context) throw new Error('useToPurchase must be used within a ToPurchaseProvider');
42
- return context;
43
- };
@@ -1,30 +0,0 @@
1
- import type { ToPurchaseItem } from '../../type/models.js';
2
- import { buildAuthHeaders, normalizeBaseUrl, readJson } from '../http.js';
3
- import { runWithRequestState, type RequestStateHandlers } from './requestState.js';
4
-
5
- type ToPurchaseApiConfig = RequestStateHandlers & {
6
- baseUrl: string;
7
- token: string | null;
8
- setToPurchase: (value: ToPurchaseItem[] | ((items: ToPurchaseItem[]) => ToPurchaseItem[])) => void;
9
- };
10
-
11
- export const fetchAggregatedShoppingRequest = async (
12
- config: ToPurchaseApiConfig
13
- ): Promise<ToPurchaseItem[]> => runWithRequestState(config, async () => {
14
- const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
15
- const response = await fetch(`${normalizedBaseUrl}/api/tobuy/shopping`, {
16
- headers: buildAuthHeaders(config.token)
17
- });
18
-
19
- if (!response.ok) {
20
- throw new Error('Failed to fetch aggregated shopping list');
21
- }
22
-
23
- const payload = await readJson<ToPurchaseItem[]>(response);
24
- if (payload) {
25
- config.setToPurchase(payload);
26
- return payload;
27
- }
28
-
29
- return [];
30
- });