shelflife-react-hooks 1.0.18 → 1.0.20
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 +23 -0
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +23 -0
- package/dist/index.esm.js.map +1 -1
- package/package.json +36 -36
- package/src/context/AuthContext.tsx +161 -161
- package/src/context/InviteContext.tsx +74 -74
- package/src/context/ProductContext.tsx +131 -121
- package/src/context/RunningLowContext.tsx +100 -100
- package/src/context/ShoppingListContext.tsx +76 -76
- package/src/context/StorageContext.tsx +105 -105
- package/src/context/StorageItemContext.tsx +157 -157
- package/src/context/StorageMemberContext.tsx +84 -84
- package/src/context/UserContext.tsx +109 -109
- package/src/context/__tests__/contexts.test.tsx +370 -370
- package/src/context/api/authApi.ts +155 -155
- package/src/context/api/inviteApi.ts +65 -65
- package/src/context/api/productApi.ts +223 -201
- package/src/context/api/requestState.ts +24 -24
- package/src/context/api/runningLowApi.ts +141 -141
- package/src/context/api/shoppingListApi.ts +161 -159
- package/src/context/api/storageApi.ts +166 -166
- package/src/context/api/storageItemApi.ts +260 -260
- package/src/context/api/storageMemberApi.ts +84 -84
- package/src/context/api/userApi.ts +161 -161
- package/src/context/http.ts +22 -22
- package/src/index.ts +21 -21
- package/src/type/PaginatedResponse.ts +8 -8
- package/src/type/auth.ts +79 -79
- package/src/type/base.ts +21 -21
- package/src/type/item.ts +12 -12
- package/src/type/member.ts +6 -6
- package/src/type/models.ts +56 -56
- package/src/type/product.ts +11 -11
- package/src/type/requests.ts +60 -60
- package/src/type/runninglow.ts +13 -13
- package/src/type/shoppingList.ts +13 -13
- package/src/type/storage.ts +7 -7
- package/src/type/user.ts +11 -11
- package/tsconfig.json +46 -46
- package/tsup.config.ts +10 -10
- package/vitest.config.ts +8 -8
|
@@ -1,105 +1,105 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
createContext,
|
|
3
|
-
useCallback,
|
|
4
|
-
useContext,
|
|
5
|
-
useMemo,
|
|
6
|
-
useState
|
|
7
|
-
} from 'react';
|
|
8
|
-
|
|
9
|
-
import type { Storage } from '../type/models.js';
|
|
10
|
-
import type { ChangeStorageNameRequest, CreateStorageRequest } from '../type/requests.js';
|
|
11
|
-
import { useAuth } from './AuthContext.js';
|
|
12
|
-
import {
|
|
13
|
-
changeStorageNameRequest,
|
|
14
|
-
createStorageRequest,
|
|
15
|
-
deleteStorageRequest,
|
|
16
|
-
fetchStorageRequest,
|
|
17
|
-
fetchStoragesRequest
|
|
18
|
-
} from './api/storageApi.js';
|
|
19
|
-
import type { PaginatedResponse } from '../type/PaginatedResponse.js';
|
|
20
|
-
|
|
21
|
-
type StorageContextValue = {
|
|
22
|
-
storages: Storage[];
|
|
23
|
-
storage: Storage | null;
|
|
24
|
-
isLoading: boolean;
|
|
25
|
-
isError: boolean;
|
|
26
|
-
error: Error | null;
|
|
27
|
-
fetchStorages: (search?: string, size?: number, page?: number) => Promise<PaginatedResponse<Storage>>;
|
|
28
|
-
fetchStorage: (id: number) => Promise<Storage | null>;
|
|
29
|
-
createStorage: (dto: CreateStorageRequest) => Promise<Storage>;
|
|
30
|
-
changeStorageName: (id: number, dto: ChangeStorageNameRequest) => Promise<Storage>;
|
|
31
|
-
deleteStorage: (id: number) => Promise<void>;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
type StorageProviderProps = {
|
|
35
|
-
baseUrl: string;
|
|
36
|
-
children: React.ReactNode;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const StorageContext = createContext<StorageContextValue | undefined>(undefined);
|
|
40
|
-
|
|
41
|
-
export const StorageProvider = ({ baseUrl, children }: StorageProviderProps) => {
|
|
42
|
-
const { token } = useAuth();
|
|
43
|
-
const [storages, setStorages] = useState<Storage[]>([]);
|
|
44
|
-
const [storage, setStorage] = useState<Storage | null>(null);
|
|
45
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
46
|
-
const [isError, setIsError] = useState(false);
|
|
47
|
-
const [error, setError] = useState<Error | null>(null);
|
|
48
|
-
|
|
49
|
-
const apiConfig = useMemo(() => ({
|
|
50
|
-
baseUrl,
|
|
51
|
-
token,
|
|
52
|
-
setStorages,
|
|
53
|
-
setStorage,
|
|
54
|
-
setIsLoading,
|
|
55
|
-
setIsError,
|
|
56
|
-
setError
|
|
57
|
-
}), [baseUrl, token]);
|
|
58
|
-
|
|
59
|
-
const fetchStorages = useCallback((search: string = "", size: number = 0, page: number = 0) => fetchStoragesRequest(apiConfig, search, size, page), [apiConfig]);
|
|
60
|
-
const fetchStorage = useCallback((id: number) => fetchStorageRequest(apiConfig, id), [apiConfig]);
|
|
61
|
-
const createStorage = useCallback(
|
|
62
|
-
(dto: CreateStorageRequest) => createStorageRequest(apiConfig, dto),
|
|
63
|
-
[apiConfig]
|
|
64
|
-
);
|
|
65
|
-
const changeStorageName = useCallback(
|
|
66
|
-
(id: number, dto: ChangeStorageNameRequest) => changeStorageNameRequest(apiConfig, id, dto),
|
|
67
|
-
[apiConfig]
|
|
68
|
-
);
|
|
69
|
-
const deleteStorage = useCallback((id: number) => deleteStorageRequest(apiConfig, id), [apiConfig]);
|
|
70
|
-
|
|
71
|
-
const value = useMemo<StorageContextValue>(() => ({
|
|
72
|
-
storages,
|
|
73
|
-
storage,
|
|
74
|
-
isLoading,
|
|
75
|
-
isError,
|
|
76
|
-
error,
|
|
77
|
-
fetchStorages,
|
|
78
|
-
fetchStorage,
|
|
79
|
-
createStorage,
|
|
80
|
-
changeStorageName,
|
|
81
|
-
deleteStorage
|
|
82
|
-
}), [
|
|
83
|
-
changeStorageName,
|
|
84
|
-
createStorage,
|
|
85
|
-
deleteStorage,
|
|
86
|
-
error,
|
|
87
|
-
fetchStorage,
|
|
88
|
-
fetchStorages,
|
|
89
|
-
isError,
|
|
90
|
-
isLoading,
|
|
91
|
-
storage,
|
|
92
|
-
storages
|
|
93
|
-
]);
|
|
94
|
-
|
|
95
|
-
return <StorageContext.Provider value={value}>{children}</StorageContext.Provider>;
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
export const useStorages = (): StorageContextValue => {
|
|
99
|
-
const context = useContext(StorageContext);
|
|
100
|
-
if (!context) {
|
|
101
|
-
throw new Error('useStorages must be used within a StorageProvider');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return context;
|
|
105
|
-
};
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { Storage } from '../type/models.js';
|
|
10
|
+
import type { ChangeStorageNameRequest, CreateStorageRequest } from '../type/requests.js';
|
|
11
|
+
import { useAuth } from './AuthContext.js';
|
|
12
|
+
import {
|
|
13
|
+
changeStorageNameRequest,
|
|
14
|
+
createStorageRequest,
|
|
15
|
+
deleteStorageRequest,
|
|
16
|
+
fetchStorageRequest,
|
|
17
|
+
fetchStoragesRequest
|
|
18
|
+
} from './api/storageApi.js';
|
|
19
|
+
import type { PaginatedResponse } from '../type/PaginatedResponse.js';
|
|
20
|
+
|
|
21
|
+
type StorageContextValue = {
|
|
22
|
+
storages: Storage[];
|
|
23
|
+
storage: Storage | null;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
isError: boolean;
|
|
26
|
+
error: Error | null;
|
|
27
|
+
fetchStorages: (search?: string, size?: number, page?: number) => Promise<PaginatedResponse<Storage>>;
|
|
28
|
+
fetchStorage: (id: number) => Promise<Storage | null>;
|
|
29
|
+
createStorage: (dto: CreateStorageRequest) => Promise<Storage>;
|
|
30
|
+
changeStorageName: (id: number, dto: ChangeStorageNameRequest) => Promise<Storage>;
|
|
31
|
+
deleteStorage: (id: number) => Promise<void>;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
type StorageProviderProps = {
|
|
35
|
+
baseUrl: string;
|
|
36
|
+
children: React.ReactNode;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const StorageContext = createContext<StorageContextValue | undefined>(undefined);
|
|
40
|
+
|
|
41
|
+
export const StorageProvider = ({ baseUrl, children }: StorageProviderProps) => {
|
|
42
|
+
const { token } = useAuth();
|
|
43
|
+
const [storages, setStorages] = useState<Storage[]>([]);
|
|
44
|
+
const [storage, setStorage] = useState<Storage | null>(null);
|
|
45
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
46
|
+
const [isError, setIsError] = useState(false);
|
|
47
|
+
const [error, setError] = useState<Error | null>(null);
|
|
48
|
+
|
|
49
|
+
const apiConfig = useMemo(() => ({
|
|
50
|
+
baseUrl,
|
|
51
|
+
token,
|
|
52
|
+
setStorages,
|
|
53
|
+
setStorage,
|
|
54
|
+
setIsLoading,
|
|
55
|
+
setIsError,
|
|
56
|
+
setError
|
|
57
|
+
}), [baseUrl, token]);
|
|
58
|
+
|
|
59
|
+
const fetchStorages = useCallback((search: string = "", size: number = 0, page: number = 0) => fetchStoragesRequest(apiConfig, search, size, page), [apiConfig]);
|
|
60
|
+
const fetchStorage = useCallback((id: number) => fetchStorageRequest(apiConfig, id), [apiConfig]);
|
|
61
|
+
const createStorage = useCallback(
|
|
62
|
+
(dto: CreateStorageRequest) => createStorageRequest(apiConfig, dto),
|
|
63
|
+
[apiConfig]
|
|
64
|
+
);
|
|
65
|
+
const changeStorageName = useCallback(
|
|
66
|
+
(id: number, dto: ChangeStorageNameRequest) => changeStorageNameRequest(apiConfig, id, dto),
|
|
67
|
+
[apiConfig]
|
|
68
|
+
);
|
|
69
|
+
const deleteStorage = useCallback((id: number) => deleteStorageRequest(apiConfig, id), [apiConfig]);
|
|
70
|
+
|
|
71
|
+
const value = useMemo<StorageContextValue>(() => ({
|
|
72
|
+
storages,
|
|
73
|
+
storage,
|
|
74
|
+
isLoading,
|
|
75
|
+
isError,
|
|
76
|
+
error,
|
|
77
|
+
fetchStorages,
|
|
78
|
+
fetchStorage,
|
|
79
|
+
createStorage,
|
|
80
|
+
changeStorageName,
|
|
81
|
+
deleteStorage
|
|
82
|
+
}), [
|
|
83
|
+
changeStorageName,
|
|
84
|
+
createStorage,
|
|
85
|
+
deleteStorage,
|
|
86
|
+
error,
|
|
87
|
+
fetchStorage,
|
|
88
|
+
fetchStorages,
|
|
89
|
+
isError,
|
|
90
|
+
isLoading,
|
|
91
|
+
storage,
|
|
92
|
+
storages
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
return <StorageContext.Provider value={value}>{children}</StorageContext.Provider>;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export const useStorages = (): StorageContextValue => {
|
|
99
|
+
const context = useContext(StorageContext);
|
|
100
|
+
if (!context) {
|
|
101
|
+
throw new Error('useStorages must be used within a StorageProvider');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return context;
|
|
105
|
+
};
|
|
@@ -1,157 +1,157 @@
|
|
|
1
|
-
import React, {
|
|
2
|
-
createContext,
|
|
3
|
-
useCallback,
|
|
4
|
-
useContext,
|
|
5
|
-
useMemo,
|
|
6
|
-
useState
|
|
7
|
-
} from 'react';
|
|
8
|
-
|
|
9
|
-
import type { RunningLowNotification, StorageItem } from '../type/models.js';
|
|
10
|
-
import type { AddItemRequest, EditItemRequest } from '../type/requests.js';
|
|
11
|
-
import { useAuth } from './AuthContext.js';
|
|
12
|
-
import {
|
|
13
|
-
addItemRequest,
|
|
14
|
-
deleteItemRequest,
|
|
15
|
-
editItemRequest,
|
|
16
|
-
fetchAboutToExpireRequest,
|
|
17
|
-
fetchAggregatedAboutToExpireRequest,
|
|
18
|
-
fetchAggregatedExpiredRequest,
|
|
19
|
-
fetchAggregatedRunningLowRequest,
|
|
20
|
-
fetchExpiredRequest,
|
|
21
|
-
fetchItemsRequest,
|
|
22
|
-
fetchRunningLowRequest
|
|
23
|
-
} from './api/storageItemApi.js';
|
|
24
|
-
|
|
25
|
-
type StorageItemContextValue = {
|
|
26
|
-
items: StorageItem[];
|
|
27
|
-
expiredItems: StorageItem[];
|
|
28
|
-
aboutToExpireItems: StorageItem[];
|
|
29
|
-
runningLow: RunningLowNotification[];
|
|
30
|
-
isLoading: boolean;
|
|
31
|
-
isError: boolean;
|
|
32
|
-
error: Error | null;
|
|
33
|
-
fetchItems: (storageId: number) => Promise<StorageItem[]>;
|
|
34
|
-
addItem: (storageId: number, dto: AddItemRequest) => Promise<StorageItem>;
|
|
35
|
-
editItem: (storageId: number, itemId: number, dto: EditItemRequest) => Promise<StorageItem>;
|
|
36
|
-
deleteItem: (storageId: number, itemId: number) => Promise<void>;
|
|
37
|
-
fetchExpired: (storageId: number) => Promise<StorageItem[]>;
|
|
38
|
-
fetchExpiredAggregated: () => Promise<StorageItem[]>;
|
|
39
|
-
fetchAboutToExpire: (storageId: number) => Promise<StorageItem[]>;
|
|
40
|
-
fetchAboutToExpireAggregated: () => Promise<StorageItem[]>;
|
|
41
|
-
fetchRunningLow: (storageId: number) => Promise<RunningLowNotification[]>;
|
|
42
|
-
fetchRunningLowAggregated: () => Promise<RunningLowNotification[]>;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
type StorageItemProviderProps = {
|
|
46
|
-
baseUrl: string;
|
|
47
|
-
children: React.ReactNode;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const StorageItemContext = createContext<StorageItemContextValue | undefined>(undefined);
|
|
51
|
-
|
|
52
|
-
export const StorageItemProvider = ({ baseUrl, children }: StorageItemProviderProps) => {
|
|
53
|
-
const { token } = useAuth();
|
|
54
|
-
const [items, setItems] = useState<StorageItem[]>([]);
|
|
55
|
-
const [expiredItems, setExpiredItems] = useState<StorageItem[]>([]);
|
|
56
|
-
const [aboutToExpireItems, setAboutToExpireItems] = useState<StorageItem[]>([]);
|
|
57
|
-
const [runningLow, setRunningLow] = useState<RunningLowNotification[]>([]);
|
|
58
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
59
|
-
const [isError, setIsError] = useState(false);
|
|
60
|
-
const [error, setError] = useState<Error | null>(null);
|
|
61
|
-
|
|
62
|
-
const apiConfig = useMemo(() => ({
|
|
63
|
-
baseUrl,
|
|
64
|
-
token,
|
|
65
|
-
setItems,
|
|
66
|
-
setExpiredItems,
|
|
67
|
-
setAboutToExpireItems,
|
|
68
|
-
setRunningLow,
|
|
69
|
-
setIsLoading,
|
|
70
|
-
setIsError,
|
|
71
|
-
setError
|
|
72
|
-
}), [baseUrl, token]);
|
|
73
|
-
|
|
74
|
-
const fetchItems = useCallback((storageId: number) => fetchItemsRequest(apiConfig, storageId), [apiConfig]);
|
|
75
|
-
const addItem = useCallback(
|
|
76
|
-
(storageId: number, dto: AddItemRequest) => addItemRequest(apiConfig, storageId, dto),
|
|
77
|
-
[apiConfig]
|
|
78
|
-
);
|
|
79
|
-
const editItem = useCallback(
|
|
80
|
-
(storageId: number, itemId: number, dto: EditItemRequest) => editItemRequest(apiConfig, storageId, itemId, dto),
|
|
81
|
-
[apiConfig]
|
|
82
|
-
);
|
|
83
|
-
const deleteItem = useCallback(
|
|
84
|
-
(storageId: number, itemId: number) => deleteItemRequest(apiConfig, storageId, itemId),
|
|
85
|
-
[apiConfig]
|
|
86
|
-
);
|
|
87
|
-
const fetchExpired = useCallback(
|
|
88
|
-
(storageId: number) => fetchExpiredRequest(apiConfig, storageId),
|
|
89
|
-
[apiConfig]
|
|
90
|
-
);
|
|
91
|
-
const fetchExpiredAggregated = useCallback(
|
|
92
|
-
() => fetchAggregatedExpiredRequest(apiConfig),
|
|
93
|
-
[apiConfig]
|
|
94
|
-
);
|
|
95
|
-
const fetchAboutToExpire = useCallback(
|
|
96
|
-
(storageId: number) => fetchAboutToExpireRequest(apiConfig, storageId),
|
|
97
|
-
[apiConfig]
|
|
98
|
-
);
|
|
99
|
-
const fetchAboutToExpireAggregated = useCallback(
|
|
100
|
-
() => fetchAggregatedAboutToExpireRequest(apiConfig),
|
|
101
|
-
[apiConfig]
|
|
102
|
-
);
|
|
103
|
-
const fetchRunningLow = useCallback(
|
|
104
|
-
(storageId: number) => fetchRunningLowRequest(apiConfig, storageId),
|
|
105
|
-
[apiConfig]
|
|
106
|
-
);
|
|
107
|
-
const fetchRunningLowAggregated = useCallback(
|
|
108
|
-
() => fetchAggregatedRunningLowRequest(apiConfig),
|
|
109
|
-
[apiConfig]
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
const value = useMemo<StorageItemContextValue>(() => ({
|
|
113
|
-
items,
|
|
114
|
-
expiredItems,
|
|
115
|
-
aboutToExpireItems,
|
|
116
|
-
runningLow,
|
|
117
|
-
isLoading,
|
|
118
|
-
isError,
|
|
119
|
-
error,
|
|
120
|
-
fetchItems,
|
|
121
|
-
addItem,
|
|
122
|
-
editItem,
|
|
123
|
-
deleteItem,
|
|
124
|
-
fetchExpired,
|
|
125
|
-
fetchExpiredAggregated,
|
|
126
|
-
fetchAboutToExpire,
|
|
127
|
-
fetchAboutToExpireAggregated,
|
|
128
|
-
fetchRunningLow,
|
|
129
|
-
fetchRunningLowAggregated
|
|
130
|
-
}), [
|
|
131
|
-
aboutToExpireItems,
|
|
132
|
-
addItem,
|
|
133
|
-
deleteItem,
|
|
134
|
-
editItem,
|
|
135
|
-
error,
|
|
136
|
-
expiredItems,
|
|
137
|
-
fetchAboutToExpire,
|
|
138
|
-
fetchExpired,
|
|
139
|
-
fetchItems,
|
|
140
|
-
fetchRunningLow,
|
|
141
|
-
isError,
|
|
142
|
-
isLoading,
|
|
143
|
-
items,
|
|
144
|
-
runningLow
|
|
145
|
-
]);
|
|
146
|
-
|
|
147
|
-
return <StorageItemContext.Provider value={value}>{children}</StorageItemContext.Provider>;
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export const useStorageItems = (): StorageItemContextValue => {
|
|
151
|
-
const context = useContext(StorageItemContext);
|
|
152
|
-
if (!context) {
|
|
153
|
-
throw new Error('useStorageItems must be used within a StorageItemProvider');
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return context;
|
|
157
|
-
};
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { RunningLowNotification, StorageItem } from '../type/models.js';
|
|
10
|
+
import type { AddItemRequest, EditItemRequest } from '../type/requests.js';
|
|
11
|
+
import { useAuth } from './AuthContext.js';
|
|
12
|
+
import {
|
|
13
|
+
addItemRequest,
|
|
14
|
+
deleteItemRequest,
|
|
15
|
+
editItemRequest,
|
|
16
|
+
fetchAboutToExpireRequest,
|
|
17
|
+
fetchAggregatedAboutToExpireRequest,
|
|
18
|
+
fetchAggregatedExpiredRequest,
|
|
19
|
+
fetchAggregatedRunningLowRequest,
|
|
20
|
+
fetchExpiredRequest,
|
|
21
|
+
fetchItemsRequest,
|
|
22
|
+
fetchRunningLowRequest
|
|
23
|
+
} from './api/storageItemApi.js';
|
|
24
|
+
|
|
25
|
+
type StorageItemContextValue = {
|
|
26
|
+
items: StorageItem[];
|
|
27
|
+
expiredItems: StorageItem[];
|
|
28
|
+
aboutToExpireItems: StorageItem[];
|
|
29
|
+
runningLow: RunningLowNotification[];
|
|
30
|
+
isLoading: boolean;
|
|
31
|
+
isError: boolean;
|
|
32
|
+
error: Error | null;
|
|
33
|
+
fetchItems: (storageId: number) => Promise<StorageItem[]>;
|
|
34
|
+
addItem: (storageId: number, dto: AddItemRequest) => Promise<StorageItem>;
|
|
35
|
+
editItem: (storageId: number, itemId: number, dto: EditItemRequest) => Promise<StorageItem>;
|
|
36
|
+
deleteItem: (storageId: number, itemId: number) => Promise<void>;
|
|
37
|
+
fetchExpired: (storageId: number) => Promise<StorageItem[]>;
|
|
38
|
+
fetchExpiredAggregated: () => Promise<StorageItem[]>;
|
|
39
|
+
fetchAboutToExpire: (storageId: number) => Promise<StorageItem[]>;
|
|
40
|
+
fetchAboutToExpireAggregated: () => Promise<StorageItem[]>;
|
|
41
|
+
fetchRunningLow: (storageId: number) => Promise<RunningLowNotification[]>;
|
|
42
|
+
fetchRunningLowAggregated: () => Promise<RunningLowNotification[]>;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type StorageItemProviderProps = {
|
|
46
|
+
baseUrl: string;
|
|
47
|
+
children: React.ReactNode;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const StorageItemContext = createContext<StorageItemContextValue | undefined>(undefined);
|
|
51
|
+
|
|
52
|
+
export const StorageItemProvider = ({ baseUrl, children }: StorageItemProviderProps) => {
|
|
53
|
+
const { token } = useAuth();
|
|
54
|
+
const [items, setItems] = useState<StorageItem[]>([]);
|
|
55
|
+
const [expiredItems, setExpiredItems] = useState<StorageItem[]>([]);
|
|
56
|
+
const [aboutToExpireItems, setAboutToExpireItems] = useState<StorageItem[]>([]);
|
|
57
|
+
const [runningLow, setRunningLow] = useState<RunningLowNotification[]>([]);
|
|
58
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
59
|
+
const [isError, setIsError] = useState(false);
|
|
60
|
+
const [error, setError] = useState<Error | null>(null);
|
|
61
|
+
|
|
62
|
+
const apiConfig = useMemo(() => ({
|
|
63
|
+
baseUrl,
|
|
64
|
+
token,
|
|
65
|
+
setItems,
|
|
66
|
+
setExpiredItems,
|
|
67
|
+
setAboutToExpireItems,
|
|
68
|
+
setRunningLow,
|
|
69
|
+
setIsLoading,
|
|
70
|
+
setIsError,
|
|
71
|
+
setError
|
|
72
|
+
}), [baseUrl, token]);
|
|
73
|
+
|
|
74
|
+
const fetchItems = useCallback((storageId: number) => fetchItemsRequest(apiConfig, storageId), [apiConfig]);
|
|
75
|
+
const addItem = useCallback(
|
|
76
|
+
(storageId: number, dto: AddItemRequest) => addItemRequest(apiConfig, storageId, dto),
|
|
77
|
+
[apiConfig]
|
|
78
|
+
);
|
|
79
|
+
const editItem = useCallback(
|
|
80
|
+
(storageId: number, itemId: number, dto: EditItemRequest) => editItemRequest(apiConfig, storageId, itemId, dto),
|
|
81
|
+
[apiConfig]
|
|
82
|
+
);
|
|
83
|
+
const deleteItem = useCallback(
|
|
84
|
+
(storageId: number, itemId: number) => deleteItemRequest(apiConfig, storageId, itemId),
|
|
85
|
+
[apiConfig]
|
|
86
|
+
);
|
|
87
|
+
const fetchExpired = useCallback(
|
|
88
|
+
(storageId: number) => fetchExpiredRequest(apiConfig, storageId),
|
|
89
|
+
[apiConfig]
|
|
90
|
+
);
|
|
91
|
+
const fetchExpiredAggregated = useCallback(
|
|
92
|
+
() => fetchAggregatedExpiredRequest(apiConfig),
|
|
93
|
+
[apiConfig]
|
|
94
|
+
);
|
|
95
|
+
const fetchAboutToExpire = useCallback(
|
|
96
|
+
(storageId: number) => fetchAboutToExpireRequest(apiConfig, storageId),
|
|
97
|
+
[apiConfig]
|
|
98
|
+
);
|
|
99
|
+
const fetchAboutToExpireAggregated = useCallback(
|
|
100
|
+
() => fetchAggregatedAboutToExpireRequest(apiConfig),
|
|
101
|
+
[apiConfig]
|
|
102
|
+
);
|
|
103
|
+
const fetchRunningLow = useCallback(
|
|
104
|
+
(storageId: number) => fetchRunningLowRequest(apiConfig, storageId),
|
|
105
|
+
[apiConfig]
|
|
106
|
+
);
|
|
107
|
+
const fetchRunningLowAggregated = useCallback(
|
|
108
|
+
() => fetchAggregatedRunningLowRequest(apiConfig),
|
|
109
|
+
[apiConfig]
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const value = useMemo<StorageItemContextValue>(() => ({
|
|
113
|
+
items,
|
|
114
|
+
expiredItems,
|
|
115
|
+
aboutToExpireItems,
|
|
116
|
+
runningLow,
|
|
117
|
+
isLoading,
|
|
118
|
+
isError,
|
|
119
|
+
error,
|
|
120
|
+
fetchItems,
|
|
121
|
+
addItem,
|
|
122
|
+
editItem,
|
|
123
|
+
deleteItem,
|
|
124
|
+
fetchExpired,
|
|
125
|
+
fetchExpiredAggregated,
|
|
126
|
+
fetchAboutToExpire,
|
|
127
|
+
fetchAboutToExpireAggregated,
|
|
128
|
+
fetchRunningLow,
|
|
129
|
+
fetchRunningLowAggregated
|
|
130
|
+
}), [
|
|
131
|
+
aboutToExpireItems,
|
|
132
|
+
addItem,
|
|
133
|
+
deleteItem,
|
|
134
|
+
editItem,
|
|
135
|
+
error,
|
|
136
|
+
expiredItems,
|
|
137
|
+
fetchAboutToExpire,
|
|
138
|
+
fetchExpired,
|
|
139
|
+
fetchItems,
|
|
140
|
+
fetchRunningLow,
|
|
141
|
+
isError,
|
|
142
|
+
isLoading,
|
|
143
|
+
items,
|
|
144
|
+
runningLow
|
|
145
|
+
]);
|
|
146
|
+
|
|
147
|
+
return <StorageItemContext.Provider value={value}>{children}</StorageItemContext.Provider>;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export const useStorageItems = (): StorageItemContextValue => {
|
|
151
|
+
const context = useContext(StorageItemContext);
|
|
152
|
+
if (!context) {
|
|
153
|
+
throw new Error('useStorageItems must be used within a StorageItemProvider');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return context;
|
|
157
|
+
};
|