openfoodfacts / openfoodfacts-server

Open Food Facts database, API server and web interface - 🐪🦋 Perl, CSS and JS coders welcome 😊 For helping in Python, see Robotoff or taxonomy-editor
GNU Affero General Public License v3.0
632 stars 370 forks source link

Highlight serving size form field when it does not contain a value #8899

Open CharlesNepote opened 1 year ago

CharlesNepote commented 1 year ago

When serving size form field does not contain a value (eg. 60 g) but only text (eg. null), it would be useful to highlight the field and add an interrogation mark associated with a tooltip -- as for other fields in the nutrition facts form.

Here is what I'm going to implement in Power User Script (in vanilla JS):

    // Check serving size field
    checkServingSize(document.getElementById("serving_size").value);
    document.getElementById("serving_size").addEventListener("input", function() {
        checkServingSize(this.value);
    });

    /***
     * checkServingSize: check if serving size contains a right value; otherwise, highlight the field
     *                   and add a interrogation mark associated with a tooltip.
     *
     * @param {string} servingSize: value of the servingSize
     * @returns: none
     */
    function checkServingSize(servingSize) {
        log("serving size val: " + servingSize);
        const servingSizeElement = document.getElementById("serving_size");
        if (servingSize.length == 0) {
            servingSizeElement.style.setProperty("background-color", "LightYellow", "important");
            document.getElementById("serving_size_help") != null ? document.getElementById("serving_size_help").remove() : false;
            return;
        }
        const regex = /\d+(\.\d+)? ?(kg|g|dg|cg|mg|mcg|l|dl|cl|ml|fl|oz)(\W+.*)?$/gi; // Examples matching: 60 g, 12 oz, 20cl, 2 fl oz
        if (regex.test(servingSize) === false) {
            log("serving size is not correct!");
            servingSizeElement.style.setProperty("background-color", "orange", "important");
            servingSizeElement.style.setProperty("display", "inline");
            if(document.getElementById("serving_size_help") == null) {
                const servingSizeHelp = '<strong id="serving_size_help" href="#serving_size_help" title="Serving size needs a value. Leave it empty otherwise."> (?) <br></strong>';
                servingSizeElement.insertAdjacentHTML("afterend", servingSizeHelp);
                document.getElementById("serving_size_help").style.setProperty("cursor", "pointer");
            }
        }
        else {
            servingSizeElement.style.setProperty("background-color", "LightYellow", "important");
            document.getElementById("serving_size_help") != null ? document.getElementById("serving_size_help").remove() : false;
        }
    }
github-actions[bot] commented 9 months ago

This issue has been open 90 days with no activity. Can you give it a little love by linking it to a parent issue, adding relevant labels and projets, creating a mockup if applicable, adding code pointers from https://github.com/openfoodfacts/openfoodfacts-server/blob/main/.github/labeler.yml, giving it a priority, editing the original issue to have a more comprehensive description… Thank you very much for your contribution to 🍊 Open Food Facts

anaritadauane commented 5 months ago

I will be working on this

anaritadauane commented 5 months ago

I will be working on this

@CharlesNepote and @stephanegigandet, can you please help replicate this step?

stephanegigandet commented 5 months ago

@CharlesNepote and @stephanegigandet, can you please help replicate this step?

You can edit a product, and type "something without a number" in the "serving size" field: you won't see anything special. When there is a value that is not empty but does not contain a number, we want to display a tooltip.

anaritadauane commented 5 months ago

@CharlesNepote and @stephanegigandet, can you please help replicate this step?

You can edit a product, and type "something without a number" in the "serving size" field: you won't see anything special. When there is a value that is not empty but does not contain a number, we want to display a tooltip.

Thank you @stephanegigandet

deveshidwivedi commented 5 months ago

Hi, @anaritadauane! Could you please confirm if you are working on this currently or not? Thanks:)

anaritadauane commented 5 months ago

Hi @deveshidwivedi , I'm not working on this issue :)