44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
import Link from 'next/link';
|
|
import { fetchJson } from '../../lib/api';
|
|
import type { Recipe } from '../../features/inventory/types';
|
|
import RecipePreview from './RecipePreview';
|
|
|
|
export default async function RecipesPage() {
|
|
const recipes = await fetchJson<Recipe[]>('/api/recipes');
|
|
|
|
return (
|
|
<main style={{ padding: '1.5rem', maxWidth: '1000px', margin: '0 auto' }}>
|
|
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '1.5rem' }}>
|
|
<h1 style={{ margin: 0 }}>Recept</h1>
|
|
<Link
|
|
href="/recipes/create"
|
|
style={{
|
|
padding: '0.5rem 1rem',
|
|
background: '#0070f3',
|
|
color: 'white',
|
|
borderRadius: '4px',
|
|
textDecoration: 'none',
|
|
fontWeight: 500,
|
|
fontSize: '1rem',
|
|
transition: 'background 0.2s',
|
|
}}
|
|
>
|
|
Lägg till nytt recept
|
|
</Link>
|
|
</div>
|
|
<p>Här kan du jämföra recept mot nuvarande hemmavaror.</p>
|
|
<RecipePreview recipes={recipes} />
|
|
</main>
|
|
);
|
|
}
|
|
|
|
function parseQuantityInput(input: string, defaultUnit: string) {
|
|
const match = input.trim().match(/^([\d.,]+)\s*([a-zA-Z]*)$/);
|
|
if (!match) return { quantity: NaN, unit: defaultUnit };
|
|
let [, num, unit] = match;
|
|
num = num.replace(',', '.');
|
|
unit = unit || defaultUnit;
|
|
if (defaultUnit === 'kg' && (unit === 'g' || unit === 'gram')) return { quantity: parseFloat(num) / 1000, unit: 'kg' };
|
|
if (defaultUnit === 'g' && (unit === 'kg' || unit === 'kilogram')) return { quantity: parseFloat(num) * 1000, unit: 'g' };
|
|
return { quantity: parseFloat(num), unit };
|
|
} |