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

@@ -183,10 +183,14 @@ function updateKcalDisplay() {
el.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,
@@ -206,7 +210,7 @@ function computeEffectiveNutritionTotals(recipe) {
let kcal = 0, protein = 0, fat = 0, carbs = 0;
for (const ing of recipe.ingredients) {
const effectiveId = getEffectiveIngredientId(ing.ingredientId);
const n = nutritionForAmount(effectiveId, ing.amount * currentServings);
const n = nutritionForAmount(effectiveId, ing.amount * currentServings, ing.unit);
if (n) {
kcal += n.kcal;
protein += n.protein;
@@ -290,7 +294,7 @@ function renderIngredients(recipe) {
const effectiveDef = INGREDIENTS[effectiveId];
const effectiveName = effectiveDef?.name || effectiveId;
const scaledAmount = ing.amount * currentServings;
const nutrition = nutritionForAmount(effectiveId, scaledAmount);
const nutrition = nutritionForAmount(effectiveId, scaledAmount, ing.unit);
const isSwapped = effectiveId !== origId;
const isExpanded = expandedAlternatives.has(origId);
@@ -314,7 +318,7 @@ function renderIngredients(recipe) {
const altName = def?.name || altId;
const isSelected = effectiveId === altId;
const isOriginal = altId === origId;
const altNutrition = nutritionForAmount(altId, scaledAmount);
const altNutrition = nutritionForAmount(altId, scaledAmount, ing.unit);
const radioDot = `<div class="w-3.5 h-3.5 rounded-full border-2 shrink-0 ${isSelected ? 'border-gray-900' : 'border-gray-300'} flex items-center justify-center">${isSelected ? '<div class="w-1.5 h-1.5 rounded-full bg-gray-900"></div>' : ''}</div>`;
const defaultTag = isOriginal ? `<span class="text-[9px] px-1.5 py-0.5 rounded ${isSelected ? 'bg-gray-200 text-gray-600' : 'bg-gray-100 text-gray-400'} font-medium ml-1">Domyślny</span>` : '';
@@ -585,7 +589,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>`;
@@ -598,7 +602,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'