From 37472f6c43690a7ab9fa400d343055e0f3f9017e Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Wed, 22 Apr 2026 18:54:04 +0200 Subject: [PATCH] refactor: remove unused load function and related logic in PantryView component --- frontend/app/baslager/PantryList.tsx | 126 ------------------ frontend/app/profil/tabs/views/PantryView.tsx | 57 -------- 2 files changed, 183 deletions(-) diff --git a/frontend/app/baslager/PantryList.tsx b/frontend/app/baslager/PantryList.tsx index 503dc2a3..33623395 100644 --- a/frontend/app/baslager/PantryList.tsx +++ b/frontend/app/baslager/PantryList.tsx @@ -98,129 +98,3 @@ export default function PantryList({ items, onDeleted }: Props) { ); } - - - async function handleRemove(id: number, name: string) { - if (!confirm(`Ta bort "${name}" från baslagret?`)) return; - const res = await fetch(`/api/admin/pantry-item/${id}`, { method: 'DELETE' }); - if (res.ok) { - if (onDeleted) onDeleted(); - else router.refresh(); - } - } - - if (items.length === 0) { - return ( -

- Baslagret är tomt. Lägg till produkter ovan. -

- ); - } - - // Gruppera per kategori - const grouped = items.reduce>((acc, item) => { - const cat = item.product.category || 'Övrigt'; - if (!acc[cat]) acc[cat] = []; - acc[cat].push(item); - return acc; - }, {}); - - const sortedCategories = Object.keys(grouped).sort((a, b) => { - if (a === 'Övrigt') return 1; - if (b === 'Övrigt') return -1; - return a.localeCompare(b, 'sv'); - }); - - return ( -
- {sortedCategories.map((category) => ( -
-

- {category} -

-
- {grouped[category].map((item) => { - const displayName = item.product.canonicalName || item.product.name; - const invItems = inventoryByProductId[item.product.id] || []; - const hasInventory = invItems.length > 0; - - // Summera per enhet för visning - const unitSummary = invItems.reduce>((acc, inv) => { - const u = inv.unit || '?'; - acc[u] = (acc[u] || 0) + parseFloat(inv.quantity || '0'); - return acc; - }, {}); - const inventoryText = Object.entries(unitSummary) - .map(([u, q]) => `${q % 1 === 0 ? q : q.toFixed(1)} ${u}`) - .join(', '); - - return ( -
-
- - {hasInventory ? '✓' : '○'} - - {displayName} - {hasInventory ? ( - - {inventoryText} - - ) : ( - - Saknas i inventariet - - )} -
- -
- ); - })} -
-
- ))} -
- ); -} diff --git a/frontend/app/profil/tabs/views/PantryView.tsx b/frontend/app/profil/tabs/views/PantryView.tsx index d00a0e37..b143289e 100644 --- a/frontend/app/profil/tabs/views/PantryView.tsx +++ b/frontend/app/profil/tabs/views/PantryView.tsx @@ -68,60 +68,3 @@ export default function PantryView() { ); } - - - const load = useCallback(async () => { - setLoading(true); - setError(null); - try { - const [pantryRes, prodRes, invRes] = await Promise.all([ - authFetch('/api/pantry'), - fetch('/api/products'), - 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'); - const [pantry, prods] = await Promise.all([pantryRes.json(), prodRes.json()]); - const inv: InventoryItem[] = invRes?.ok ? await invRes.json() : []; - setPantryItems(pantry); - setProducts(prods); - const byProd = inv.reduce>((acc, item) => { - if (!acc[item.productId]) acc[item.productId] = []; - acc[item.productId].push(item); - return acc; - }, {}); - setInventoryByProductId(byProd); - } catch (e) { - setError(e instanceof Error ? e.message : 'Okänt fel'); - } finally { - setLoading(false); - } - }, [authFetch]); - - useEffect(() => { load(); }, [load]); - - if (loading) return

Laddar baslager…

; - if (error) return

{error}

; - - const pantryProductIds = new Set(pantryItems.map((i) => i.productId)); - - return ( -
-

- Produkter du alltid räknar med att ha hemma. Lägg till och ta bort varor i ditt baslager. -

- -
-

Lägg till produkt

- -
- -
-

- {pantryItems.length} {pantryItems.length === 1 ? 'produkt' : 'produkter'} i baslagret -

- -
-
- ); -}