import InventoryForm from './InventoryForm'; import ProductForm from './ProductForm'; import Link from 'next/link'; import { fetchJson } from '../../lib/api'; import type { InventoryItem, Product } from '../../features/inventory/types'; import InventoryList from './InventoryList'; import Navigation from '../Navigation'; function formatDate(value: string | null) { if (!value) return null; return new Date(value).toLocaleDateString('sv-SE'); } function getBestBeforeStatus(bestBeforeDate: string | null) { if (!bestBeforeDate) { return { label: 'Ingen bäst före angiven', color: '#666', background: '#f5f5f5', border: '#ddd', }; } const today = new Date(); const bestBefore = new Date(bestBeforeDate); today.setHours(0, 0, 0, 0); bestBefore.setHours(0, 0, 0, 0); const diffMs = bestBefore.getTime() - today.getTime(); const diffDays = Math.round(diffMs / (1000 * 60 * 60 * 24)); if (diffDays < 0) { return { label: 'Utgången', color: '#8b0000', background: '#ffeaea', border: '#f1b5b5', }; } if (diffDays <= 3) { return { label: 'Snart utgången', color: '#8a4b00', background: '#fff4e5', border: '#f0cf9b', }; } return { label: 'OK', color: '#1f5f2c', background: '#ecf8ee', border: '#b9e0bf', }; } type InventoryPageProps = { searchParams?: Promise<{ location?: string; sort?: string; }>; }; function buildInventoryUrl(location?: string, sort?: string) { const params = new URLSearchParams(); if (location) { params.set('location', location); } if (sort) { params.set('sort', sort); } const query = params.toString(); return query ? `/inventory?${query}` : '/inventory'; } export default async function InventoryPage({ searchParams }: InventoryPageProps) { const resolvedSearchParams = searchParams ? await searchParams : {}; const location = resolvedSearchParams.location || ''; const sort = resolvedSearchParams.sort || ''; const inventoryPath = (() => { const params = new URLSearchParams(); if (location) params.set('location', location); if (sort) params.set('sort', sort); const query = params.toString(); return query ? `/api/inventory?${query}` : '/api/inventory'; })(); const [inventory, products] = await Promise.all([ fetchJson(inventoryPath), fetchJson('/api/products'), ]); const locationOptions = ['', 'Kyl', 'Frys', 'Skafferi']; const sortOptions = [ { value: '', label: 'Senast tillagda' }, { value: 'nameAsc', label: 'Namn A\u2013\u00d6' }, { value: 'bestBeforeAsc', label: 'B\u00e4st f\u00f6re Stigande' }, { value: 'bestBeforeDesc', label: 'B\u00e4st f\u00f6re Fallande' }, ]; return (

Varor hemma

Filter och sortering

Plats
{locationOptions.map((option) => { const isActive = location === option; const label = option === '' ? 'Alla' : option; return ( {label} ); })}
Sortering
{sortOptions.map((option) => { const isActive = sort === option.value; return ( {option.label} ); })}
); }