Fix calculating nutritions
All checks were successful
Build and Deploy / build-and-push (push) Successful in 26s

This commit is contained in:
2026-03-27 23:08:54 +01:00
parent 02fb8a2754
commit 855d17374e
3 changed files with 33 additions and 15 deletions

View File

@@ -173,10 +173,14 @@ function updateKcalDisplay() {
document.getElementById('rd-kcal').textContent = `${kcal} kcal`;
}
function nutritionForAmount(ingredientId, amount) {
function nutritionForAmount(ingredientId, amount, unit) {
const def = INGREDIENTS[ingredientId];
if (!def?.nutritionPer100g) return null;
const f = amount / 100;
let grams = amount;
if ((unit === 'szt.' || unit === 'szt') && def.weightPerPiece) {
grams = amount * def.weightPerPiece;
}
const f = grams / 100;
return {
kcal: Math.round(def.nutritionPer100g.kcal * f),
protein: Math.round(def.nutritionPer100g.protein * f * 10) / 10,
@@ -214,7 +218,7 @@ function renderIngredients(recipe) {
const altCards = ing.alternatives.map((altId) => {
const altDef = INGREDIENTS[altId];
const altName = escapeHtml(altDef?.name || altId);
const nutrition = nutritionForAmount(altId, scaledAmount);
const nutrition = nutritionForAmount(altId, scaledAmount, ing.unit);
return `<div class="bg-gray-50 rounded-lg p-2.5 border border-gray-100">
<p class="text-[12px] font-medium text-gray-700">${altName}</p>
${renderNutritionLine(nutrition)}
@@ -497,7 +501,7 @@ export function setupRecipeDetail() {
<span class="text-[12px] font-semibold text-gray-900 truncate">${escapeHtml(effectiveName)}</span>
<span class="text-[10px] text-gray-400 tabular-nums shrink-0">${displayAmount} ${escapeHtml(ing.unit)}</span>
</div>
${renderNutritionLine(nutritionForAmount(effectiveId, scaledAmount))}
${renderNutritionLine(nutritionForAmount(effectiveId, scaledAmount, ing.unit))}
</div>
<i class="fas fa-chevron-${isExpanded ? 'up' : 'down'} text-[9px] text-gray-400 shrink-0"></i>
</button>`;
@@ -510,7 +514,7 @@ export function setupRecipeDetail() {
const altName = def?.name || altId;
const isSelected = effectiveId === altId;
const isOriginal = altId === origId;
const nutrition = nutritionForAmount(altId, scaledAmount);
const nutrition = nutritionForAmount(altId, scaledAmount, ing.unit);
const selectedCls = isSelected
? 'border-gray-900 bg-gray-50 ring-1 ring-gray-900'