Enhance RecipePreview and RecipesService with additional inventory item properties and conversion logic

This commit is contained in:
Nils-Johan Gynther
2026-04-09 22:09:19 +02:00
parent 4fd3c8dc20
commit 29910130f0
3 changed files with 41 additions and 18 deletions
+2
View File
@@ -221,6 +221,8 @@ export class RecipesService {
quantity: item.quantity, quantity: item.quantity,
unit: item.unit, unit: item.unit,
location: item.location, location: item.location,
brand: item.brand || null,
bestBeforeDate: item.bestBeforeDate || null,
})), })),
otherInventoryItems: otherUnitItems.map((item: any) => { otherInventoryItems: otherUnitItems.map((item: any) => {
// Kolla om konvertering är möjlig (samma enhetskategori) // Kolla om konvertering är möjlig (samma enhetskategori)
+31 -12
View File
@@ -42,6 +42,14 @@ function formatDate(value: string | null) {
return new Date(value).toLocaleDateString('sv-SE'); 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) { export default function RecipePreview({ recipes }: Props) {
const [selectedRecipeId, setSelectedRecipeId] = useState(''); const [selectedRecipeId, setSelectedRecipeId] = useState('');
const [preview, setPreview] = useState<RecipeInventoryPreview | null>(null); const [preview, setPreview] = useState<RecipeInventoryPreview | null>(null);
@@ -217,27 +225,38 @@ export default function RecipePreview({ recipes }: Props) {
#{item.id}: {item.quantity} {item.unit} #{item.id}: {item.quantity} {item.unit}
{item.brand ? `, ${item.brand}` : ''} {item.brand ? `, ${item.brand}` : ''}
{item.location ? `, ${item.location}` : ''} {item.location ? `, ${item.location}` : ''}
{item.bestBeforeDate {item.bestBeforeDate ? `, bäst före ${formatDate(item.bestBeforeDate)}` : ''}
? `, bäst före ${formatDate(item.bestBeforeDate)}`
: ''}
</div> </div>
))} ))}
</div> </div>
) : null} ) : null}
{ingredient.otherInventoryItems.length > 0 ? ( {ingredient.otherInventoryItems && ingredient.otherInventoryItems.length > 0 ? (
<div style={{ display: 'grid', gap: '0.35rem' }}> <div style={{ display: 'grid', gap: '0.35rem' }}>
<strong>Andra inventory-poster med annan enhet</strong> <strong>Andra enheter:</strong>
{ingredient.otherInventoryItems.map((item) => ( {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 (
<div key={item.id}> <div key={item.id}>
#{item.id}: {item.quantity} {item.unit} #{item.id}: {item.quantity} {item.unit}
{item.brand ? `, ${item.brand}` : ''} {item.canConvert ? (
{item.location ? `, ${item.location}` : ''} <span> {(item.convertedQuantity || 0).toFixed(2)} {ingredient.requiredUnit}</span>
{item.bestBeforeDate ) : (
? `, bäst före ${formatDate(item.bestBeforeDate)}` <span>
: ''} {weight && pieces
? ' (kan inte konvertera vikt till stycken)'
: pieces2 && weight2
? ' (kan inte konvertera stycken till vikt)'
: ' (kan inte konvertera)'}
</span>
)}
</div> </div>
))} );
})}
</div> </div>
) : null} ) : null}
</article> </article>
+5 -3
View File
@@ -80,9 +80,11 @@ export type RecipePreviewInventoryItem = {
id: number; id: number;
quantity: string; quantity: string;
unit: string; unit: string;
brand: string | null; brand?: string | null;
location: string | null; location?: string | null;
bestBeforeDate: string | null; bestBeforeDate?: string | null;
canConvert?: boolean;
convertedQuantity?: number;
}; };
export type RecipeInventoryPreviewIngredient = { export type RecipeInventoryPreviewIngredient = {