Skip to content

Commit

Permalink
fix: on the fly nutrition edit checks #8420
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanegigandet committed May 26, 2023
1 parent 8d6d754 commit 99783e0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 33 deletions.
9 changes: 5 additions & 4 deletions po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6657,14 +6657,15 @@ msgctxt "faq_for_producers"
msgid "FAQ for producers"
msgstr "FAQ for producers"

msgctxt "product_js_enter_value_between_0_and_100"
msgid "Please enter a value between 0 and 100."
msgstr "Please enter a value between 0 and 100."
# variable names between { } must not be translated
msgctxt "product_js_enter_value_between_0_and_max"
msgid "Please enter a value between 0 and {max}."
msgstr "Please enter a value between 0 and {max}."

msgctxt "product_js_sugars_warning"
msgid "Sugars should not be higher than carbohydrates."
msgstr "Sugars should not be higher than carbohydrates."

msgctxt "product_js_saturated_fat_warning"
msgid "Saturated fat should not be higher than fat."
msgstr "Saturated fat should not be higher than fat."
msgstr "Saturated fat should not be higher than fat."
9 changes: 5 additions & 4 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -6667,14 +6667,15 @@ msgctxt "faq_for_producers"
msgid "FAQ for producers"
msgstr "FAQ for producers"

msgctxt "product_js_enter_value_between_0_and_100"
msgid "Please enter a value between 0 and 100."
msgstr "Please enter a value between 0 and 100."
# variable names between { } must not be translated
msgctxt "product_js_enter_value_between_0_and_max"
msgid "Please enter a value between 0 and {max}."
msgstr "Please enter a value between 0 and {max}."

msgctxt "product_js_sugars_warning"
msgid "Sugars should not be higher than carbohydrates."
msgstr "Sugars should not be higher than carbohydrates."

msgctxt "product_js_saturated_fat_warning"
msgid "Saturated fat should not be higher than fat."
msgstr "Saturated fat should not be higher than fat."
msgstr "Saturated fat should not be higher than fat."
83 changes: 58 additions & 25 deletions templates/web/pages/product_edit/product_edit_form_display.tt.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

\$('input[name=[% nutrition.nutrition_data_per %]]').change(function() {
if (\$('input[name=[% nutrition.nutrition_data_per %]]:checked').val() == '100g') {
\$('#[% nutrition.nutrition_data %]_100g').show();
\$('#[% nutrition.nutrition_data %]_maxg').show();
\$('#[% nutrition.nutrition_data %]_serving').hide();
} else {
\$('#[% nutrition.nutrition_data %]_100g').hide();
\$('#[% nutrition.nutrition_data %]_maxg').hide();
\$('#[% nutrition.nutrition_data %]_serving').show();
}
update_nutrition_image_copy();
Expand Down Expand Up @@ -50,34 +50,67 @@ function show_warning(should_show, nutrient_id, warning_message){
}
}

var required_nutrients_id = ['energy-kj', 'energy-kcal', 'fat', 'saturated-fat', 'sugars', 'carbohydrates', 'fiber', 'proteins', 'salt', 'sodium', 'alcohol'];
function check_nutrient(nutrient_id) {
// check the changed nutrient value
var nutrient_value = \$('#nutriment_' + nutrient_id).val().replace(',','.').replace(/^<|>|~/, '');
var nutrient_unit = \$('#nutriment_' + nutrient_id + '_unit').val()

required_nutrients_id.forEach(nutrient_id => {
\$('#nutriment_' + nutrient_id).on('input', function() {
// define the max valid value
var max;
var percent;

// check the changed nutrient value
var nutrient_value = \$(this).val().replace(',','.');
var is_above_or_below_100 = (isNaN(nutrient_value) && nutrient_value != '-') || nutrient_value < 0 || nutrient_value > 100;
if (nutrient_id == 'energy-kj') {
max = 3800;
}
else if (nutrient_id == 'energy-kcal') {
max = 900;
}
else if (nutrient_id == 'alcohol') {
max = 100;
percent = true;
}
else if (nutrient_unit == 'g') {
max = 100;
}
else if (nutrient_unit == 'mg') {
max = 100 * 1000;
}
else if (nutrient_unit == 'µg') {
max = 100 * 1000 * 1000;
}

var is_above_or_below_max;
if (max) {
is_above_or_below_max = (isNaN(nutrient_value) && nutrient_value != '-') || nutrient_value < 0 || nutrient_value > max;
// if the nutrition facts are indicated per serving, the value can be above 100
if ((nutrient_value > 100) && (\$('#nutrition_data_per_serving').is(':checked'))) {
is_above_or_below_100 = false;
if ((nutrient_value > max) && (\$('#nutrition_data_per_serving').is(':checked')) && !percent) {
is_above_or_below_max = false;
}
show_warning(is_above_or_below_100, nutrient_id, lang().product_js_enter_value_between_0_and_100);
show_warning(is_above_or_below_max, nutrient_id, lang().product_js_enter_value_between_0_and_max.replace('{max}', max));
}

// check that nutrients are sound (e.g. sugars is not above carbohydrates)
// but only if the changed nutrient does not have a warning
// otherwise we may clear the sugars or saturated-fat warning
if (! is_above_or_below_100) {
var fat_value = \$('#nutriment_fat').val().replace(',','.');
var carbohydrates_value = \$('#nutriment_carbohydrates').val().replace(',','.');
var sugars_value = \$('#nutriment_sugars').val().replace(',','.');
var saturated_fats_value = \$('#nutriment_saturated-fat').val().replace(',','.');
// check that nutrients are sound (e.g. sugars is not above carbohydrates)
// but only if the changed nutrient does not have a warning
// otherwise we may clear the sugars or saturated-fat warning
if (! is_above_or_below_max) {
var fat_value = \$('#nutriment_fat').val().replace(',','.');
var carbohydrates_value = \$('#nutriment_carbohydrates').val().replace(',','.');
var sugars_value = \$('#nutriment_sugars').val().replace(',','.');
var saturated_fats_value = \$('#nutriment_saturated-fat').val().replace(',','.');

var is_sugars_above_carbohydrates = parseFloat(carbohydrates_value) < parseFloat(sugars_value);
show_warning(is_sugars_above_carbohydrates, 'sugars', lang().product_js_sugars_warning);

var is_sugars_above_carbohydrates = parseFloat(carbohydrates_value) < parseFloat(sugars_value);
show_warning(is_sugars_above_carbohydrates, 'sugars', lang().product_js_sugars_warning);

var is_fat_above_saturated_fats = parseFloat(fat_value) < parseFloat(saturated_fats_value);
show_warning(is_fat_above_saturated_fats, 'saturated-fat', lang().product_js_saturated_fat_warning);
}
var is_fat_above_saturated_fats = parseFloat(fat_value) < parseFloat(saturated_fats_value);
show_warning(is_fat_above_saturated_fats, 'saturated-fat', lang().product_js_saturated_fat_warning);
}
}

var required_nutrients_id = ['energy-kj', 'energy-kcal', 'fat', 'saturated-fat', 'sugars', 'carbohydrates', 'fiber', 'proteins', 'salt', 'sodium', 'alcohol'];

required_nutrients_id.forEach(nutrient_id => {
\$('#nutriment_' + nutrient_id).on('input', function() {
check_nutrient(nutrient_id);
});
check_nutrient(nutrient_id);
});

0 comments on commit 99783e0

Please sign in to comment.