shelflife-react-hooks 1.0.12 → 1.0.13
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/package.json
CHANGED
|
@@ -31,7 +31,7 @@ type ProductContextValue = {
|
|
|
31
31
|
isLoading: boolean;
|
|
32
32
|
isError: boolean;
|
|
33
33
|
error: Error | null;
|
|
34
|
-
fetchProducts: (
|
|
34
|
+
fetchProducts: (search: string, size: number, page: number) => Promise<Product[]>;
|
|
35
35
|
fetchProduct: (id: number) => Promise<Product | null>;
|
|
36
36
|
createProduct: (dto: CreateProductRequest) => Promise<Product>;
|
|
37
37
|
updateProduct: (id: number, dto: UpdateProductRequest) => Promise<Product>;
|
|
@@ -66,7 +66,7 @@ export const ProductProvider = ({ baseUrl, children }: ProductProviderProps) =>
|
|
|
66
66
|
}), [baseUrl, token]);
|
|
67
67
|
|
|
68
68
|
const fetchProducts = useCallback(
|
|
69
|
-
(
|
|
69
|
+
(search: string = "", size: number = 0, page: number = 0) => fetchProductsRequest(apiConfig, search, size, page),
|
|
70
70
|
[apiConfig]
|
|
71
71
|
);
|
|
72
72
|
const fetchProduct = useCallback((id: number) => fetchProductRequest(apiConfig, id), [apiConfig]);
|
|
@@ -23,7 +23,7 @@ type StorageContextValue = {
|
|
|
23
23
|
isLoading: boolean;
|
|
24
24
|
isError: boolean;
|
|
25
25
|
error: Error | null;
|
|
26
|
-
fetchStorages: () => Promise<Storage[]>;
|
|
26
|
+
fetchStorages: (search?: string, size?: number, page?: number) => Promise<Storage[]>;
|
|
27
27
|
fetchStorage: (id: number) => Promise<Storage | null>;
|
|
28
28
|
createStorage: (dto: CreateStorageRequest) => Promise<Storage>;
|
|
29
29
|
changeStorageName: (id: number, dto: ChangeStorageNameRequest) => Promise<Storage>;
|
|
@@ -55,7 +55,7 @@ export const StorageProvider = ({ baseUrl, children }: StorageProviderProps) =>
|
|
|
55
55
|
setError
|
|
56
56
|
}), [baseUrl, token]);
|
|
57
57
|
|
|
58
|
-
const fetchStorages = useCallback(() => fetchStoragesRequest(apiConfig), [apiConfig]);
|
|
58
|
+
const fetchStorages = useCallback((search: string = "", size: number = 0, page: number = 0) => fetchStoragesRequest(apiConfig, search, page, size), [apiConfig]);
|
|
59
59
|
const fetchStorage = useCallback((id: number) => fetchStorageRequest(apiConfig, id), [apiConfig]);
|
|
60
60
|
const createStorage = useCallback(
|
|
61
61
|
(dto: CreateStorageRequest) => createStorageRequest(apiConfig, dto),
|
|
@@ -4,12 +4,6 @@ import type { CreateProductRequest, UpdateProductRequest } from '../../type/requ
|
|
|
4
4
|
import { buildAuthHeaders, normalizeBaseUrl, readArrayBuffer, readJson } from '../http.js';
|
|
5
5
|
import { runWithRequestState, type RequestStateHandlers } from './requestState.js';
|
|
6
6
|
|
|
7
|
-
type ProductQuery = {
|
|
8
|
-
barcode?: string;
|
|
9
|
-
name?: string;
|
|
10
|
-
category?: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
7
|
type ProductApiConfig = RequestStateHandlers & {
|
|
14
8
|
baseUrl: string;
|
|
15
9
|
token: string | null;
|
|
@@ -30,23 +24,14 @@ const updateById = (items: Product[], updated: Product): Product[] => {
|
|
|
30
24
|
|
|
31
25
|
export const fetchProductsRequest = async (
|
|
32
26
|
config: ProductApiConfig,
|
|
33
|
-
|
|
27
|
+
search: string,
|
|
28
|
+
size: number,
|
|
29
|
+
page: number
|
|
34
30
|
): Promise<Product[]> => runWithRequestState(config, async () => {
|
|
35
31
|
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
if (query?.name) {
|
|
41
|
-
params.set('name', query.name);
|
|
42
|
-
}
|
|
43
|
-
if (query?.category) {
|
|
44
|
-
params.set('category', query.category);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const url = params.toString()
|
|
48
|
-
? `${normalizedBaseUrl}/api/products?${params.toString()}`
|
|
49
|
-
: `${normalizedBaseUrl}/api/products`;
|
|
32
|
+
let url = `${normalizedBaseUrl}/api/products?search=${search}`;
|
|
33
|
+
if (size > 0)
|
|
34
|
+
url += `&page=${page}&size=${size}`;
|
|
50
35
|
|
|
51
36
|
const response = await fetch(url, {
|
|
52
37
|
headers: buildAuthHeaders(config.token)
|
|
@@ -22,11 +22,20 @@ const updateById = (items: Storage[], updated: Storage): Storage[] => {
|
|
|
22
22
|
return next;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
-
export const fetchStoragesRequest = async (
|
|
25
|
+
export const fetchStoragesRequest = async (
|
|
26
|
+
config: StorageApiConfig,
|
|
27
|
+
search: string,
|
|
28
|
+
size: number,
|
|
29
|
+
page: number
|
|
30
|
+
): Promise<Storage[]> => runWithRequestState(
|
|
26
31
|
config,
|
|
27
32
|
async () => {
|
|
28
33
|
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
29
|
-
|
|
34
|
+
let url = `${normalizedBaseUrl}/api/storages?search=${search}`;
|
|
35
|
+
if (size > 0)
|
|
36
|
+
url += `&page=${page}&size=${size}`;
|
|
37
|
+
|
|
38
|
+
const response = await fetch(url, {
|
|
30
39
|
headers: buildAuthHeaders(config.token)
|
|
31
40
|
});
|
|
32
41
|
|
|
@@ -123,7 +132,7 @@ export const changeStorageNameRequest = async (
|
|
|
123
132
|
const payload = await readJson<CreateStorageError>(response);
|
|
124
133
|
if (payload?.name)
|
|
125
134
|
throw payload;
|
|
126
|
-
|
|
135
|
+
|
|
127
136
|
throw new Error('Failed to update storage');
|
|
128
137
|
}
|
|
129
138
|
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export * from './type/auth.js';
|
|
2
2
|
export * from './type/base.js';
|
|
3
|
+
export * from './type/item.js';
|
|
4
|
+
export * from './type/member.js';
|
|
3
5
|
export * from './type/models.js';
|
|
4
6
|
export * from './type/product.js';
|
|
5
|
-
export * from './type/runninglow.js';
|
|
6
7
|
export * from './type/requests.js';
|
|
8
|
+
export * from './type/runninglow.js';
|
|
9
|
+
export * from './type/shoppingList.js';
|
|
7
10
|
export * from './type/storage.js';
|
|
8
11
|
export * from './type/user.js';
|
|
9
12
|
export * from './context/AuthContext.js';
|
package/src/type/requests.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type CreateProductRequest = {
|
|
2
2
|
name: string;
|
|
3
3
|
category: string;
|
|
4
|
+
description?: string;
|
|
4
5
|
barcode?: string;
|
|
5
6
|
expirationDaysDelta: number;
|
|
6
7
|
};
|
|
@@ -8,6 +9,7 @@ export type CreateProductRequest = {
|
|
|
8
9
|
export type UpdateProductRequest = {
|
|
9
10
|
name?: string;
|
|
10
11
|
category?: string;
|
|
12
|
+
description?: string;
|
|
11
13
|
barcode?: string;
|
|
12
14
|
expirationDaysDelta?: number;
|
|
13
15
|
};
|