diff --git a/backend/src/recipes/recipes.service.ts b/backend/src/recipes/recipes.service.ts index c8d073c5..b568e510 100644 --- a/backend/src/recipes/recipes.service.ts +++ b/backend/src/recipes/recipes.service.ts @@ -221,6 +221,8 @@ export class RecipesService { quantity: item.quantity, unit: item.unit, location: item.location, + brand: item.brand || null, + bestBeforeDate: item.bestBeforeDate || null, })), otherInventoryItems: otherUnitItems.map((item: any) => { // Kolla om konvertering är möjlig (samma enhetskategori) diff --git a/frontend/app/recipes/RecipePreview.tsx b/frontend/app/recipes/RecipePreview.tsx index 55bcf60e..65529140 100644 --- a/frontend/app/recipes/RecipePreview.tsx +++ b/frontend/app/recipes/RecipePreview.tsx @@ -42,6 +42,14 @@ function formatDate(value: string | null) { return new Date(value).toLocaleDateString('sv-SE'); } +function isWeightUnit(unit: string): boolean { + return ['kg', 'g', 'mg', 'ml', 'l'].includes(unit.trim().toLowerCase()); +} + +function isPieceUnit(unit: string): boolean { + return ['st', 'stycke'].includes(unit.trim().toLowerCase()); +} + export default function RecipePreview({ recipes }: Props) { const [selectedRecipeId, setSelectedRecipeId] = useState(''); const [preview, setPreview] = useState(null); @@ -217,27 +225,38 @@ export default function RecipePreview({ recipes }: Props) { #{item.id}: {item.quantity} {item.unit} {item.brand ? `, ${item.brand}` : ''} {item.location ? `, ${item.location}` : ''} - {item.bestBeforeDate - ? `, bäst före ${formatDate(item.bestBeforeDate)}` - : ''} + {item.bestBeforeDate ? `, bäst före ${formatDate(item.bestBeforeDate)}` : ''} ))} ) : null} - {ingredient.otherInventoryItems.length > 0 ? ( + {ingredient.otherInventoryItems && ingredient.otherInventoryItems.length > 0 ? (
- Andra inventory-poster med annan enhet - {ingredient.otherInventoryItems.map((item) => ( -
- #{item.id}: {item.quantity} {item.unit} - {item.brand ? `, ${item.brand}` : ''} - {item.location ? `, ${item.location}` : ''} - {item.bestBeforeDate - ? `, bäst före ${formatDate(item.bestBeforeDate)}` - : ''} -
- ))} + Andra enheter: + {ingredient.otherInventoryItems.map((item) => { + const weight = isWeightUnit(item.unit); + const pieces = isPieceUnit(ingredient.requiredUnit); + const pieces2 = isPieceUnit(item.unit); + const weight2 = isWeightUnit(ingredient.requiredUnit); + + return ( +
+ #{item.id}: {item.quantity} {item.unit} + {item.canConvert ? ( + ≈ {(item.convertedQuantity || 0).toFixed(2)} {ingredient.requiredUnit} + ) : ( + + {weight && pieces + ? ' (kan inte konvertera vikt till stycken)' + : pieces2 && weight2 + ? ' (kan inte konvertera stycken till vikt)' + : ' (kan inte konvertera)'} + + )} +
+ ); + })}
) : null} diff --git a/frontend/features/inventory/types.ts b/frontend/features/inventory/types.ts index e1068af9..37db3f10 100644 --- a/frontend/features/inventory/types.ts +++ b/frontend/features/inventory/types.ts @@ -80,9 +80,11 @@ export type RecipePreviewInventoryItem = { id: number; quantity: string; unit: string; - brand: string | null; - location: string | null; - bestBeforeDate: string | null; + brand?: string | null; + location?: string | null; + bestBeforeDate?: string | null; + canConvert?: boolean; + convertedQuantity?: number; }; export type RecipeInventoryPreviewIngredient = {