shelflife-react-hooks 1.0.8 → 1.0.9
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 +155 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +50 -1
- package/dist/index.d.ts +50 -1
- package/dist/index.esm.js +151 -0
- package/dist/index.esm.js.map +1 -1
- package/package.json +1 -1
- package/src/context/ShoppingListContext.tsx +64 -0
- package/src/context/ToPurchaseContext.tsx +43 -0
- package/src/context/api/shoppingListApi.ts +111 -0
- package/src/context/api/toPurchaseApi.ts +30 -0
- package/src/index.ts +3 -1
- package/src/type/models.ts +13 -0
- package/src/type/requests.ts +9 -0
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { ShoppingListItem } from '../../type/models.js';
|
|
2
|
+
import type { CreateShoppingItemRequest, EditShoppingItemRequest } from '../../type/requests.js';
|
|
3
|
+
import { buildAuthHeaders, normalizeBaseUrl, readJson } from '../http.js';
|
|
4
|
+
import { runWithRequestState, type RequestStateHandlers } from './requestState.js';
|
|
5
|
+
|
|
6
|
+
type ShoppingListApiConfig = RequestStateHandlers & {
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
token: string | null;
|
|
9
|
+
setItems: (value: ShoppingListItem[] | ((items: ShoppingListItem[]) => ShoppingListItem[])) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const updateById = (items: ShoppingListItem[], updated: ShoppingListItem): ShoppingListItem[] => {
|
|
13
|
+
const index = items.findIndex((i) => i.id === updated.id);
|
|
14
|
+
if (index === -1) return [updated, ...items];
|
|
15
|
+
const next = [...items];
|
|
16
|
+
next[index] = updated;
|
|
17
|
+
return next;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const fetchShoppingListRequest = async (
|
|
21
|
+
config: ShoppingListApiConfig,
|
|
22
|
+
storageId: number
|
|
23
|
+
): Promise<ShoppingListItem[]> => runWithRequestState(config, async () => {
|
|
24
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
25
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist`, {
|
|
26
|
+
headers: buildAuthHeaders(config.token)
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw new Error('Failed to fetch shopping list items');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const payload = await readJson<ShoppingListItem[]>(response);
|
|
34
|
+
if (payload) {
|
|
35
|
+
config.setItems(payload);
|
|
36
|
+
return payload;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return [];
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
export const createShoppingItemRequest = async (
|
|
43
|
+
config: ShoppingListApiConfig,
|
|
44
|
+
storageId: number,
|
|
45
|
+
dto: CreateShoppingItemRequest
|
|
46
|
+
): Promise<ShoppingListItem> => runWithRequestState(config, async () => {
|
|
47
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
48
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist`, {
|
|
49
|
+
method: 'POST',
|
|
50
|
+
headers: {
|
|
51
|
+
...buildAuthHeaders(config.token),
|
|
52
|
+
'Content-Type': 'application/json'
|
|
53
|
+
},
|
|
54
|
+
body: JSON.stringify(dto)
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
throw new Error('Failed to create shopping list item');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const payload = await readJson<ShoppingListItem>(response);
|
|
62
|
+
if (!payload) throw new Error('Create shopping item response missing data');
|
|
63
|
+
|
|
64
|
+
config.setItems((previous) => [payload, ...previous]);
|
|
65
|
+
return payload;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const editShoppingItemRequest = async (
|
|
69
|
+
config: ShoppingListApiConfig,
|
|
70
|
+
storageId: number,
|
|
71
|
+
itemId: number,
|
|
72
|
+
dto: EditShoppingItemRequest
|
|
73
|
+
): Promise<ShoppingListItem> => runWithRequestState(config, async () => {
|
|
74
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
75
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist/${itemId}`, {
|
|
76
|
+
method: 'PUT',
|
|
77
|
+
headers: {
|
|
78
|
+
...buildAuthHeaders(config.token),
|
|
79
|
+
'Content-Type': 'application/json'
|
|
80
|
+
},
|
|
81
|
+
body: JSON.stringify(dto)
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (!response.ok) {
|
|
85
|
+
throw new Error('Failed to edit shopping list item');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const payload = await readJson<ShoppingListItem>(response);
|
|
89
|
+
if (!payload) throw new Error('Edit shopping item response missing data');
|
|
90
|
+
|
|
91
|
+
config.setItems((previous) => updateById(previous, payload));
|
|
92
|
+
return payload;
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
export const deleteShoppingItemRequest = async (
|
|
96
|
+
config: ShoppingListApiConfig,
|
|
97
|
+
storageId: number,
|
|
98
|
+
itemId: number
|
|
99
|
+
): Promise<void> => runWithRequestState(config, async () => {
|
|
100
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
101
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist/${itemId}`, {
|
|
102
|
+
method: 'DELETE',
|
|
103
|
+
headers: buildAuthHeaders(config.token)
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
throw new Error('Failed to delete shopping list item');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
config.setItems((previous) => previous.filter((i) => i.id !== itemId));
|
|
111
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -14,4 +14,6 @@ export * from './context/RunningLowContext.js';
|
|
|
14
14
|
export * from './context/StorageContext.js';
|
|
15
15
|
export * from './context/StorageItemContext.js';
|
|
16
16
|
export * from './context/StorageMemberContext.js';
|
|
17
|
-
export * from './context/UserContext.js';
|
|
17
|
+
export * from './context/UserContext.js';
|
|
18
|
+
export * from './context/ShoppingListContext.js';
|
|
19
|
+
export * from './context/ToPurchaseContext.js';
|
package/src/type/models.ts
CHANGED
|
@@ -46,3 +46,16 @@ export type RunningLowNotification = {
|
|
|
46
46
|
runningLowAt: number;
|
|
47
47
|
amount: number;
|
|
48
48
|
};
|
|
49
|
+
|
|
50
|
+
export type ShoppingListItem = {
|
|
51
|
+
id: number;
|
|
52
|
+
storage: Storage;
|
|
53
|
+
product: Product;
|
|
54
|
+
amountToBuy: number;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type ToPurchaseItem = {
|
|
58
|
+
productId: number;
|
|
59
|
+
productName: string;
|
|
60
|
+
amountToBuy: number;
|
|
61
|
+
};
|
package/src/type/requests.ts
CHANGED
|
@@ -30,6 +30,15 @@ export type EditItemRequest = {
|
|
|
30
30
|
expiresAt?: string;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
export type CreateShoppingItemRequest = {
|
|
34
|
+
productId: number;
|
|
35
|
+
amountToBuy: number;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type EditShoppingItemRequest = {
|
|
39
|
+
amountToBuy: number;
|
|
40
|
+
};
|
|
41
|
+
|
|
33
42
|
export type CreateStorageRequest = {
|
|
34
43
|
name: string;
|
|
35
44
|
};
|