shelflife-react-hooks 1.0.0
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 +1374 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.cts +339 -0
- package/dist/index.d.ts +339 -0
- package/dist/index.esm.js +1376 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +33 -0
- package/src/context/AuthContext.tsx +109 -0
- package/src/context/InviteContext.tsx +74 -0
- package/src/context/ProductContext.tsx +126 -0
- package/src/context/RunningLowContext.tsx +100 -0
- package/src/context/StorageContext.tsx +104 -0
- package/src/context/StorageItemContext.tsx +136 -0
- package/src/context/StorageMemberContext.tsx +84 -0
- package/src/context/UserContext.tsx +109 -0
- package/src/context/__tests__/contexts.test.tsx +354 -0
- package/src/context/api/authApi.ts +141 -0
- package/src/context/api/inviteApi.ts +65 -0
- package/src/context/api/productApi.ts +204 -0
- package/src/context/api/requestState.ts +25 -0
- package/src/context/api/runningLowApi.ts +130 -0
- package/src/context/api/storageApi.ts +147 -0
- package/src/context/api/storageItemApi.ts +187 -0
- package/src/context/api/storageMemberApi.ts +78 -0
- package/src/context/api/userApi.ts +155 -0
- package/src/context/http.ts +22 -0
- package/src/index.ts +17 -0
- package/src/type/auth.ts +62 -0
- package/src/type/base.ts +22 -0
- package/src/type/models.ts +48 -0
- package/src/type/product.ts +29 -0
- package/src/type/requests.ts +49 -0
- package/src/type/runninglow.ts +23 -0
- package/src/type/storage.ts +70 -0
- package/src/type/user.ts +23 -0
- package/tsconfig.json +46 -0
- package/tsup.config.ts +11 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { RunningLowSetting } from '../type/models.js';
|
|
10
|
+
import type { CreateSettingRequest, EditSettingRequest } from '../type/requests.js';
|
|
11
|
+
import { useAuth } from './AuthContext.js';
|
|
12
|
+
import {
|
|
13
|
+
createSettingRequest,
|
|
14
|
+
deleteSettingRequest,
|
|
15
|
+
editSettingRequest,
|
|
16
|
+
fetchSettingsRequest
|
|
17
|
+
} from './api/runningLowApi.js';
|
|
18
|
+
|
|
19
|
+
type RunningLowContextValue = {
|
|
20
|
+
settings: RunningLowSetting[];
|
|
21
|
+
isLoading: boolean;
|
|
22
|
+
isError: boolean;
|
|
23
|
+
error: Error | null;
|
|
24
|
+
fetchSettings: (storageId: number) => Promise<RunningLowSetting[]>;
|
|
25
|
+
createSetting: (storageId: number, dto: CreateSettingRequest) => Promise<RunningLowSetting>;
|
|
26
|
+
editSetting: (storageId: number, id: number, dto: EditSettingRequest) => Promise<RunningLowSetting>;
|
|
27
|
+
deleteSetting: (storageId: number, id: number) => Promise<void>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type RunningLowProviderProps = {
|
|
31
|
+
baseUrl: string;
|
|
32
|
+
children: React.ReactNode;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const RunningLowContext = createContext<RunningLowContextValue | undefined>(undefined);
|
|
36
|
+
|
|
37
|
+
export const RunningLowProvider = ({ baseUrl, children }: RunningLowProviderProps) => {
|
|
38
|
+
const { token } = useAuth();
|
|
39
|
+
const [settings, setSettings] = useState<RunningLowSetting[]>([]);
|
|
40
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
41
|
+
const [isError, setIsError] = useState(false);
|
|
42
|
+
const [error, setError] = useState<Error | null>(null);
|
|
43
|
+
|
|
44
|
+
const apiConfig = useMemo(() => ({
|
|
45
|
+
baseUrl,
|
|
46
|
+
token,
|
|
47
|
+
setSettings,
|
|
48
|
+
setIsLoading,
|
|
49
|
+
setIsError,
|
|
50
|
+
setError
|
|
51
|
+
}), [baseUrl, token]);
|
|
52
|
+
|
|
53
|
+
const fetchSettings = useCallback(
|
|
54
|
+
(storageId: number) => fetchSettingsRequest(apiConfig, storageId),
|
|
55
|
+
[apiConfig]
|
|
56
|
+
);
|
|
57
|
+
const createSetting = useCallback(
|
|
58
|
+
(storageId: number, dto: CreateSettingRequest) => createSettingRequest(apiConfig, storageId, dto),
|
|
59
|
+
[apiConfig]
|
|
60
|
+
);
|
|
61
|
+
const editSetting = useCallback(
|
|
62
|
+
(storageId: number, id: number, dto: EditSettingRequest) => editSettingRequest(apiConfig, storageId, id, dto),
|
|
63
|
+
[apiConfig]
|
|
64
|
+
);
|
|
65
|
+
const deleteSetting = useCallback(
|
|
66
|
+
(storageId: number, id: number) => deleteSettingRequest(apiConfig, storageId, id),
|
|
67
|
+
[apiConfig]
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const value = useMemo<RunningLowContextValue>(() => ({
|
|
71
|
+
settings,
|
|
72
|
+
isLoading,
|
|
73
|
+
isError,
|
|
74
|
+
error,
|
|
75
|
+
fetchSettings,
|
|
76
|
+
createSetting,
|
|
77
|
+
editSetting,
|
|
78
|
+
deleteSetting
|
|
79
|
+
}), [
|
|
80
|
+
createSetting,
|
|
81
|
+
deleteSetting,
|
|
82
|
+
editSetting,
|
|
83
|
+
error,
|
|
84
|
+
fetchSettings,
|
|
85
|
+
isError,
|
|
86
|
+
isLoading,
|
|
87
|
+
settings
|
|
88
|
+
]);
|
|
89
|
+
|
|
90
|
+
return <RunningLowContext.Provider value={value}>{children}</RunningLowContext.Provider>;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const useRunningLow = (): RunningLowContextValue => {
|
|
94
|
+
const context = useContext(RunningLowContext);
|
|
95
|
+
if (!context) {
|
|
96
|
+
throw new Error('useRunningLow must be used within a RunningLowProvider');
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return context;
|
|
100
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
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
|
+
|
|
20
|
+
type StorageContextValue = {
|
|
21
|
+
storages: Storage[];
|
|
22
|
+
storage: Storage | null;
|
|
23
|
+
isLoading: boolean;
|
|
24
|
+
isError: boolean;
|
|
25
|
+
error: Error | null;
|
|
26
|
+
fetchStorages: () => Promise<Storage[]>;
|
|
27
|
+
fetchStorage: (id: number) => Promise<Storage | null>;
|
|
28
|
+
createStorage: (dto: CreateStorageRequest) => Promise<Storage>;
|
|
29
|
+
changeStorageName: (id: number, dto: ChangeStorageNameRequest) => Promise<Storage>;
|
|
30
|
+
deleteStorage: (id: number) => Promise<void>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type StorageProviderProps = {
|
|
34
|
+
baseUrl: string;
|
|
35
|
+
children: React.ReactNode;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const StorageContext = createContext<StorageContextValue | undefined>(undefined);
|
|
39
|
+
|
|
40
|
+
export const StorageProvider = ({ baseUrl, children }: StorageProviderProps) => {
|
|
41
|
+
const { token } = useAuth();
|
|
42
|
+
const [storages, setStorages] = useState<Storage[]>([]);
|
|
43
|
+
const [storage, setStorage] = useState<Storage | null>(null);
|
|
44
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
45
|
+
const [isError, setIsError] = useState(false);
|
|
46
|
+
const [error, setError] = useState<Error | null>(null);
|
|
47
|
+
|
|
48
|
+
const apiConfig = useMemo(() => ({
|
|
49
|
+
baseUrl,
|
|
50
|
+
token,
|
|
51
|
+
setStorages,
|
|
52
|
+
setStorage,
|
|
53
|
+
setIsLoading,
|
|
54
|
+
setIsError,
|
|
55
|
+
setError
|
|
56
|
+
}), [baseUrl, token]);
|
|
57
|
+
|
|
58
|
+
const fetchStorages = useCallback(() => fetchStoragesRequest(apiConfig), [apiConfig]);
|
|
59
|
+
const fetchStorage = useCallback((id: number) => fetchStorageRequest(apiConfig, id), [apiConfig]);
|
|
60
|
+
const createStorage = useCallback(
|
|
61
|
+
(dto: CreateStorageRequest) => createStorageRequest(apiConfig, dto),
|
|
62
|
+
[apiConfig]
|
|
63
|
+
);
|
|
64
|
+
const changeStorageName = useCallback(
|
|
65
|
+
(id: number, dto: ChangeStorageNameRequest) => changeStorageNameRequest(apiConfig, id, dto),
|
|
66
|
+
[apiConfig]
|
|
67
|
+
);
|
|
68
|
+
const deleteStorage = useCallback((id: number) => deleteStorageRequest(apiConfig, id), [apiConfig]);
|
|
69
|
+
|
|
70
|
+
const value = useMemo<StorageContextValue>(() => ({
|
|
71
|
+
storages,
|
|
72
|
+
storage,
|
|
73
|
+
isLoading,
|
|
74
|
+
isError,
|
|
75
|
+
error,
|
|
76
|
+
fetchStorages,
|
|
77
|
+
fetchStorage,
|
|
78
|
+
createStorage,
|
|
79
|
+
changeStorageName,
|
|
80
|
+
deleteStorage
|
|
81
|
+
}), [
|
|
82
|
+
changeStorageName,
|
|
83
|
+
createStorage,
|
|
84
|
+
deleteStorage,
|
|
85
|
+
error,
|
|
86
|
+
fetchStorage,
|
|
87
|
+
fetchStorages,
|
|
88
|
+
isError,
|
|
89
|
+
isLoading,
|
|
90
|
+
storage,
|
|
91
|
+
storages
|
|
92
|
+
]);
|
|
93
|
+
|
|
94
|
+
return <StorageContext.Provider value={value}>{children}</StorageContext.Provider>;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const useStorages = (): StorageContextValue => {
|
|
98
|
+
const context = useContext(StorageContext);
|
|
99
|
+
if (!context) {
|
|
100
|
+
throw new Error('useStorages must be used within a StorageProvider');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return context;
|
|
104
|
+
};
|
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
fetchExpiredRequest,
|
|
18
|
+
fetchItemsRequest,
|
|
19
|
+
fetchRunningLowRequest
|
|
20
|
+
} from './api/storageItemApi.js';
|
|
21
|
+
|
|
22
|
+
type StorageItemContextValue = {
|
|
23
|
+
items: StorageItem[];
|
|
24
|
+
expiredItems: StorageItem[];
|
|
25
|
+
aboutToExpireItems: StorageItem[];
|
|
26
|
+
runningLow: RunningLowNotification[];
|
|
27
|
+
isLoading: boolean;
|
|
28
|
+
isError: boolean;
|
|
29
|
+
error: Error | null;
|
|
30
|
+
fetchItems: (storageId: number) => Promise<StorageItem[]>;
|
|
31
|
+
addItem: (storageId: number, dto: AddItemRequest) => Promise<StorageItem>;
|
|
32
|
+
editItem: (storageId: number, itemId: number, dto: EditItemRequest) => Promise<StorageItem>;
|
|
33
|
+
deleteItem: (storageId: number, itemId: number) => Promise<void>;
|
|
34
|
+
fetchExpired: (storageId: number) => Promise<StorageItem[]>;
|
|
35
|
+
fetchAboutToExpire: (storageId: number) => Promise<StorageItem[]>;
|
|
36
|
+
fetchRunningLow: (storageId: number) => Promise<RunningLowNotification[]>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type StorageItemProviderProps = {
|
|
40
|
+
baseUrl: string;
|
|
41
|
+
children: React.ReactNode;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const StorageItemContext = createContext<StorageItemContextValue | undefined>(undefined);
|
|
45
|
+
|
|
46
|
+
export const StorageItemProvider = ({ baseUrl, children }: StorageItemProviderProps) => {
|
|
47
|
+
const { token } = useAuth();
|
|
48
|
+
const [items, setItems] = useState<StorageItem[]>([]);
|
|
49
|
+
const [expiredItems, setExpiredItems] = useState<StorageItem[]>([]);
|
|
50
|
+
const [aboutToExpireItems, setAboutToExpireItems] = useState<StorageItem[]>([]);
|
|
51
|
+
const [runningLow, setRunningLow] = useState<RunningLowNotification[]>([]);
|
|
52
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
53
|
+
const [isError, setIsError] = useState(false);
|
|
54
|
+
const [error, setError] = useState<Error | null>(null);
|
|
55
|
+
|
|
56
|
+
const apiConfig = useMemo(() => ({
|
|
57
|
+
baseUrl,
|
|
58
|
+
token,
|
|
59
|
+
setItems,
|
|
60
|
+
setExpiredItems,
|
|
61
|
+
setAboutToExpireItems,
|
|
62
|
+
setRunningLow,
|
|
63
|
+
setIsLoading,
|
|
64
|
+
setIsError,
|
|
65
|
+
setError
|
|
66
|
+
}), [baseUrl, token]);
|
|
67
|
+
|
|
68
|
+
const fetchItems = useCallback((storageId: number) => fetchItemsRequest(apiConfig, storageId), [apiConfig]);
|
|
69
|
+
const addItem = useCallback(
|
|
70
|
+
(storageId: number, dto: AddItemRequest) => addItemRequest(apiConfig, storageId, dto),
|
|
71
|
+
[apiConfig]
|
|
72
|
+
);
|
|
73
|
+
const editItem = useCallback(
|
|
74
|
+
(storageId: number, itemId: number, dto: EditItemRequest) => editItemRequest(apiConfig, storageId, itemId, dto),
|
|
75
|
+
[apiConfig]
|
|
76
|
+
);
|
|
77
|
+
const deleteItem = useCallback(
|
|
78
|
+
(storageId: number, itemId: number) => deleteItemRequest(apiConfig, storageId, itemId),
|
|
79
|
+
[apiConfig]
|
|
80
|
+
);
|
|
81
|
+
const fetchExpired = useCallback(
|
|
82
|
+
(storageId: number) => fetchExpiredRequest(apiConfig, storageId),
|
|
83
|
+
[apiConfig]
|
|
84
|
+
);
|
|
85
|
+
const fetchAboutToExpire = useCallback(
|
|
86
|
+
(storageId: number) => fetchAboutToExpireRequest(apiConfig, storageId),
|
|
87
|
+
[apiConfig]
|
|
88
|
+
);
|
|
89
|
+
const fetchRunningLow = useCallback(
|
|
90
|
+
(storageId: number) => fetchRunningLowRequest(apiConfig, storageId),
|
|
91
|
+
[apiConfig]
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const value = useMemo<StorageItemContextValue>(() => ({
|
|
95
|
+
items,
|
|
96
|
+
expiredItems,
|
|
97
|
+
aboutToExpireItems,
|
|
98
|
+
runningLow,
|
|
99
|
+
isLoading,
|
|
100
|
+
isError,
|
|
101
|
+
error,
|
|
102
|
+
fetchItems,
|
|
103
|
+
addItem,
|
|
104
|
+
editItem,
|
|
105
|
+
deleteItem,
|
|
106
|
+
fetchExpired,
|
|
107
|
+
fetchAboutToExpire,
|
|
108
|
+
fetchRunningLow
|
|
109
|
+
}), [
|
|
110
|
+
aboutToExpireItems,
|
|
111
|
+
addItem,
|
|
112
|
+
deleteItem,
|
|
113
|
+
editItem,
|
|
114
|
+
error,
|
|
115
|
+
expiredItems,
|
|
116
|
+
fetchAboutToExpire,
|
|
117
|
+
fetchExpired,
|
|
118
|
+
fetchItems,
|
|
119
|
+
fetchRunningLow,
|
|
120
|
+
isError,
|
|
121
|
+
isLoading,
|
|
122
|
+
items,
|
|
123
|
+
runningLow
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
return <StorageItemContext.Provider value={value}>{children}</StorageItemContext.Provider>;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export const useStorageItems = (): StorageItemContextValue => {
|
|
130
|
+
const context = useContext(StorageItemContext);
|
|
131
|
+
if (!context) {
|
|
132
|
+
throw new Error('useStorageItems must be used within a StorageItemProvider');
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return context;
|
|
136
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { StorageMember } from '../type/models.js';
|
|
10
|
+
import type { InviteMemberRequest } from '../type/requests.js';
|
|
11
|
+
import { useAuth } from './AuthContext.js';
|
|
12
|
+
import {
|
|
13
|
+
fetchMembersRequest,
|
|
14
|
+
inviteMemberRequest,
|
|
15
|
+
removeMemberRequest
|
|
16
|
+
} from './api/storageMemberApi.js';
|
|
17
|
+
|
|
18
|
+
type StorageMemberContextValue = {
|
|
19
|
+
members: StorageMember[];
|
|
20
|
+
isLoading: boolean;
|
|
21
|
+
isError: boolean;
|
|
22
|
+
error: Error | null;
|
|
23
|
+
fetchMembers: (storageId: number) => Promise<StorageMember[]>;
|
|
24
|
+
inviteMember: (storageId: number, dto: InviteMemberRequest) => Promise<StorageMember>;
|
|
25
|
+
removeMember: (storageId: number, userId: number) => Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type StorageMemberProviderProps = {
|
|
29
|
+
baseUrl: string;
|
|
30
|
+
children: React.ReactNode;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const StorageMemberContext = createContext<StorageMemberContextValue | undefined>(undefined);
|
|
34
|
+
|
|
35
|
+
export const StorageMemberProvider = ({ baseUrl, children }: StorageMemberProviderProps) => {
|
|
36
|
+
const { token } = useAuth();
|
|
37
|
+
const [members, setMembers] = useState<StorageMember[]>([]);
|
|
38
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
39
|
+
const [isError, setIsError] = useState(false);
|
|
40
|
+
const [error, setError] = useState<Error | null>(null);
|
|
41
|
+
|
|
42
|
+
const apiConfig = useMemo(() => ({
|
|
43
|
+
baseUrl,
|
|
44
|
+
token,
|
|
45
|
+
setMembers,
|
|
46
|
+
setIsLoading,
|
|
47
|
+
setIsError,
|
|
48
|
+
setError
|
|
49
|
+
}), [baseUrl, token]);
|
|
50
|
+
|
|
51
|
+
const fetchMembers = useCallback(
|
|
52
|
+
(storageId: number) => fetchMembersRequest(apiConfig, storageId),
|
|
53
|
+
[apiConfig]
|
|
54
|
+
);
|
|
55
|
+
const inviteMember = useCallback(
|
|
56
|
+
(storageId: number, dto: InviteMemberRequest) => inviteMemberRequest(apiConfig, storageId, dto),
|
|
57
|
+
[apiConfig]
|
|
58
|
+
);
|
|
59
|
+
const removeMember = useCallback(
|
|
60
|
+
(storageId: number, userId: number) => removeMemberRequest(apiConfig, storageId, userId),
|
|
61
|
+
[apiConfig]
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const value = useMemo<StorageMemberContextValue>(() => ({
|
|
65
|
+
members,
|
|
66
|
+
isLoading,
|
|
67
|
+
isError,
|
|
68
|
+
error,
|
|
69
|
+
fetchMembers,
|
|
70
|
+
inviteMember,
|
|
71
|
+
removeMember
|
|
72
|
+
}), [error, fetchMembers, inviteMember, isError, isLoading, members, removeMember]);
|
|
73
|
+
|
|
74
|
+
return <StorageMemberContext.Provider value={value}>{children}</StorageMemberContext.Provider>;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const useStorageMembers = (): StorageMemberContextValue => {
|
|
78
|
+
const context = useContext(StorageMemberContext);
|
|
79
|
+
if (!context) {
|
|
80
|
+
throw new Error('useStorageMembers must be used within a StorageMemberProvider');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return context;
|
|
84
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
createContext,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useMemo,
|
|
6
|
+
useState
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
import type { User } from '../type/models.js';
|
|
10
|
+
import type { ChangeUserDataRequest } from '../type/requests.js';
|
|
11
|
+
import { useAuth } from './AuthContext.js';
|
|
12
|
+
import {
|
|
13
|
+
deleteUserRequest,
|
|
14
|
+
fetchUserRequest,
|
|
15
|
+
fetchUsersRequest,
|
|
16
|
+
getUserPfpRequest,
|
|
17
|
+
updateUserRequest,
|
|
18
|
+
uploadUserPfpRequest
|
|
19
|
+
} from './api/userApi.js';
|
|
20
|
+
|
|
21
|
+
type UserContextValue = {
|
|
22
|
+
users: User[];
|
|
23
|
+
user: User | null;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
isError: boolean;
|
|
26
|
+
error: Error | null;
|
|
27
|
+
fetchUsers: () => Promise<User[]>;
|
|
28
|
+
fetchUser: (id: number) => Promise<User | null>;
|
|
29
|
+
updateUser: (id: number, dto: ChangeUserDataRequest) => Promise<User>;
|
|
30
|
+
deleteUser: (id: number) => Promise<void>;
|
|
31
|
+
getUserPfp: (id: number) => Promise<ArrayBuffer>;
|
|
32
|
+
uploadUserPfp: (id: number, file: Blob) => Promise<void>;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type UserProviderProps = {
|
|
36
|
+
baseUrl: string;
|
|
37
|
+
children: React.ReactNode;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const UserContext = createContext<UserContextValue | undefined>(undefined);
|
|
41
|
+
|
|
42
|
+
export const UserProvider = ({ baseUrl, children }: UserProviderProps) => {
|
|
43
|
+
const { token } = useAuth();
|
|
44
|
+
const [users, setUsers] = useState<User[]>([]);
|
|
45
|
+
const [user, setUser] = useState<User | null>(null);
|
|
46
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
47
|
+
const [isError, setIsError] = useState(false);
|
|
48
|
+
const [error, setError] = useState<Error | null>(null);
|
|
49
|
+
|
|
50
|
+
const apiConfig = useMemo(() => ({
|
|
51
|
+
baseUrl,
|
|
52
|
+
token,
|
|
53
|
+
setUsers,
|
|
54
|
+
setUser,
|
|
55
|
+
setIsLoading,
|
|
56
|
+
setIsError,
|
|
57
|
+
setError
|
|
58
|
+
}), [baseUrl, token]);
|
|
59
|
+
|
|
60
|
+
const fetchUsers = useCallback(() => fetchUsersRequest(apiConfig), [apiConfig]);
|
|
61
|
+
const fetchUser = useCallback((id: number) => fetchUserRequest(apiConfig, id), [apiConfig]);
|
|
62
|
+
const updateUser = useCallback(
|
|
63
|
+
(id: number, dto: ChangeUserDataRequest) => updateUserRequest(apiConfig, id, dto),
|
|
64
|
+
[apiConfig]
|
|
65
|
+
);
|
|
66
|
+
const deleteUser = useCallback((id: number) => deleteUserRequest(apiConfig, id), [apiConfig]);
|
|
67
|
+
const getUserPfp = useCallback((id: number) => getUserPfpRequest(apiConfig, id), [apiConfig]);
|
|
68
|
+
const uploadUserPfp = useCallback(
|
|
69
|
+
(id: number, file: Blob) => uploadUserPfpRequest(apiConfig, id, file),
|
|
70
|
+
[apiConfig]
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const value = useMemo<UserContextValue>(() => ({
|
|
74
|
+
users,
|
|
75
|
+
user,
|
|
76
|
+
isLoading,
|
|
77
|
+
isError,
|
|
78
|
+
error,
|
|
79
|
+
fetchUsers,
|
|
80
|
+
fetchUser,
|
|
81
|
+
updateUser,
|
|
82
|
+
deleteUser,
|
|
83
|
+
getUserPfp,
|
|
84
|
+
uploadUserPfp
|
|
85
|
+
}), [
|
|
86
|
+
deleteUser,
|
|
87
|
+
error,
|
|
88
|
+
fetchUser,
|
|
89
|
+
fetchUsers,
|
|
90
|
+
getUserPfp,
|
|
91
|
+
isError,
|
|
92
|
+
isLoading,
|
|
93
|
+
updateUser,
|
|
94
|
+
uploadUserPfp,
|
|
95
|
+
user,
|
|
96
|
+
users
|
|
97
|
+
]);
|
|
98
|
+
|
|
99
|
+
return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const useUsers = (): UserContextValue => {
|
|
103
|
+
const context = useContext(UserContext);
|
|
104
|
+
if (!context) {
|
|
105
|
+
throw new Error('useUsers must be used within a UserProvider');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return context;
|
|
109
|
+
};
|