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
package/dist/index.cjs.js
CHANGED
|
@@ -24,9 +24,11 @@ __export(index_exports, {
|
|
|
24
24
|
InviteProvider: () => InviteProvider,
|
|
25
25
|
ProductProvider: () => ProductProvider,
|
|
26
26
|
RunningLowProvider: () => RunningLowProvider,
|
|
27
|
+
ShoppingListProvider: () => ShoppingListProvider,
|
|
27
28
|
StorageItemProvider: () => StorageItemProvider,
|
|
28
29
|
StorageMemberProvider: () => StorageMemberProvider,
|
|
29
30
|
StorageProvider: () => StorageProvider,
|
|
31
|
+
ToPurchaseProvider: () => ToPurchaseProvider,
|
|
30
32
|
UserProvider: () => UserProvider,
|
|
31
33
|
buildAuthHeaders: () => buildAuthHeaders,
|
|
32
34
|
normalizeBaseUrl: () => normalizeBaseUrl,
|
|
@@ -36,9 +38,11 @@ __export(index_exports, {
|
|
|
36
38
|
useInvites: () => useInvites,
|
|
37
39
|
useProducts: () => useProducts,
|
|
38
40
|
useRunningLow: () => useRunningLow,
|
|
41
|
+
useShoppingList: () => useShoppingList,
|
|
39
42
|
useStorageItems: () => useStorageItems,
|
|
40
43
|
useStorageMembers: () => useStorageMembers,
|
|
41
44
|
useStorages: () => useStorages,
|
|
45
|
+
useToPurchase: () => useToPurchase,
|
|
42
46
|
useUsers: () => useUsers
|
|
43
47
|
});
|
|
44
48
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -1423,15 +1427,164 @@ var useUsers = () => {
|
|
|
1423
1427
|
}
|
|
1424
1428
|
return context;
|
|
1425
1429
|
};
|
|
1430
|
+
|
|
1431
|
+
// src/context/ShoppingListContext.tsx
|
|
1432
|
+
var import_react9 = require("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
|
+
var import_jsx_runtime9 = require("react/jsx-runtime");
|
|
1507
|
+
var ShoppingListContext = (0, import_react9.createContext)(void 0);
|
|
1508
|
+
var ShoppingListProvider = ({ baseUrl, children }) => {
|
|
1509
|
+
const { token } = useAuth();
|
|
1510
|
+
const [items, setItems] = (0, import_react9.useState)([]);
|
|
1511
|
+
const [isLoading, setIsLoading] = (0, import_react9.useState)(false);
|
|
1512
|
+
const [isError, setIsError] = (0, import_react9.useState)(false);
|
|
1513
|
+
const [error, setError] = (0, import_react9.useState)(null);
|
|
1514
|
+
const apiConfig = { baseUrl, token, setItems, setIsLoading, setIsError, setError };
|
|
1515
|
+
const fetchItems = (0, import_react9.useCallback)(async (storageId) => {
|
|
1516
|
+
return fetchShoppingListRequest(apiConfig, storageId);
|
|
1517
|
+
}, [apiConfig]);
|
|
1518
|
+
const createItem = (0, import_react9.useCallback)(async (storageId, dto) => {
|
|
1519
|
+
return createShoppingItemRequest(apiConfig, storageId, dto);
|
|
1520
|
+
}, [apiConfig]);
|
|
1521
|
+
const editItem = (0, import_react9.useCallback)(async (storageId, itemId, dto) => {
|
|
1522
|
+
return editShoppingItemRequest(apiConfig, storageId, itemId, dto);
|
|
1523
|
+
}, [apiConfig]);
|
|
1524
|
+
const deleteItem = (0, import_react9.useCallback)(async (storageId, itemId) => {
|
|
1525
|
+
return deleteShoppingItemRequest(apiConfig, storageId, itemId);
|
|
1526
|
+
}, [apiConfig]);
|
|
1527
|
+
const value = (0, import_react9.useMemo)(() => ({ items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem }), [items, isLoading, isError, error, fetchItems, createItem, editItem, deleteItem]);
|
|
1528
|
+
return /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(ShoppingListContext.Provider, { value, children });
|
|
1529
|
+
};
|
|
1530
|
+
var useShoppingList = () => {
|
|
1531
|
+
const context = (0, import_react9.useContext)(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
|
+
var import_react10 = require("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
|
+
var import_jsx_runtime10 = require("react/jsx-runtime");
|
|
1558
|
+
var ToPurchaseContext = (0, import_react10.createContext)(void 0);
|
|
1559
|
+
var ToPurchaseProvider = ({ baseUrl, children }) => {
|
|
1560
|
+
const { token } = useAuth();
|
|
1561
|
+
const [items, setItems] = (0, import_react10.useState)([]);
|
|
1562
|
+
const [isLoading, setIsLoading] = (0, import_react10.useState)(false);
|
|
1563
|
+
const [isError, setIsError] = (0, import_react10.useState)(false);
|
|
1564
|
+
const [error, setError] = (0, import_react10.useState)(null);
|
|
1565
|
+
const apiConfig = { baseUrl, token, setToPurchase: setItems, setIsLoading, setIsError, setError };
|
|
1566
|
+
const fetchAggregated = (0, import_react10.useCallback)(async () => {
|
|
1567
|
+
return fetchAggregatedShoppingRequest(apiConfig);
|
|
1568
|
+
}, [apiConfig]);
|
|
1569
|
+
const value = (0, import_react10.useMemo)(() => ({ items, isLoading, isError, error, fetchAggregated }), [items, isLoading, isError, error, fetchAggregated]);
|
|
1570
|
+
return /* @__PURE__ */ (0, import_jsx_runtime10.jsx)(ToPurchaseContext.Provider, { value, children });
|
|
1571
|
+
};
|
|
1572
|
+
var useToPurchase = () => {
|
|
1573
|
+
const context = (0, import_react10.useContext)(ToPurchaseContext);
|
|
1574
|
+
if (!context) throw new Error("useToPurchase must be used within a ToPurchaseProvider");
|
|
1575
|
+
return context;
|
|
1576
|
+
};
|
|
1426
1577
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1427
1578
|
0 && (module.exports = {
|
|
1428
1579
|
AuthProvider,
|
|
1429
1580
|
InviteProvider,
|
|
1430
1581
|
ProductProvider,
|
|
1431
1582
|
RunningLowProvider,
|
|
1583
|
+
ShoppingListProvider,
|
|
1432
1584
|
StorageItemProvider,
|
|
1433
1585
|
StorageMemberProvider,
|
|
1434
1586
|
StorageProvider,
|
|
1587
|
+
ToPurchaseProvider,
|
|
1435
1588
|
UserProvider,
|
|
1436
1589
|
buildAuthHeaders,
|
|
1437
1590
|
normalizeBaseUrl,
|
|
@@ -1441,9 +1594,11 @@ var useUsers = () => {
|
|
|
1441
1594
|
useInvites,
|
|
1442
1595
|
useProducts,
|
|
1443
1596
|
useRunningLow,
|
|
1597
|
+
useShoppingList,
|
|
1444
1598
|
useStorageItems,
|
|
1445
1599
|
useStorageMembers,
|
|
1446
1600
|
useStorages,
|
|
1601
|
+
useToPurchase,
|
|
1447
1602
|
useUsers
|
|
1448
1603
|
});
|
|
1449
1604
|
//# sourceMappingURL=index.cjs.js.map
|