shelflife-react-hooks 1.0.7 → 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 +186 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.cts +71 -43
- package/dist/index.d.ts +71 -43
- package/dist/index.esm.js +182 -1
- 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);
|
|
@@ -146,7 +150,7 @@ var changePasswordRequest = async (config, dto) => runWithRequestState(config, a
|
|
|
146
150
|
});
|
|
147
151
|
if (!response.ok) {
|
|
148
152
|
const error = await readJson(response);
|
|
149
|
-
if (error?.oldPassword || error?.newPasswordRepeat)
|
|
153
|
+
if (error?.oldPassword || error?.newPassword || error?.newPasswordRepeat)
|
|
150
154
|
throw error;
|
|
151
155
|
throw new Error("Change password failed");
|
|
152
156
|
}
|
|
@@ -245,6 +249,9 @@ var updateUserRequest = async (config, id, dto) => runWithRequestState(config, a
|
|
|
245
249
|
body: JSON.stringify(dto)
|
|
246
250
|
});
|
|
247
251
|
if (!response.ok) {
|
|
252
|
+
const update = await readJson(response);
|
|
253
|
+
if (update?.email || update?.username)
|
|
254
|
+
throw update;
|
|
248
255
|
throw new Error("Failed to update user");
|
|
249
256
|
}
|
|
250
257
|
const payload = await readJson(response);
|
|
@@ -549,6 +556,9 @@ var createProductRequest = async (config, dto) => runWithRequestState(config, as
|
|
|
549
556
|
body: JSON.stringify(dto)
|
|
550
557
|
});
|
|
551
558
|
if (!response.ok) {
|
|
559
|
+
const err = await readJson(response);
|
|
560
|
+
if (err?.name || err?.barcode || err?.category || err?.expirationDaysDelta)
|
|
561
|
+
throw err;
|
|
552
562
|
throw new Error("Failed to create product");
|
|
553
563
|
}
|
|
554
564
|
const payload = await readJson(response);
|
|
@@ -570,6 +580,9 @@ var updateProductRequest = async (config, id, dto) => runWithRequestState(config
|
|
|
570
580
|
body: JSON.stringify(dto)
|
|
571
581
|
});
|
|
572
582
|
if (!response.ok) {
|
|
583
|
+
const err = await readJson(response);
|
|
584
|
+
if (err?.name || err?.barcode || err?.category || err?.expirationDaysDelta)
|
|
585
|
+
throw err;
|
|
573
586
|
throw new Error("Failed to update product");
|
|
574
587
|
}
|
|
575
588
|
const payload = await readJson(response);
|
|
@@ -736,6 +749,9 @@ var createSettingRequest = async (config, storageId, dto) => runWithRequestState
|
|
|
736
749
|
}
|
|
737
750
|
);
|
|
738
751
|
if (!response.ok) {
|
|
752
|
+
const err = await readJson(response);
|
|
753
|
+
if (err?.productId || err?.runningLow)
|
|
754
|
+
throw err;
|
|
739
755
|
throw new Error("Failed to create running low setting");
|
|
740
756
|
}
|
|
741
757
|
const payload = await readJson(response);
|
|
@@ -759,6 +775,9 @@ var editSettingRequest = async (config, storageId, id, dto) => runWithRequestSta
|
|
|
759
775
|
}
|
|
760
776
|
);
|
|
761
777
|
if (!response.ok) {
|
|
778
|
+
const err = await readJson(response);
|
|
779
|
+
if (err?.runningLow)
|
|
780
|
+
throw err;
|
|
762
781
|
throw new Error("Failed to edit running low setting");
|
|
763
782
|
}
|
|
764
783
|
const payload = await readJson(response);
|
|
@@ -907,6 +926,9 @@ var createStorageRequest = async (config, dto) => runWithRequestState(config, as
|
|
|
907
926
|
body: JSON.stringify(dto)
|
|
908
927
|
});
|
|
909
928
|
if (!response.ok) {
|
|
929
|
+
const payload2 = await readJson(response);
|
|
930
|
+
if (payload2?.name)
|
|
931
|
+
throw payload2;
|
|
910
932
|
throw new Error("Failed to create storage");
|
|
911
933
|
}
|
|
912
934
|
const payload = await readJson(response);
|
|
@@ -928,6 +950,9 @@ var changeStorageNameRequest = async (config, id, dto) => runWithRequestState(co
|
|
|
928
950
|
body: JSON.stringify(dto)
|
|
929
951
|
});
|
|
930
952
|
if (!response.ok) {
|
|
953
|
+
const payload2 = await readJson(response);
|
|
954
|
+
if (payload2?.name)
|
|
955
|
+
throw payload2;
|
|
931
956
|
throw new Error("Failed to update storage");
|
|
932
957
|
}
|
|
933
958
|
const payload = await readJson(response);
|
|
@@ -1053,6 +1078,9 @@ var addItemRequest = async (config, storageId, dto) => runWithRequestState(confi
|
|
|
1053
1078
|
body: JSON.stringify(dto)
|
|
1054
1079
|
});
|
|
1055
1080
|
if (!response.ok) {
|
|
1081
|
+
const err = await readJson(response);
|
|
1082
|
+
if (err?.productId || err?.expiresAt)
|
|
1083
|
+
throw err;
|
|
1056
1084
|
throw new Error("Failed to add storage item");
|
|
1057
1085
|
}
|
|
1058
1086
|
const payload = await readJson(response);
|
|
@@ -1073,6 +1101,9 @@ var editItemRequest = async (config, storageId, itemId, dto) => runWithRequestSt
|
|
|
1073
1101
|
body: JSON.stringify(dto)
|
|
1074
1102
|
});
|
|
1075
1103
|
if (!response.ok) {
|
|
1104
|
+
const err = await readJson(response);
|
|
1105
|
+
if (err?.expiresAt)
|
|
1106
|
+
throw err;
|
|
1076
1107
|
throw new Error("Failed to edit storage item");
|
|
1077
1108
|
}
|
|
1078
1109
|
const payload = await readJson(response);
|
|
@@ -1258,6 +1289,9 @@ var inviteMemberRequest = async (config, storageId, dto) => runWithRequestState(
|
|
|
1258
1289
|
body: JSON.stringify(dto)
|
|
1259
1290
|
});
|
|
1260
1291
|
if (!response.ok) {
|
|
1292
|
+
const err = await readJson(response);
|
|
1293
|
+
if (err?.email)
|
|
1294
|
+
throw err;
|
|
1261
1295
|
throw new Error("Failed to invite member");
|
|
1262
1296
|
}
|
|
1263
1297
|
const payload = await readJson(response);
|
|
@@ -1393,15 +1427,164 @@ var useUsers = () => {
|
|
|
1393
1427
|
}
|
|
1394
1428
|
return context;
|
|
1395
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
|
+
};
|
|
1396
1577
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1397
1578
|
0 && (module.exports = {
|
|
1398
1579
|
AuthProvider,
|
|
1399
1580
|
InviteProvider,
|
|
1400
1581
|
ProductProvider,
|
|
1401
1582
|
RunningLowProvider,
|
|
1583
|
+
ShoppingListProvider,
|
|
1402
1584
|
StorageItemProvider,
|
|
1403
1585
|
StorageMemberProvider,
|
|
1404
1586
|
StorageProvider,
|
|
1587
|
+
ToPurchaseProvider,
|
|
1405
1588
|
UserProvider,
|
|
1406
1589
|
buildAuthHeaders,
|
|
1407
1590
|
normalizeBaseUrl,
|
|
@@ -1411,9 +1594,11 @@ var useUsers = () => {
|
|
|
1411
1594
|
useInvites,
|
|
1412
1595
|
useProducts,
|
|
1413
1596
|
useRunningLow,
|
|
1597
|
+
useShoppingList,
|
|
1414
1598
|
useStorageItems,
|
|
1415
1599
|
useStorageMembers,
|
|
1416
1600
|
useStorages,
|
|
1601
|
+
useToPurchase,
|
|
1417
1602
|
useUsers
|
|
1418
1603
|
});
|
|
1419
1604
|
//# sourceMappingURL=index.cjs.js.map
|