shelflife-react-hooks 1.0.8 → 1.0.10
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/authApi.ts +0 -1
- package/src/context/api/shoppingListApi.ts +122 -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
- package/src/type/shoppingList.ts +14 -0
package/dist/index.d.ts
CHANGED
|
@@ -54,6 +54,17 @@ type RunningLowNotification = {
|
|
|
54
54
|
runningLowAt: number;
|
|
55
55
|
amount: number;
|
|
56
56
|
};
|
|
57
|
+
type ShoppingListItem = {
|
|
58
|
+
id: number;
|
|
59
|
+
storage: Storage;
|
|
60
|
+
product: Product;
|
|
61
|
+
amountToBuy: number;
|
|
62
|
+
};
|
|
63
|
+
type ToPurchaseItem = {
|
|
64
|
+
productId: number;
|
|
65
|
+
productName: string;
|
|
66
|
+
amountToBuy: number;
|
|
67
|
+
};
|
|
57
68
|
|
|
58
69
|
type LoginRequest = {
|
|
59
70
|
email: string;
|
|
@@ -143,6 +154,13 @@ type AddItemRequest = {
|
|
|
143
154
|
type EditItemRequest = {
|
|
144
155
|
expiresAt?: string;
|
|
145
156
|
};
|
|
157
|
+
type CreateShoppingItemRequest = {
|
|
158
|
+
productId: number;
|
|
159
|
+
amountToBuy: number;
|
|
160
|
+
};
|
|
161
|
+
type EditShoppingItemRequest = {
|
|
162
|
+
amountToBuy: number;
|
|
163
|
+
};
|
|
146
164
|
type CreateStorageRequest = {
|
|
147
165
|
name: string;
|
|
148
166
|
};
|
|
@@ -333,4 +351,35 @@ type UserProviderProps = {
|
|
|
333
351
|
declare const UserProvider: ({ baseUrl, children }: UserProviderProps) => react_jsx_runtime.JSX.Element;
|
|
334
352
|
declare const useUsers: () => UserContextValue;
|
|
335
353
|
|
|
336
|
-
|
|
354
|
+
type ShoppingListContextValue = {
|
|
355
|
+
items: ShoppingListItem[];
|
|
356
|
+
isLoading: boolean;
|
|
357
|
+
isError: boolean;
|
|
358
|
+
error: Error | null;
|
|
359
|
+
fetchItems: (storageId: number) => Promise<ShoppingListItem[]>;
|
|
360
|
+
createItem: (storageId: number, dto: CreateShoppingItemRequest) => Promise<ShoppingListItem>;
|
|
361
|
+
editItem: (storageId: number, itemId: number, dto: EditShoppingItemRequest) => Promise<ShoppingListItem>;
|
|
362
|
+
deleteItem: (storageId: number, itemId: number) => Promise<void>;
|
|
363
|
+
};
|
|
364
|
+
type ShoppingListProviderProps = {
|
|
365
|
+
baseUrl: string;
|
|
366
|
+
children: React.ReactNode;
|
|
367
|
+
};
|
|
368
|
+
declare const ShoppingListProvider: ({ baseUrl, children }: ShoppingListProviderProps) => react_jsx_runtime.JSX.Element;
|
|
369
|
+
declare const useShoppingList: () => ShoppingListContextValue;
|
|
370
|
+
|
|
371
|
+
type ToPurchaseContextValue = {
|
|
372
|
+
items: ToPurchaseItem[];
|
|
373
|
+
isLoading: boolean;
|
|
374
|
+
isError: boolean;
|
|
375
|
+
error: Error | null;
|
|
376
|
+
fetchAggregated: () => Promise<ToPurchaseItem[]>;
|
|
377
|
+
};
|
|
378
|
+
type ToPurchaseProviderProps = {
|
|
379
|
+
baseUrl: string;
|
|
380
|
+
children: React.ReactNode;
|
|
381
|
+
};
|
|
382
|
+
declare const ToPurchaseProvider: ({ baseUrl, children }: ToPurchaseProviderProps) => react_jsx_runtime.JSX.Element;
|
|
383
|
+
declare const useToPurchase: () => ToPurchaseContextValue;
|
|
384
|
+
|
|
385
|
+
export { type AddItemRequest, type AuthContextValue, AuthProvider, type BaseErrorResponse, type ChangePasswordErrorResponse, type ChangePasswordRequest, type ChangePasswordResponse, type ChangeStorageNameRequest, type ChangeUserDataRequest, type CreateProductRequest, type CreateRunningLowSettingError, type CreateSettingRequest, type CreateShoppingItemRequest, type CreateStorageError, type CreateStorageRequest, type EditItemRequest, type EditRunningLowSettingError, type EditSettingRequest, type EditShoppingItemRequest, type EmptyResponse, type ErrorResponse, type FieldErrorMap, type GetMeResponse, type ImageResponse, type InviteMemberRequest, InviteProvider, type LoginErrorResponse, type LoginRequest, type LoginResponse, type LoginSuccessResponse, type LogoutErrorResponse, type LogoutResponse, type Product, type ProductCreateError, ProductProvider, type RunningLowNotification, RunningLowProvider, type RunningLowSetting, type ShoppingListItem, ShoppingListProvider, type SignupError, type SignupRequest, type SignupResponse, type Storage, type StorageItem, StorageItemProvider, type StorageMember, StorageMemberProvider, StorageProvider, type ToPurchaseItem, ToPurchaseProvider, type TokenStorage, type UpdateProductRequest, type UpdateUserError, type User, UserProvider, buildAuthHeaders, normalizeBaseUrl, readArrayBuffer, readJson, useAuth, useInvites, useProducts, useRunningLow, useShoppingList, useStorageItems, useStorageMembers, useStorages, useToPurchase, useUsers };
|
package/dist/index.esm.js
CHANGED
|
@@ -1427,14 +1427,163 @@ var useUsers = () => {
|
|
|
1427
1427
|
}
|
|
1428
1428
|
return context;
|
|
1429
1429
|
};
|
|
1430
|
+
|
|
1431
|
+
// src/context/ShoppingListContext.tsx
|
|
1432
|
+
import { createContext as createContext9, useCallback as useCallback9, useContext as useContext9, useMemo as useMemo9, useState as useState9 } from "react";
|
|
1433
|
+
|
|
1434
|
+
// src/context/api/shoppingListApi.ts
|
|
1435
|
+
var updateById6 = (items, updated) => {
|
|
1436
|
+
const index = items.findIndex((i) => i.id === updated.id);
|
|
1437
|
+
if (index === -1) return [updated, ...items];
|
|
1438
|
+
const next = [...items];
|
|
1439
|
+
next[index] = updated;
|
|
1440
|
+
return next;
|
|
1441
|
+
};
|
|
1442
|
+
var fetchShoppingListRequest = async (config, storageId) => runWithRequestState(config, async () => {
|
|
1443
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1444
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist`, {
|
|
1445
|
+
headers: buildAuthHeaders(config.token)
|
|
1446
|
+
});
|
|
1447
|
+
if (!response.ok) {
|
|
1448
|
+
throw new Error("Failed to fetch shopping list items");
|
|
1449
|
+
}
|
|
1450
|
+
const payload = await readJson(response);
|
|
1451
|
+
if (payload) {
|
|
1452
|
+
config.setItems(payload);
|
|
1453
|
+
return payload;
|
|
1454
|
+
}
|
|
1455
|
+
return [];
|
|
1456
|
+
});
|
|
1457
|
+
var createShoppingItemRequest = async (config, storageId, dto) => runWithRequestState(config, async () => {
|
|
1458
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1459
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist`, {
|
|
1460
|
+
method: "POST",
|
|
1461
|
+
headers: {
|
|
1462
|
+
...buildAuthHeaders(config.token),
|
|
1463
|
+
"Content-Type": "application/json"
|
|
1464
|
+
},
|
|
1465
|
+
body: JSON.stringify(dto)
|
|
1466
|
+
});
|
|
1467
|
+
if (!response.ok) {
|
|
1468
|
+
throw new Error("Failed to create shopping list item");
|
|
1469
|
+
}
|
|
1470
|
+
const payload = await readJson(response);
|
|
1471
|
+
if (!payload) throw new Error("Create shopping item response missing data");
|
|
1472
|
+
config.setItems((previous) => [payload, ...previous]);
|
|
1473
|
+
return payload;
|
|
1474
|
+
});
|
|
1475
|
+
var editShoppingItemRequest = async (config, storageId, itemId, dto) => runWithRequestState(config, async () => {
|
|
1476
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1477
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist/${itemId}`, {
|
|
1478
|
+
method: "PUT",
|
|
1479
|
+
headers: {
|
|
1480
|
+
...buildAuthHeaders(config.token),
|
|
1481
|
+
"Content-Type": "application/json"
|
|
1482
|
+
},
|
|
1483
|
+
body: JSON.stringify(dto)
|
|
1484
|
+
});
|
|
1485
|
+
if (!response.ok) {
|
|
1486
|
+
throw new Error("Failed to edit shopping list item");
|
|
1487
|
+
}
|
|
1488
|
+
const payload = await readJson(response);
|
|
1489
|
+
if (!payload) throw new Error("Edit shopping item response missing data");
|
|
1490
|
+
config.setItems((previous) => updateById6(previous, payload));
|
|
1491
|
+
return payload;
|
|
1492
|
+
});
|
|
1493
|
+
var deleteShoppingItemRequest = async (config, storageId, itemId) => runWithRequestState(config, async () => {
|
|
1494
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1495
|
+
const response = await fetch(`${normalizedBaseUrl}/api/storages/${storageId}/shoppinglist/${itemId}`, {
|
|
1496
|
+
method: "DELETE",
|
|
1497
|
+
headers: buildAuthHeaders(config.token)
|
|
1498
|
+
});
|
|
1499
|
+
if (!response.ok) {
|
|
1500
|
+
throw new Error("Failed to delete shopping list item");
|
|
1501
|
+
}
|
|
1502
|
+
config.setItems((previous) => previous.filter((i) => i.id !== itemId));
|
|
1503
|
+
});
|
|
1504
|
+
|
|
1505
|
+
// src/context/ShoppingListContext.tsx
|
|
1506
|
+
import { jsx as jsx9 } from "react/jsx-runtime";
|
|
1507
|
+
var ShoppingListContext = createContext9(void 0);
|
|
1508
|
+
var ShoppingListProvider = ({ baseUrl, children }) => {
|
|
1509
|
+
const { token } = useAuth();
|
|
1510
|
+
const [items, setItems] = useState9([]);
|
|
1511
|
+
const [isLoading, setIsLoading] = useState9(false);
|
|
1512
|
+
const [isError, setIsError] = useState9(false);
|
|
1513
|
+
const [error, setError] = useState9(null);
|
|
1514
|
+
const apiConfig = { baseUrl, token, setItems, setIsLoading, setIsError, setError };
|
|
1515
|
+
const fetchItems = useCallback9(async (storageId) => {
|
|
1516
|
+
return fetchShoppingListRequest(apiConfig, storageId);
|
|
1517
|
+
}, [apiConfig]);
|
|
1518
|
+
const createItem = useCallback9(async (storageId, dto) => {
|
|
1519
|
+
return createShoppingItemRequest(apiConfig, storageId, dto);
|
|
1520
|
+
}, [apiConfig]);
|
|
1521
|
+
const editItem = useCallback9(async (storageId, itemId, dto) => {
|
|
1522
|
+
return editShoppingItemRequest(apiConfig, storageId, itemId, dto);
|
|
1523
|
+
}, [apiConfig]);
|
|
1524
|
+
const deleteItem = useCallback9(async (storageId, itemId) => {
|
|
1525
|
+
return deleteShoppingItemRequest(apiConfig, storageId, itemId);
|
|
1526
|
+
}, [apiConfig]);
|
|
1527
|
+
const value = useMemo9(() => ({ items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem }), [items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem]);
|
|
1528
|
+
return /* @__PURE__ */ jsx9(ShoppingListContext.Provider, { value, children });
|
|
1529
|
+
};
|
|
1530
|
+
var useShoppingList = () => {
|
|
1531
|
+
const context = useContext9(ShoppingListContext);
|
|
1532
|
+
if (!context) throw new Error("useShoppingList must be used within a ShoppingListProvider");
|
|
1533
|
+
return context;
|
|
1534
|
+
};
|
|
1535
|
+
|
|
1536
|
+
// src/context/ToPurchaseContext.tsx
|
|
1537
|
+
import { createContext as createContext10, useCallback as useCallback10, useContext as useContext10, useMemo as useMemo10, useState as useState10 } from "react";
|
|
1538
|
+
|
|
1539
|
+
// src/context/api/toPurchaseApi.ts
|
|
1540
|
+
var fetchAggregatedShoppingRequest = async (config) => runWithRequestState(config, async () => {
|
|
1541
|
+
const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
|
|
1542
|
+
const response = await fetch(`${normalizedBaseUrl}/api/tobuy/shopping`, {
|
|
1543
|
+
headers: buildAuthHeaders(config.token)
|
|
1544
|
+
});
|
|
1545
|
+
if (!response.ok) {
|
|
1546
|
+
throw new Error("Failed to fetch aggregated shopping list");
|
|
1547
|
+
}
|
|
1548
|
+
const payload = await readJson(response);
|
|
1549
|
+
if (payload) {
|
|
1550
|
+
config.setToPurchase(payload);
|
|
1551
|
+
return payload;
|
|
1552
|
+
}
|
|
1553
|
+
return [];
|
|
1554
|
+
});
|
|
1555
|
+
|
|
1556
|
+
// src/context/ToPurchaseContext.tsx
|
|
1557
|
+
import { jsx as jsx10 } from "react/jsx-runtime";
|
|
1558
|
+
var ToPurchaseContext = createContext10(void 0);
|
|
1559
|
+
var ToPurchaseProvider = ({ baseUrl, children }) => {
|
|
1560
|
+
const { token } = useAuth();
|
|
1561
|
+
const [items, setItems] = useState10([]);
|
|
1562
|
+
const [isLoading, setIsLoading] = useState10(false);
|
|
1563
|
+
const [isError, setIsError] = useState10(false);
|
|
1564
|
+
const [error, setError] = useState10(null);
|
|
1565
|
+
const apiConfig = { baseUrl, token, setToPurchase: setItems, setIsLoading, setIsError, setError };
|
|
1566
|
+
const fetchAggregated = useCallback10(async () => {
|
|
1567
|
+
return fetchAggregatedShoppingRequest(apiConfig);
|
|
1568
|
+
}, [apiConfig]);
|
|
1569
|
+
const value = useMemo10(() => ({ items, isLoading, isError, error, fetchAggregated }), [items, isLoading, isError, error, fetchAggregated]);
|
|
1570
|
+
return /* @__PURE__ */ jsx10(ToPurchaseContext.Provider, { value, children });
|
|
1571
|
+
};
|
|
1572
|
+
var useToPurchase = () => {
|
|
1573
|
+
const context = useContext10(ToPurchaseContext);
|
|
1574
|
+
if (!context) throw new Error("useToPurchase must be used within a ToPurchaseProvider");
|
|
1575
|
+
return context;
|
|
1576
|
+
};
|
|
1430
1577
|
export {
|
|
1431
1578
|
AuthProvider,
|
|
1432
1579
|
InviteProvider,
|
|
1433
1580
|
ProductProvider,
|
|
1434
1581
|
RunningLowProvider,
|
|
1582
|
+
ShoppingListProvider,
|
|
1435
1583
|
StorageItemProvider,
|
|
1436
1584
|
StorageMemberProvider,
|
|
1437
1585
|
StorageProvider,
|
|
1586
|
+
ToPurchaseProvider,
|
|
1438
1587
|
UserProvider,
|
|
1439
1588
|
buildAuthHeaders,
|
|
1440
1589
|
normalizeBaseUrl,
|
|
@@ -1444,9 +1593,11 @@ export {
|
|
|
1444
1593
|
useInvites,
|
|
1445
1594
|
useProducts,
|
|
1446
1595
|
useRunningLow,
|
|
1596
|
+
useShoppingList,
|
|
1447
1597
|
useStorageItems,
|
|
1448
1598
|
useStorageMembers,
|
|
1449
1599
|
useStorages,
|
|
1600
|
+
useToPurchase,
|
|
1450
1601
|
useUsers
|
|
1451
1602
|
};
|
|
1452
1603
|
//# sourceMappingURL=index.esm.js.map
|