Skip to content

Commit

Permalink
feat: reset preferences instead of deleting + add switch to use prefe…
Browse files Browse the repository at this point in the history
…rences in preferences + fix bug for unknown mandatory attributes (#6800)

* fix: typo in product scoring

* make lint happy

* feat: reset preferences + add switch on top of preferences

* Update po/common/en.po

Co-authored-by: Pierre Slamich <[email protected]>

* fix to set status to unknown if a mandatory attribute is unknown

* changed message for default preferences

* add missing semi-colon

Co-authored-by: Pierre Slamich <[email protected]>
  • Loading branch information
stephanegigandet and teolemon authored May 25, 2022
1 parent b449d32 commit 9251301
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 35 deletions.
73 changes: 47 additions & 26 deletions html/js/product-preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
var attribute_groups; // All supported attribute groups and attributes + translated strings
var preferences; // All supported preferences + translated strings
var use_user_product_preferences_for_ranking = JSON.parse(localStorage.getItem('use_user_product_preferences_for_ranking'));
var default_preferences = { "nutriscore" : "very_important", "nova" : "important", "ecoscore" : "important" };

function get_user_product_preferences () {
// Retrieve user preferences from local storage
Expand All @@ -17,7 +18,7 @@ function get_user_product_preferences () {
}
else {
// Default preferences
user_product_preferences = { "nutriscore" : "very_important", "nova" : "important", "ecoscore" : "important" };
user_product_preferences = default_preferences;
}

return user_product_preferences;
Expand Down Expand Up @@ -84,6 +85,39 @@ function display_selected_preferences (target_selected_summary, product_preferen
}


function generate_preferences_switch_button(preferences_text, checkbox_id) {

var checked = '';
if (use_user_product_preferences_for_ranking) {
checked = " checked";
}

var html = '<fieldset class="switch round success" tabindex="0" id="' + checkbox_id +'_switch" style="float:left;margin:0;margin-right:.5rem;padding-top:0.1rem;">'
+ '<input class="preferences_checkboxes" id="' + checkbox_id + '" type="checkbox"' + checked + '>'
+ '<label for="' + checkbox_id +'"></label></fieldset>'
+ '<label for="' + checkbox_id +'" style="float:left;margin-right:1em;padding-top:4px">' + preferences_text + '</label>';

return html;
}

function activate_preferences_switch_buttons(change) {

$(".preferences_checkboxes").change(function() {

localStorage.setItem('use_user_product_preferences_for_ranking', this.checked);
use_user_product_preferences_for_ranking = this.checked;

// Update the other checkbox value
$(".preferences_checkboxes").prop('checked',use_user_product_preferences_for_ranking);

// Call the change callback if we have one (e.g. to update search results)
if (change) {
change();
}
});

}

// display a switch to use preferences (on list of products pages) and a button to edit preferences

function display_use_preferences_switch_and_edit_preferences_button (target_selected, target_selection_form, change) {
Expand All @@ -98,16 +132,8 @@ function display_use_preferences_switch_and_edit_preferences_button (target_sele
// we are on a page with multiple products

if (page_type == 'products') {

var checked = '';
if (use_user_product_preferences_for_ranking) {
checked = " checked";
}

html += '<fieldset class="switch round success" tabindex="0" id="preferences_switch" style="float:left;margin-right:.5rem;padding-top:0.1rem;">'
+ '<input id="preferences_checkbox" type="checkbox"' + checked + '>'
+ '<label for="preferences_checkbox"></label></fieldset>'
+ '<label for="preferences_checkbox" style="float:left;margin-right:1em;padding-top:0.5rem;">' + preferences_text + '</label>' + html_edit_preferences;

html += generate_preferences_switch_button(preferences_text, "preferences_switch_in_list_of_products") + html_edit_preferences;
}
else {

Expand All @@ -117,16 +143,7 @@ function display_use_preferences_switch_and_edit_preferences_button (target_sele
$( target_selected ).html(html);

if (page_type == 'products') {
$("#preferences_checkbox").change(function() {

localStorage.setItem('use_user_product_preferences_for_ranking', this.checked);
use_user_product_preferences_for_ranking = this.checked;

// Call the change callback if we have one (e.g. to update search results)
if (change) {
change();
}
});
activate_preferences_switch_buttons(change);
}

$( "#show_selection_form").on( "click", function() {
Expand Down Expand Up @@ -243,7 +260,9 @@ function display_user_product_preferences (target_selected, target_selection_for
+ " " + lang().close + '</a></div>'
+ "<h2>" + lang().preferences_edit_your_food_preferences + "</h2>"
+ "<p>" + lang().preferences_locally_saved + "</p>"
+ '<a id="delete_all_preferences_button" class="button small round success" role="button" tabindex="0">' + lang().delete_all_preferences + '</a>'
+ generate_preferences_switch_button(lang().classify_products_according_to_your_preferences, "preferences_switch_in_preferences") + "<hr class='clear:left;'>"
+ '<a id="reset_preferences_button" class="button small round success" role="button" tabindex="0">' + lang().reset_preferences + '</a>'
+ ' ' + lang().reset_preferences_details
+ '<ul id="user_product_preferences" class="accordion" data-accordion>'
+ attribute_groups_html.join( "" )
+ '</ul>'
Expand All @@ -254,6 +273,8 @@ function display_user_product_preferences (target_selected, target_selection_for
+ " " + lang().close + '</a></div><br><br>'
+ '</div>'
);

activate_preferences_switch_buttons(change);

$( ".attribute_radio").change( function () {
if (this.checked) {
Expand All @@ -274,8 +295,8 @@ function display_user_product_preferences (target_selected, target_selection_for
display_use_preferences_switch_and_edit_preferences_button(target_selected, target_selection_form, change);
}

$( "#delete_all_preferences_button").on("click", function() {
user_product_preferences = {};
$( "#reset_preferences_button").on("click", function() {
user_product_preferences = default_preferences;
localStorage.setItem('user_product_preferences', JSON.stringify(user_product_preferences));

// Redisplay user preferences
Expand All @@ -288,9 +309,9 @@ function display_user_product_preferences (target_selected, target_selection_for
}
});

$("#delete_all_preferences_button").on('keydown', (event) => {
$("#reset_preferences_button").on('keydown', (event) => {
if (event.key === 'Space' || event.key === 'Enter') {
$("#delete_all_preferences_button").trigger("click");
$("#reset_preferences_button").trigger("click");
}
});

Expand Down
23 changes: 15 additions & 8 deletions html/js/product-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
//
// Output values are returned in the product object
//
// - match_status:
// very_good_match
// good_match
// poor_match
// unknown_match
// may_not_match
// does_not_match
//
// - match_score: number from 0 to 100
// - the score is 0 if
// - otherwise the score is a weighted average of how well the product matches
// each attribute selected by the user
//
// - match_status:
// - very_good_match score >= 75
// - good_match score >= 50
// - poor_match score < 50
// - unknown_match at least one mandatory attribute is unknown, or unknown attributes weight more than 50% of the score
// - may_not_match at least one mandatory attribute score is <= 50 (e.g. may contain traces of an allergen)
// - does_not_match at least one mandatory attribute score is <= 10 (e.g. contains an allergen, is not vegan)
//
// - match_attributes: array of arrays of attributes corresponding to the product and
// each set of preferences: mandatory, very_important, important
Expand Down Expand Up @@ -125,6 +128,10 @@ function match_product_to_preferences (product, product_preferences) {
else if ("may_not_match" in product.attributes_for_status) {
product.match_status = "may_not_match";
}
// If one of the mandatory attribute is unknown, set an unknown match
else if ("unknown_match" in product.attributes_for_status) {
product.match_status = "unknown_match";
}
// If too many attributes are unknown, set an unknown match
else if (sum_of_factors_for_unknown_attributes >= sum_of_factors / 2) {
product.match_status = "unknown_match";
Expand Down
12 changes: 11 additions & 1 deletion po/common/common.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6131,4 +6131,14 @@ msgstr "May not match"
# The translation needs to be short as it is displayed at the top of small product cards
msgctxt "products_match_does_not_match"
msgid "Does not match"
msgstr "Does not match"
msgstr "Does not match"

msgctxt "reset_preferences"
msgid "Use default preferences"
msgstr "Use default preferences"

msgctxt "reset_preferences_details"
msgid "Nutri-Score, Eco-Score and food processing level (NOVA)"
msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)"


10 changes: 10 additions & 0 deletions po/common/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -6155,3 +6155,13 @@ msgstr "May not match"
msgctxt "products_match_does_not_match"
msgid "Does not match"
msgstr "Does not match"

msgctxt "reset_preferences"
msgid "Use default preferences"
msgstr "Use default preferences"

msgctxt "reset_preferences_details"
msgid "Nutri-Score, Eco-Score and food processing level (NOVA)"
msgstr "Nutri-Score, Eco-Score and food processing level (NOVA)"


0 comments on commit 9251301

Please sign in to comment.