shelflife-react-hooks 1.0.18 → 1.0.20

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.
Files changed (44) hide show
  1. package/dist/index.cjs.js +23 -0
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.cts +2 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.esm.js +23 -0
  6. package/dist/index.esm.js.map +1 -1
  7. package/package.json +36 -36
  8. package/src/context/AuthContext.tsx +161 -161
  9. package/src/context/InviteContext.tsx +74 -74
  10. package/src/context/ProductContext.tsx +131 -121
  11. package/src/context/RunningLowContext.tsx +100 -100
  12. package/src/context/ShoppingListContext.tsx +76 -76
  13. package/src/context/StorageContext.tsx +105 -105
  14. package/src/context/StorageItemContext.tsx +157 -157
  15. package/src/context/StorageMemberContext.tsx +84 -84
  16. package/src/context/UserContext.tsx +109 -109
  17. package/src/context/__tests__/contexts.test.tsx +370 -370
  18. package/src/context/api/authApi.ts +155 -155
  19. package/src/context/api/inviteApi.ts +65 -65
  20. package/src/context/api/productApi.ts +223 -201
  21. package/src/context/api/requestState.ts +24 -24
  22. package/src/context/api/runningLowApi.ts +141 -141
  23. package/src/context/api/shoppingListApi.ts +161 -159
  24. package/src/context/api/storageApi.ts +166 -166
  25. package/src/context/api/storageItemApi.ts +260 -260
  26. package/src/context/api/storageMemberApi.ts +84 -84
  27. package/src/context/api/userApi.ts +161 -161
  28. package/src/context/http.ts +22 -22
  29. package/src/index.ts +21 -21
  30. package/src/type/PaginatedResponse.ts +8 -8
  31. package/src/type/auth.ts +79 -79
  32. package/src/type/base.ts +21 -21
  33. package/src/type/item.ts +12 -12
  34. package/src/type/member.ts +6 -6
  35. package/src/type/models.ts +56 -56
  36. package/src/type/product.ts +11 -11
  37. package/src/type/requests.ts +60 -60
  38. package/src/type/runninglow.ts +13 -13
  39. package/src/type/shoppingList.ts +13 -13
  40. package/src/type/storage.ts +7 -7
  41. package/src/type/user.ts +11 -11
  42. package/tsconfig.json +46 -46
  43. package/tsup.config.ts +10 -10
  44. package/vitest.config.ts +8 -8
package/dist/index.d.cts CHANGED
@@ -264,9 +264,11 @@ declare const useInvites: () => InviteContextValue;
264
264
  type ProductContextValue = {
265
265
  products: Product[];
266
266
  product: Product | null;
267
+ categories: string[];
267
268
  isLoading: boolean;
268
269
  isError: boolean;
269
270
  error: Error | null;
271
+ fetchCategories: () => Promise<string[] | null>;
270
272
  fetchProducts: (search?: string, size?: number, page?: number) => Promise<PaginatedResponse<Product>>;
271
273
  fetchProduct: (id: number) => Promise<Product | null>;
272
274
  createProduct: (dto: CreateProductRequest) => Promise<Product>;
package/dist/index.d.ts CHANGED
@@ -264,9 +264,11 @@ declare const useInvites: () => InviteContextValue;
264
264
  type ProductContextValue = {
265
265
  products: Product[];
266
266
  product: Product | null;
267
+ categories: string[];
267
268
  isLoading: boolean;
268
269
  isError: boolean;
269
270
  error: Error | null;
271
+ fetchCategories: () => Promise<string[] | null>;
270
272
  fetchProducts: (search?: string, size?: number, page?: number) => Promise<PaginatedResponse<Product>>;
271
273
  fetchProduct: (id: number) => Promise<Product | null>;
272
274
  createProduct: (dto: CreateProductRequest) => Promise<Product>;
package/dist/index.esm.js CHANGED
@@ -506,6 +506,21 @@ var fetchProductRequest = async (config, id) => runWithRequestState(config, asyn
506
506
  config.setProducts((previous) => updateById2(previous, payload));
507
507
  return payload;
508
508
  });
509
+ var fetchCategoriesRequest = async (config) => runWithRequestState(config, async () => {
510
+ const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
511
+ const response = await fetch(`${normalizedBaseUrl}/api/products/categories`, {
512
+ headers: buildAuthHeaders(config.token)
513
+ });
514
+ if (!response.ok) {
515
+ throw new Error("Failed to fetch categories");
516
+ }
517
+ const payload = await readJson(response);
518
+ if (!payload) {
519
+ throw new Error("Category response missing data");
520
+ }
521
+ config.setCategories(payload);
522
+ return payload;
523
+ });
509
524
  var createProductRequest = async (config, dto) => runWithRequestState(config, async () => {
510
525
  const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);
511
526
  const response = await fetch(`${normalizedBaseUrl}/api/products`, {
@@ -595,6 +610,7 @@ import { jsx as jsx3 } from "react/jsx-runtime";
595
610
  var ProductContext = createContext3(void 0);
596
611
  var ProductProvider = ({ baseUrl, children }) => {
597
612
  const { token } = useAuth();
613
+ const [categories, setCategories] = useState3([]);
598
614
  const [products, setProducts] = useState3([]);
599
615
  const [product, setProduct] = useState3(null);
600
616
  const [isLoading, setIsLoading] = useState3(false);
@@ -603,6 +619,7 @@ var ProductProvider = ({ baseUrl, children }) => {
603
619
  const apiConfig = useMemo3(() => ({
604
620
  baseUrl,
605
621
  token,
622
+ setCategories,
606
623
  setProducts,
607
624
  setProduct,
608
625
  setIsLoading,
@@ -613,6 +630,7 @@ var ProductProvider = ({ baseUrl, children }) => {
613
630
  (search = "", size = 0, page = 0) => fetchProductsRequest(apiConfig, search, size, page),
614
631
  [apiConfig]
615
632
  );
633
+ const fetchCategories = useCallback3(() => fetchCategoriesRequest(apiConfig), [apiConfig]);
616
634
  const fetchProduct = useCallback3((id) => fetchProductRequest(apiConfig, id), [apiConfig]);
617
635
  const createProduct = useCallback3(
618
636
  (dto) => createProductRequest(apiConfig, dto),
@@ -634,6 +652,8 @@ var ProductProvider = ({ baseUrl, children }) => {
634
652
  isLoading,
635
653
  isError,
636
654
  error,
655
+ categories,
656
+ fetchCategories,
637
657
  fetchProducts,
638
658
  fetchProduct,
639
659
  createProduct,
@@ -647,11 +667,13 @@ var ProductProvider = ({ baseUrl, children }) => {
647
667
  error,
648
668
  fetchProduct,
649
669
  fetchProducts,
670
+ fetchCategories,
650
671
  getProductIcon,
651
672
  isError,
652
673
  isLoading,
653
674
  product,
654
675
  products,
676
+ categories,
655
677
  updateProduct,
656
678
  uploadProductIcon
657
679
  ]);
@@ -1532,6 +1554,7 @@ var createStorageItemsWithShoppingListItemRequest = async (config, storageId, it
1532
1554
  if (!response.ok) {
1533
1555
  throw new Error("Failed to add items");
1534
1556
  }
1557
+ config.setItems((previous) => previous.filter((i) => i.id !== itemId));
1535
1558
  });
1536
1559
  var createShoppingItemRequest = async (config, storageId, dto) => runWithRequestState(config, async () => {
1537
1560
  const normalizedBaseUrl = normalizeBaseUrl(config.baseUrl);