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

Keep stock selection when error on saving #12621

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

cyrillefr
Copy link
Contributor

What? Why?

What should we test?

  • Feature Admin V3 toggled on
  • Visit /admin/products
  • Add a variant ( + sign) to a product
  • Do not fill anything, except the On Hand part
    • click on checkbox On demand
    • or choose a number
  • Click Save changes
  • Page should return in error mode with a cannot be blank in the Unit column
  • But the status for the On Hand part should keep what was entered at first.

How I did it

I added 2 not to be persisted attributes aimed at dealing with the UI. Their purpose is to be passed and passed by in case of validation errors from and to the UI.
Therefore, there is no interference with the current fields and logic.

In the view: I added some kind of "mode": when variant exists on DB or not.
So instead of on_hand/on_demand methods passed in form helpers, it will be now either:

  • on_hand when variant exists (current only case)
  • on_hand_desired for a new variant

So that, in variant, one can get values from the ui whether or not the variant exists or not.
In case validation fails, the value for on_hand/on_demand are stored and are passed by to the ui in the variant object.
If everything fine, then variant is persisted in DB.

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

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@mkllnk mkllnk added the feature toggled These pull requests' changes are invisible by default and are grouped in release notes label Jun 27, 2024
Comment on lines 158 to 168
variant.on_demand = on_demand if on_demand.present?
variant.on_demand = variant.on_demand_desired if variant.on_demand_desired.present?
variant.on_hand = on_hand.to_i if on_hand.present?
variant.on_hand = variant.on_hand_desired.to_i if variant.on_hand_desired.present?
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me which value is used in which case here. Looking only at this logic, the stock could even be set twice when updated. So first it would set the old on_hand value when it's present and then set the new desired value. This is a bit inefficient but also confusing to follow.

I think that it would be clearer to have one branch of logic:

  • Form always sets the desired value.
  • We always use the desired value to update stock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was a bit cluttered. After a few trials (and errors), I think this is better now.
The conditional I added is because in legacy mode, you enter the create variant with on Hand/on demand whereas in v3 you do not.

@@ -30,14 +31,14 @@
= error_message_on variant, :price
%td.col-on_hand.field.popout{'data-controller': "popout"}
%button.popout__button{'data-popout-target': "button", 'aria-label': t('admin.products_page.columns.on_hand')}
= variant.on_demand ? t(:on_demand) : variant.on_hand
= variant.send(method_on_demand) ? t(:on_demand) : variant.send(method_on_hand)
Copy link
Member

Choose a reason for hiding this comment

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

Using send is a sign that there's an issue with the code design. I think that it would be better to implement the reader on the model to read the value from the database if the desired value isn't set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added 2 methods in variant_stock to choose from which field to read. I have applied the same logic as in the view.

Comment on lines 937 to 965
it 'displays the correct value afterwards for On Hand' do
within new_variant_row do
fill_in "Name", with: "Large box"
click_on "On Hand"
fill_in "On Hand", with: "19"
end

click_button "Save changes"

expect(page).to have_content "Please review the errors and try again"
within row_containing_name("Large box") do
expect(page).to have_content "19"
end
end

it 'displays the correct value afterwards for On demand' do
within new_variant_row do
fill_in "Name", with: "Large box"
click_on "On Hand"
check "On demand"
end

click_button "Save changes"

expect(page).to have_content "Please review the errors and try again"
within row_containing_name("Large box") do
expect(page).to have_content "On demand"
end
end
Copy link
Member

Choose a reason for hiding this comment

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

Great to have this tested! 💯

Since system specs are slow, I would combine these two examples. You can fill both values, hit save once and then assert on both values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have refactored the spec in this way :)

@cyrillefr cyrillefr force-pushed the KeepStockSelectionWhenErrorOnSaving branch from 6bc72f9 to 32b386e Compare June 28, 2024 15:46
@cyrillefr cyrillefr closed this Dec 20, 2024
@cyrillefr cyrillefr deleted the KeepStockSelectionWhenErrorOnSaving branch December 20, 2024 19:17
@sigmundpetersen
Copy link
Contributor

Hi all.
Why was this one closed? I can't find a mention of that in the issue or pr.
Seems the changes were ready for another round of review, but that the pr was left hanging in the 'In progress' column.

@openfoodfoundation/reviewers should we reopen and have another look at it?
Or do you think it should stay closed?

@dacook dacook requested review from mkllnk and dacook January 7, 2025 04:24
Copy link
Member

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

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

Much better!

It's still a little bit cumbersome but that's worth fixing the bug. Thank you!

I'm sorry that I missed your updates. Can you please re-open? I would approve this as is.

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.

I'm guessing the new, separate "desired" attributes are to reduce risk. It's a shame it makes it pretty complicated, but it protects against creating new stock issues. I probably would have chosen a different name for these temporary variables, but don't have any better ideas.

I have a few other thoughts on how this could be organised, because as Maikel says it's pretty cumbersome.

But it would be great to fix the bug 👍

= f.label :on_demand do
= f.check_box :on_demand, 'data-action': 'change->toggle-control#disableIfPresent change->popout#closeIfChecked'
= f.label method_on_demand do
= f.check_box method_on_demand, 'data-action': 'change->toggle-control#disableIfPresent change->popout#closeIfChecked'
Copy link
Member

Choose a reason for hiding this comment

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

I wish there was a better way to manage these two cases. If we need to add this complexity, the on_hand/on_demand field might deserve to have its own partial.


on_hand
end

Copy link
Member

Choose a reason for hiding this comment

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

These are arguably view-specific logic, so perhaps these could be moved to view helpers. Also method_on_demand.

@cyrillefr
Copy link
Contributor Author

Hello @mkllnk , @dacook , @sigmundpetersen

Sorry for the PR closing. I did some local project cleaning a fee weeks ago and I guess by deleting a local branch, it automatically closed this PR. It was not my intention. I must have made some bad filtering and did not see the corresponding issue was still open ....

Do you want me to re open this PR ? (I guess i can do it by restoring the deleted branch).

@sigmundpetersen
Copy link
Contributor

Do you want me to re open this PR ? (I guess i can do it by restoring the deleted branch).

Yes, thank you @cyrillefr . Maybe we could get the fix merged, that would be nice 👍

@cyrillefr cyrillefr restored the KeepStockSelectionWhenErrorOnSaving branch February 3, 2025 14:41
@cyrillefr cyrillefr reopened this Feb 3, 2025
@rioug
Copy link
Collaborator

rioug commented Feb 3, 2025

@cyrillefr If you are not on it already, could you address the conflict ?

- added 2 not to be persisted attributes aimed at dealing with the UI
- added them to the permitted list
- updated view to switch mode about on_hand/on_demand
  that is: from an already persisted variant or not
    - Not persisted deals with on_*_desired not to be persisted fields
    - Persisted mode deals with regular on_* fields
- the corresponding spec for both on_hand/on_demand
- 2 new methods for reading either current/desired on hand/on demand
  depending on variant state. Goal is to get rid of send method in View
- referring in on_hand/on_demand is in fact irrelevant. In the piece of
  code, only desired on_hand/on_demand can be called as we are only in
  new variant (non persisted) mode
- View does not use send method anymore, replaced by current_or_desired
- refactor of the spec -> 2 examples in one to get more speed.
@cyrillefr cyrillefr force-pushed the KeepStockSelectionWhenErrorOnSaving branch from 32b386e to 75dc20c Compare February 5, 2025 14:07
@cyrillefr
Copy link
Contributor Author

Conflict is solved, but I need to fix the specs since behaviour seemed to have a bit changed since last time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature toggled These pull requests' changes are invisible by default and are grouped in release notes
Projects
Status: In Progress ⚙
Development

Successfully merging this pull request may close these issues.

[BUU] Stock settings selection is lost, if there are errors on saving
5 participants