feat: integrate authFetch for API calls in InventoryView and PantryView, and add pantry route with authentication

This commit is contained in:
Nils-Johan Gynther
2026-04-21 14:49:20 +02:00
parent 81b63b3fdb
commit c57f4bde19
3 changed files with 22 additions and 5 deletions
@@ -4,19 +4,21 @@ import { useState, useEffect, useCallback } from 'react';
import type { InventoryItem, Product } from '../../../../features/inventory/types';
import InventoryList from '../../../inventory/InventoryList';
import InventoryForm from '../../../inventory/InventoryForm';
import { useAuthFetch } from '../../../../lib/use-auth-fetch';
export default function InventoryView() {
const [inventory, setInventory] = useState<InventoryItem[]>([]);
const [products, setProducts] = useState<Product[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const authFetch = useAuthFetch();
const load = useCallback(async () => {
setLoading(true);
setError(null);
try {
const [invRes, prodRes] = await Promise.all([
fetch('/api/inventory'),
authFetch('/api/inventory'),
fetch('/api/products'),
]);
if (!invRes.ok) throw new Error('Kunde inte hämta inventarie');
@@ -29,7 +31,7 @@ export default function InventoryView() {
} finally {
setLoading(false);
}
}, []);
}, [authFetch]);
useEffect(() => { load(); }, [load]);
@@ -4,6 +4,7 @@ import { useState, useEffect, useCallback } from 'react';
import type { Product } from '../../../../features/inventory/types';
import AddToPantryForm from '../../../baslager/AddToPantryForm';
import PantryList from '../../../baslager/PantryList';
import { useAuthFetch } from '../../../../lib/use-auth-fetch';
type PantryItem = {
id: number;
@@ -25,15 +26,16 @@ export default function PantryView() {
const [inventoryByProductId, setInventoryByProductId] = useState<Record<number, InventoryItem[]>>({});
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const authFetch = useAuthFetch();
const load = useCallback(async () => {
setLoading(true);
setError(null);
try {
const [pantryRes, prodRes, invRes] = await Promise.all([
fetch('/api/pantry'),
authFetch('/api/pantry'),
fetch('/api/products'),
fetch('/api/inventory').catch(() => null),
authFetch('/api/inventory').catch(() => null),
]);
if (!pantryRes.ok) throw new Error('Kunde inte hämta baslager');
if (!prodRes.ok) throw new Error('Kunde inte hämta produkter');
@@ -52,7 +54,7 @@ export default function PantryView() {
} finally {
setLoading(false);
}
}, []);
}, [authFetch]);
useEffect(() => { load(); }, [load]);