Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Prevention - On the fly quality checks in the product edit form #8258

Merged
merged 10 commits into from
May 3, 2023

Conversation

jnsereko
Copy link
Contributor

@jnsereko jnsereko commented Mar 27, 2023

What

Created an invisible span having a warning on saturated fats and sugars values. inputing an element above the range displays the span.

Screenshot

onfly.mov

Related issue(s) and discussion

@jnsereko jnsereko requested a review from a team as a code owner March 27, 2023 07:53
@github-actions github-actions bot added ✏️ Editing Template::Toolkit The templating toolkit used by product opener. The starting point for HTML/JS/CSS fixes. labels Mar 27, 2023
@github-advanced-security
Copy link

You have successfully added a new SonarCloud configuration ``. As part of the setup process, we have scanned this repository and found no existing alerts. In the future, you will see all code scanning alerts on the repository Security tab.

@@ -34,3 +34,21 @@
\$('#nutrition_data_div').show();
}
});

\$('#nutriment_sugars').on('input', function() {
var inputValue = $(this).val();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @stephanegigandet @yuktea
I jQuery is blocking this from working

its like the .val() is not a function. Uncaught TypeError: this.val is not a function

think i have called the element correctly so it must be working

Copy link
Contributor

@stephanegigandet stephanegigandet Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jnsereko , it's because this file is a template file, so anything starting with $ is interpreted as a variable in the template. If you want the $ to be output in the JS code, you need to escape it like this: \$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jnsereko
Copy link
Contributor Author

this @stephanegigandet somewhat messes up the table. i thought of creating a warning div, just like https://github.com/jnsereko/openfoodfacts-server/blob/d9631fdd0e73ac311a9512ebf156533369f775ae/templates/web/pages/product_edit/product_edit_form_display.tt.html#L78-L82 but since the table is somewhat long, covering almost the whole screen users might not see it

@stephanegigandet
Copy link
Contributor

this @stephanegigandet somewhat messes up the table. i thought of creating a warning div, just like https://github.com/jnsereko/openfoodfacts-server/blob/d9631fdd0e73ac311a9512ebf156533369f775ae/templates/web/pages/product_edit/product_edit_form_display.tt.html#L78-L82 but since the table is somewhat long, covering almost the whole screen users might not see it

One option could be to have an extra table row with the warning, just below the value that is problematic. This way you could use the whole row width (the 3 columns).

@@ -227,6 +227,11 @@ <h1>[% title %]</h1>

<td class="nutriment_col" [% column_display_style_nutrition_data %]>
<input class="nutriment_value nutriment_value_as_sold" id="nutriment_[% nutriment.enid %]" name="nutriment_[% nutriment.enid %]" value="[% nutriment.value %]" [% nutriment.disabled %] autocomplete="off"/>
[% IF nutriment.nid == 'saturated-fat' %]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was not specific enough, but the warnings we have in mind are more things like the warnings we have in https://github.com/openfoodfacts/openfoodfacts-server/blob/main/lib/ProductOpener/DataQualityFood.pm#L631

e.g. if the nutrients are per 100g, then we should not have any value greater than 100.

same for sub values: sugars should not be higher than carbohydrates, and saturated fats should not be higher than fat.

Maybe we could start with only those 2 checks, and then gradually add more.

@CharlesNepote
Copy link
Member

Thanks a lot @jnsereko. The video is nice to clearly understand how it works.

About the user interface: I'm afraid that the way it is displayed will have bad side effects:

  1. Displaying a red message could lead the user to abort. Many people do, when seeing a red alert.
  2. The column is taking too much place: it will make the side image far smaller and could lead to image readability issues.

I suggest two points:

  1. When the error occurs, colorize the field background (with a soft color, not too flashy).
  2. When the error occurs, display a simple (?) (a question mark inside a circle, I guess you can do it in CSS), and a text displayed when the mouse is going over it (in Power User Script I use the title HTML attribute, no need to use javascript).

Here is an example I use in Power User Script (but the background color here is too flashy):
image

I'm not sure if the color should be displayed while typing or when the user is leaving the field for another one.

@jnsereko jnsereko force-pushed the 1905 branch 4 times, most recently from ea935b4 to 4ff4132 Compare April 24, 2023 22:19
@jnsereko
Copy link
Contributor Author

hello @stephanegigandet @CharlesNepote @yuktea @alexgarel
Sorry this has taken some time.

The current push has some changes attending to some of @stephanegigandet and @CharlesNepote suggestions.
ie

When the error occurs, colorize the field background (with a soft color, not too flashy).

When the error occurs, display a simple (?) (a question mark inside a circle, I guess you can do it in CSS), and a text displayed when the mouse is going over it (in Power User Script I use the title HTML attribute, no need to use javascript).

if the nutrients are per 100g, then we should not have any value greater than 100.

same for sub values: sugars should not be higher than carbohydrates, and saturated fats should not be higher than fat.

The current video in the PR description shows a visual result.

@jnsereko jnsereko force-pushed the 1905 branch 2 times, most recently from f08256e to 085445f Compare April 25, 2023 09:15
@jnsereko jnsereko changed the title Prevention - On the fly quality checks in the product edit form feat: Prevention - On the fly quality checks in the product edit form Apr 25, 2023
@stephanegigandet
Copy link
Contributor

Hi @jnsereko , this looks quite good, in terms of functionality and how to display the warning, it's great I think.

var saturated_fats_value;
var fat_value;

[% FOREACH nutriment IN nutriments %]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but it will generate a huge JS file, with the code repeated for all nutrients.

It would be better to create a javascript array that contains all nutrients, and then have a for loop in Javascript, instead of a foreach loop in the template language.

@CharlesNepote
Copy link
Member

Well done @jnsereko! The video looks really cool. Last point: I would choose a lighter color to highlight errors, not to frighten people. The yellow is a bit flashy...

@stephanegigandet would it be possible to test it on openfoodfacts.net?

@jnsereko
Copy link
Contributor Author

Well done @jnsereko! The video looks really cool. Last point: I would choose a lighter color to highlight errors, not to frighten people. The yellow is a bit flashy...

@stephanegigandet would it be possible to test it on openfoodfacts.net?

hey @CharlesNepote
Would pale pink work better?
Screenshot 2023-04-27 at 02 39 34


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

required_nutrients_id.forEach(nutirent_id => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanegigandet, using the ordinary for loop is tricky. Only the last nutrient, "alcohol" is accessed the colour but the on-input event exists on other elements

Is this okay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good this way, thank you! :)

Could you just replace all the "nutirent" by "nutrient" ? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @stephanegigandet
That slipped my eyes

@jnsereko
Copy link
Contributor Author

jnsereko commented May 2, 2023

hey @stephanegigandet @alexgarel
All tests are passing now 😄

@jnsereko jnsereko requested a review from stephanegigandet May 2, 2023 10:15
@stephanegigandet
Copy link
Contributor

@jnsereko Thank you!

\$('#nutriment_' + nutrient_id).on('input', function() {
var nutrient_value = \$(this).val();
var is_above_or_below_100 = isNaN(nutrient_value) || nutrient_value < 0 || nutrient_value > 100;
show_warning(is_above_or_below_100, nutrient_id, "Please enter a value between 0 and 100");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warnings should be added to po/common/common.pot and po/common/en.po so they can be translated, but we can also do that later in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanegigandet can we do the translations in another PR
i have tried them locally but it might take me some time to make the logic work.

# product_non_normalized_code moved
"Moved 0000012345678 to 12345678",
# removed product with existing id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bug that is not related to your PR, could you remove this change?
The bug is fixed in this PR: #8384

@sonarqubecloud
Copy link

sonarqubecloud bot commented May 2, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@stephanegigandet stephanegigandet merged commit 89da8ff into openfoodfacts:main May 3, 2023
@CharlesNepote
Copy link
Member

The question mark should not appear when there is a "hyphen", meaning this data is not displayed on the packaging. It's important.
Should I open another bug or not?

See:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Editing 🧽 on-the-fly quality checks Template::Toolkit The templating toolkit used by product opener. The starting point for HTML/JS/CSS fixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevention - On the fly quality checks in the product edit form (javascript)
4 participants