Enhance RecipePreview and RecipesService with additional inventory item properties and conversion logic
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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) => {
|
||||||
<div key={item.id}>
|
const weight = isWeightUnit(item.unit);
|
||||||
#{item.id}: {item.quantity} {item.unit}
|
const pieces = isPieceUnit(ingredient.requiredUnit);
|
||||||
{item.brand ? `, ${item.brand}` : ''}
|
const pieces2 = isPieceUnit(item.unit);
|
||||||
{item.location ? `, ${item.location}` : ''}
|
const weight2 = isWeightUnit(ingredient.requiredUnit);
|
||||||
{item.bestBeforeDate
|
|
||||||
? `, bäst före ${formatDate(item.bestBeforeDate)}`
|
return (
|
||||||
: ''}
|
<div key={item.id}>
|
||||||
</div>
|
#{item.id}: {item.quantity} {item.unit}
|
||||||
))}
|
{item.canConvert ? (
|
||||||
|
<span> ≈ {(item.convertedQuantity || 0).toFixed(2)} {ingredient.requiredUnit}</span>
|
||||||
|
) : (
|
||||||
|
<span>
|
||||||
|
{weight && pieces
|
||||||
|
? ' (kan inte konvertera vikt till stycken)'
|
||||||
|
: pieces2 && weight2
|
||||||
|
? ' (kan inte konvertera stycken till vikt)'
|
||||||
|
: ' (kan inte konvertera)'}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
</article>
|
</article>
|
||||||
|
|||||||
@@ -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 = {
|
||||||
|
|||||||
Reference in New Issue
Block a user