shelflife-react-hooks 1.0.17 → 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.
- package/dist/index.cjs.js +32 -47
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +4 -20
- package/dist/index.d.ts +4 -20
- package/dist/index.esm.js +32 -45
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/context/ShoppingListContext.tsx +14 -2
- package/src/context/api/shoppingListApi.ts +38 -1
- package/src/index.ts +1 -2
- package/src/type/models.ts +2 -7
- package/src/context/ToPurchaseContext.tsx +0 -43
- package/src/context/api/toPurchaseApi.ts +0 -30
|
@@ -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
|
|
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
|
};
|
|
@@ -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
|
|
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';
|
package/src/type/models.ts
CHANGED
|
@@ -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/shoppinglist`, {
|
|
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
|
-
});
|