food diary improvements

This commit is contained in:
Joren de Graaf
2026-10-17 12:12:41 +02:00
parent 76c76bc281
commit e3b14d6823
9 changed files with 258 additions and 20 deletions
+46 -1
View File
@@ -4,16 +4,61 @@
const amount = document.querySelector('#id_amount');
const custom = document.querySelector('[data-food-custom-fields]');
const amountField = document.querySelector('[data-food-amount-field]');
const countField = document.querySelector('[data-food-portion-count]');
const count = document.querySelector('#id_portion_count');
const save = document.querySelector('#id_save_portion');
const saveFields = document.querySelector('[data-food-save-fields]');
const inventory = document.querySelector('[data-food-inventory]');
const packageSelect = inventory?.querySelector('#id_inventory_package');
const preview = inventory?.querySelector('[data-food-inventory-preview]');
const fmt = value => Number(value.toFixed(3)).toString();
const syncPreview = () => {
if (!preview) return;
const selected = packageSelect.selectedOptions[0];
const used = Number(amount.value);
if (!Number.isFinite(used) || used <= 0) {
preview.textContent = 'Enter a valid amount to preview nutrition and stock.';
return;
}
const countValue = Number(count.value || 1);
const selectedLabel = select.selectedOptions[0].textContent.split(' — ')[0].trim();
const label = select.value === 'custom' ? `${fmt(used)} ${inventory.dataset.unit}` :
`${fmt(countValue)} × ${selectedLabel} = ${fmt(used)} ${inventory.dataset.unit}`;
const kcal = Number(inventory.dataset.kcal);
const nutrition = inventory.dataset.kcal ? ` · ${fmt(kcal * used / 100)} kcal` : ' · calories unavailable';
if (!selected.value) {
preview.textContent = `${label}${nutrition}. Inventory will not change.`;
return;
}
const remaining = Number(selected.dataset.remaining);
const nativeRemaining = Number(selected.dataset.nativeRemaining);
const nativeUnit = selected.dataset.nativeUnit;
const factor = remaining / nativeRemaining;
const after = remaining - used;
const onePortion = Number(select.selectedOptions[0].dataset.amount);
const portionStock = select.value.startsWith('portion:') && onePortion > 0 ?
` ${fmt(remaining / onePortion)} ${selectedLabel} equivalents available → ${fmt(Math.max(0, after / onePortion))} after.` : '';
preview.textContent = after < -0.000001 ?
`${label}${nutrition}. Not enough in this package: ${fmt(remaining)} ${inventory.dataset.unit} available.` :
`${label}${nutrition}. ${fmt(nativeRemaining)} ${nativeUnit} available now → ${fmt(after / factor)} ${nativeUnit} after logging.${portionStock}`;
};
const sync = () => {
const customSelected = select.value === 'custom';
custom.hidden = !customSelected;
amountField.hidden = !customSelected;
if (!customSelected) amount.value = select.selectedOptions[0].dataset.amount || '100';
countField.hidden = customSelected;
custom.style.display = customSelected ? '' : 'none';
amountField.style.display = customSelected ? '' : 'none';
countField.style.display = customSelected ? 'none' : '';
if (!customSelected) amount.value = String(Number(select.selectedOptions[0].dataset.amount || '100') * Number(count.value || 1));
saveFields.hidden = !customSelected || !save.checked;
saveFields.style.display = customSelected && save.checked ? '' : 'none';
syncPreview();
};
select.addEventListener('change', sync);
save.addEventListener('change', sync);
count.addEventListener('input', sync);
amount.addEventListener('input', syncPreview);
packageSelect?.addEventListener('change', syncPreview);
sync();
})();