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

[BUU] Fixes Products Page ActionView::Template::Error #12710

Conversation

chahmedejaz
Copy link
Collaborator

@chahmedejaz chahmedejaz commented Jul 25, 2024

What? Why?

What should we test?

  • Visit the Products page, and make sure that some product variants have nil as their unit_value. i.e. very old products, created at the time when we didn't have validation for unit_value
  • You should be able to view the page without any errors.

Release notes

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

@chahmedejaz chahmedejaz changed the title [BUU] - Fixes Products Page Error [BUU] - Fixes Products Page ActionView::Template::Error Jul 25, 2024
@chahmedejaz chahmedejaz added the bug-s3 The bug is stopping a critical or non-critical feature but there is a usable workaround. label Jul 25, 2024
@chahmedejaz chahmedejaz marked this pull request as ready for review July 25, 2024 18:58
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍
However I am wondering if we should show a blank value when unit_value and unit_description are nil ? @openfoodfoundation/core-devs ?

@dacook
Copy link
Member

dacook commented Jul 29, 2024

That sounds logical to me, I forget if there's a reason we didn't before.
I guess with this current method, any time you save the record, it will get updated to 1 without the user necessarily noticing. Essentially it defaults to 1, which might not be correct for that product.

So if we let it be blank instead, then when you save you should get an error message. Then the user can choose the appropriate number. I think that would be better, if the interface can support it. @chahmedejaz would you be able to test it out and see what works best?

@chahmedejaz
Copy link
Collaborator Author

That sounds logical to me, I forget if there's a reason we didn't before. I guess with this current method, any time you save the record, it will get updated to 1 without the user necessarily noticing. Essentially it defaults to 1, which might not be correct for that product.

So if we let it be blank instead, then when you save you should get an error message. Then the user can choose the appropriate number. I think that would be better, if the interface can support it. would you be able to test it out and see what works best?

Sure, David. Let me do a spike on it and get back to you. Thanks

@dacook
Copy link
Member

dacook commented Jul 30, 2024

Hi @chahmedejaz , sorry I didn't have an answer before.

I think it's helpful to remember this is a rare case and considered corrupt data. The error above ☝️ means the whole page fails to load which is very disruptive. But if cloning a corrupt product fails, I think that's much less disruptive and easy to work around, so we don't need to worry about solving it now. (the user can just manually create a new product with the same details)

@chahmedejaz
Copy link
Collaborator Author

chahmedejaz commented Jul 30, 2024

Hi @chahmedejaz , sorry I didn't have an answer before.

I think it's helpful to remember this is a rare case and considered corrupt data. The error above ☝️ means the whole page fails to load which is very disruptive. But if cloning a corrupt product fails, I think that's much less disruptive and easy to work around, so we don't need to worry about solving it now. (the user can just manually create a new product with the same details)

Thanks @dacook - Sounds good 👍

- if unit_value is not present and unit_description then display unit_description only.
- if both are not present then display empty fields
@@ -11,19 +11,20 @@ def product_image_form_path(product)
end

def prepare_new_variant(product)
product.variants.build do |variant|
variant.unit_value = 1.0 * (product.variant_unit_scale || 1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mimicking old UI behavior, when creating a new variant, then both of these are empty

@@ -18,7 +18,7 @@
= f.hidden_field :unit_value
= f.hidden_field :unit_description
= f.text_field :unit_value_with_description,
value: unit_value_with_description(variant), 'aria-label': t('admin.products_page.columns.unit_value'), required: true
value: unit_value_with_description(variant), 'aria-label': t('admin.products_page.columns.unit_value')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Having required here is causing an error upon saving in the following scenario:

  • New varaint button is clicked to create a new variant
  • As per Old UI behavior, unit_value is empty for this product.
  • However, in the new UI, if this variant is changed then the saving form doesn't work because this field is hidden on the UI until its pop-up is opened
  • JS tries to find it to show the required validation error but gets broken because it's not able to find it on the UI
  • Hence neither the required error gets displayed nor the form gets submitted.
  • This PR removes the required validation which won't cause the failure of the above use-case however, the backend is handling this blank field error, so it will return the erorr "can't be blank" which will be displayed below it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

JS tries to find it to show the required validation error but gets broken because it's not able to find it on the UI

This validation is handled by the browser, probably in Javascript. I think that's a fair trade off, I am wondering if we could show the pop up with error on form submit so we can still benefit from the browser validation but it's probably not worth the effort.

Copy link
Collaborator

Choose a reason for hiding this comment

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

FYI: I ran into the same issue while working on the Product Refactor and I managed to implement the solution I was talking about above. It ended up pretty simple in the end.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for implementing this one. ♥️
I tried to implement the above, maybe I was looking at it from a different POV and was facing issues. Looking forward to your solution to this in the refactor 😄

Copy link
Member

Choose a reason for hiding this comment

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

The required JS validation was added after discussing with Mario. It ensures that the popout can't be closed if it's empty, which helps avoid the problem being hidden.
But as you've found, we have a problem if the field is empty and the popout never gets opened.

This change seems like the most practical best solution 👍
I'm not sure if the red border will show on the popout, until you open it. But if there error text is there, I think that's ok.

@@ -58,9 +58,6 @@
end

# Unit popout
fill_in "Unit value", with: ""
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As we don't have required validation as per this, so we can remove its expectation

Copy link
Member

Choose a reason for hiding this comment

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

@chahmedejaz
Copy link
Collaborator Author

@dacook @rioug - I've addressed your comments above, please review.
Here are the changes:
1014a50
d4e0b2a

Thanks

@rioug rioug added the feature toggled These pull requests' changes are invisible by default and are grouped in release notes label Aug 5, 2024
Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Looks good 👍 Thanks @chahmedejaz

@@ -11,19 +11,20 @@ def product_image_form_path(product)
end

def prepare_new_variant(product)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We could remove the helper has it's not doing much now, but that's fine as it is.

@@ -18,7 +18,7 @@
= f.hidden_field :unit_value
= f.hidden_field :unit_description
= f.text_field :unit_value_with_description,
value: unit_value_with_description(variant), 'aria-label': t('admin.products_page.columns.unit_value'), required: true
value: unit_value_with_description(variant), 'aria-label': t('admin.products_page.columns.unit_value')
Copy link
Member

Choose a reason for hiding this comment

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

The required JS validation was added after discussing with Mario. It ensures that the popout can't be closed if it's empty, which helps avoid the problem being hidden.
But as you've found, we have a problem if the field is empty and the popout never gets opened.

This change seems like the most practical best solution 👍
I'm not sure if the red border will show on the popout, until you open it. But if there error text is there, I think that's ok.

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Actually sorry I just remembered that we are losing test coverage here. I think the removed test can still be covered in a different way:

@@ -58,9 +58,6 @@
end

# Unit popout
fill_in "Unit value", with: ""
Copy link
Member

Choose a reason for hiding this comment

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

@chahmedejaz
Copy link
Collaborator Author

Can the case of blank unit value now be added here?

https://github.com/openfoodfoundation/openfoodnetwork/blob/master/spec/system/admin/products_v3/update_spec.rb#L251

Sure, David.
Sorry, I thought the blank error functionality is already tested in the above spec and the removed spec was just validating that if the pop-up remains open if the field is blank 😅
No worries, I'll it now 😄

@dacook
Copy link
Member

dacook commented Aug 6, 2024

Sure, David. Sorry, I thought the blank error functionality is already tested in the above spec and the removed spec was just validating that if the pop-up remains open if the field is blank 😅 No worries, I'll it now 😄

I think it is covered for "adding variants", but it would be worth covering for "updating">"with invalid data" also. That is, at "shows errors for both product and variant fields". I hope that makes sense.

@chahmedejaz
Copy link
Collaborator Author

Sure, David. Sorry, I thought the blank error functionality is already tested in the above spec and the removed spec was just validating that if the pop-up remains open if the field is blank 😅 No worries, I'll it now 😄

I think it is covered for "adding variants", but it would be worth covering for "updating">"with invalid data" also. That is, at "shows errors for both product and variant fields". I hope that makes sense.

Sure, David 👍🏻

@sigmundpetersen sigmundpetersen changed the title [BUU] - Fixes Products Page ActionView::Template::Error [BUU] Fixes Products Page ActionView::Template::Error Aug 7, 2024
@chahmedejaz chahmedejaz requested a review from dacook August 7, 2024 12:18
@chahmedejaz
Copy link
Collaborator Author

Can the case of blank unit value now be added here?
https://github.com/openfoodfoundation/openfoodnetwork/blob/master/spec/system/admin/products_v3/update_spec.rb#L251

Hi @dacook - It's fixed here: 647a384. Thanks.

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@filipefurtad0 filipefurtad0 self-assigned this Aug 8, 2024
@filipefurtad0 filipefurtad0 added the pr-staged-fr staging.coopcircuits.fr label Aug 8, 2024
@filipefurtad0
Copy link
Contributor

Hey @chahmedejaz ,

I've edited the DB to trigger the error, and could observer the error 500 when rendering the /products page.

After staging the PR, I can confirm the page renders, and that unit_value remains empty:

image

image

image

We should keep in mind that this PR only affects the products page and that the shopfront breaks in this case:

image

But at least the user has the possibility to access the products page and fix it - provided they can find the variant which is responsible.

I believe this is the intended scenario - the /products page does not break - I'd say this is looking good. Merging! Thanks.

@filipefurtad0 filipefurtad0 merged commit 76d874d into openfoodfoundation:master Aug 8, 2024
52 checks passed
@filipefurtad0 filipefurtad0 removed the pr-staged-fr staging.coopcircuits.fr label Aug 8, 2024
@dacook dacook added user facing changes Thes pull requests affect the user experience and removed feature toggled These pull requests' changes are invisible by default and are grouped in release notes labels Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug-s3 The bug is stopping a critical or non-critical feature but there is a usable workaround. user facing changes Thes pull requests affect the user experience
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[BUU] ActionView::Template::Error in admin/products_v3#index
4 participants