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

Fix problem with item name dropdown selection #4797

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

coalest
Copy link
Collaborator

@coalest coalest commented Nov 21, 2024

Resolves #4547

Description

This issue occurs when an ajax call to populate the item names in the dropdown options occurs while the dropdown has been opened. Selecting an option doesn't work because that option no longer exists, so nothing happens (so item selected in the field will be the first item from the list of new options).

To fix this I made two changes:

  1. Added an event listener so that whenever a item name dropdown option is selected, if the id of that item matches in the list of new options, select that option. (Note: This is enough to fix the bug)
  2. But during investigation I found that when adding a new item, two ajax calls are fired off (to /inventory.json) every time you click "Add Another Item". And an ajax call is made to fetch the inventory whenever the storage location changes. I'm assuming it's not needed to fetch the inventory so often. So I changed it to only fetch the inventory item names/quantities on storage location field change, then we store those dropdown options and reuse them whenever a new line item is added.

This will make adding new line items more responsive, and with less jank (from the reduced number of ajax calls) (ie. the item name changing from "XL Diapers" to "XL Diapers (1353)").

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Tested manually in Chrome on my local machine, using the reproduction steps provided here.

Testing has been tough. I see a similar issue to the one I'm having where the inputevent isn't being triggered by cuprite. In my case the select2:select event isn't being triggered for some reason.

@cielf cielf requested a review from dorner November 21, 2024 17:25
@dorner
Copy link
Collaborator

dorner commented Nov 22, 2024

Looks great on the technical side - @cielf did you want to test this out manually?

@coalest coalest changed the title 4547: Fix problem with item name dropdown selection WIP: 4547: Fix problem with item name dropdown selection Nov 22, 2024
@coalest
Copy link
Collaborator Author

coalest commented Nov 22, 2024

Actually, I might I have a better way to resolve this issue. Instead of using an the select2:select event listener to detect when an option has been selected and match it to the new option, I think we can might be able change the options in the open dropdown as well. Need to test it out though.

@cielf
Copy link
Collaborator

cielf commented Nov 22, 2024

I would - but now I'll wait.

@coalest coalest force-pushed the 4547-item-dropdown-problem-fix branch from e5203c1 to e913553 Compare November 24, 2024 14:24
@coalest coalest force-pushed the 4547-item-dropdown-problem-fix branch from e858028 to 71698f4 Compare November 24, 2024 19:42
@coalest
Copy link
Collaborator Author

coalest commented Nov 25, 2024

So I tried to update the opened dropdown options, but I couldn't find a way to do so using select2's api. I tested out a hacky way (closing and opening the dropdown to refresh), and it kinda works but is a little glitchy if the user is currently scrolling or typing something in dropdown.

In summary, we can fix the bug by matching the options (option 1), or by rerendering the options with some jankiness (option 2).

Here are two videos showing the difference from a user standpoint. I used Chrome and changed the network setting to have 3 seconds of latency.

Video of what option 1 looks like (commit 71698f4):
I click "Kids (Size 6)" and it becomes "Kids (Size 6) (1986)".

Screencast.from.2024-11-24.19-35-28.webm

Video of what option 2 looks like (commit bdd3a6a):
Quantities are added dynamically to the dropdown with a bit of jank.

Screencast.from.2024-11-24.19-37-16.webm

@dorner @cielf Do y'all have a preference from a functional standpoint? (Keep in mind I do think this issue will be less common going forward with the change I made to only fetch inventory quantities on storage location change not when adding a new item. So either way, this should occur less often.)

P.S. Sorry for the force pushing, I had an annoying issue with Rails serving stale JS assets and while debugging it I made some weird commits that I cleaned up.

@coalest coalest changed the title WIP: 4547: Fix problem with item name dropdown selection Fix problem with item name dropdown selection Nov 25, 2024
@cielf
Copy link
Collaborator

cielf commented Nov 25, 2024

I think it's more smooth for the users to see the number of items available as they are picking them, so option # 2, please.

@coalest
Copy link
Collaborator Author

coalest commented Nov 26, 2024

Updated to use the workaround for option 2. This is now ready for functional and technical review

@cielf
Copy link
Collaborator

cielf commented Nov 26, 2024

LGTM from functional pov

@dorner
Copy link
Collaborator

dorner commented Nov 27, 2024

@cielf FYI - the changes in this PR basically completely rewrite the distributions/transfers dropdown logic. Might be worth some extra cycles to regression test it before merging.

@cielf
Copy link
Collaborator

cielf commented Nov 27, 2024

Agreed. Thanks for pointing that out.

@coalest
Copy link
Collaborator Author

coalest commented Nov 28, 2024

@dorner If we want to play it safe for production, I can split this into two PRs (one to refresh dropdowns and fix the bug and one with the refactoring).

@cielf cielf closed this Nov 29, 2024
@cielf cielf reopened this Nov 29, 2024
@cielf cielf self-requested a review November 29, 2024 14:01
Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

Mostly working like a charm -checked transfers, distributions, and inventory adjustments but found one thing that needs to be addressed on the sequence for distributions made from requests, if you have an error.

Here's the sequence:
sign in as [email protected]
click on Requests
click on view
click Fulfill request
Pick a storage location
Make one of the items have a bigger quantity than is allowed (i.e. we are forcing an error)
Save
Yes, it's correct

The quantities for the items disappear.

@coalest
Copy link
Collaborator Author

coalest commented Nov 29, 2024

@cielf Good catch. I didn't think about the pages with turbo streams, those aren't triggering the Javascript that is supposed to execute on page load.

The same thing happens when saving a new distribution with errors. (Distributions index => New distribution => Select storage location => Save with an error => Quantities disappear)

Does this work currently in production? I feel like this issue should be occurring on the main branch as well.

@cielf
Copy link
Collaborator

cielf commented Nov 29, 2024

@coalest Yup -- confirmed It's happening on staging. Do you think you can fix it within the bounds of this PR, or do we need to split it up?

@coalest
Copy link
Collaborator Author

coalest commented Nov 30, 2024

@cielf I just pushed a commit on this branch that should fix the issue.

Ideally, I would like to refactor things a little bit. I don't like the current flow on some pages. Currently (like when we get a request to show the page to edit a distribution), we have access to what the dropdown options should be on the backend if there is a storage location selected (item names + quantities). But instead of providing the HTML with those dropdown options included, we send back default options (with missing quantities) and an ajax request gets triggered via javascript to update labels.

Not a big deal, but I think it could be improved. But I think that could be a future PR.

@coalest coalest requested a review from cielf November 30, 2024 20:57
@cielf
Copy link
Collaborator

cielf commented Nov 30, 2024

@dorner I'm taking this out for a spin, but could you do another technical pass in parallel? Thx.

Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@dorner
Copy link
Collaborator

dorner commented Dec 1, 2024

@coalest if we can separate out the behavior changes from the refactor, I'd much rather do that.

content += "</option>\n";
return content;
function newOption(item, selectedValue, includeQuantity) {
const text = includeQuantity ? item.item_name + ` (${item.quantity})` : item.item_name;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The middle clause looks a bit weird - I'd expect to see `${item.item_name} (${item.quantity})`

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Very true, not sure what happened there. Will fix.

@coalest
Copy link
Collaborator Author

coalest commented Dec 1, 2024

@dorner @cielf Reverted all refactoring out of this PR, leaving only the bug fix. Ready for review 🙏

@@ -699,6 +699,34 @@
end
end

context "with dropdown options changed while open", skip: "select2 not working correctly with cuprite" do
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Really don't understand why I can't get this system spec to work. These steps work in a browser locally. I notice some weird behavior with select2 though. It seems like those events aren't being triggered in cuprite.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I should probably just delete this?

@coalest
Copy link
Collaborator Author

coalest commented Dec 1, 2024

Tests aren't passing due to a flaky test. Here is a PR to fix that: #4816

@coalest coalest requested review from cielf and dorner December 1, 2024 12:57
Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

Huh. I could have sworn this was working earlier -- but now it isn't, with the case of fulfilling a request, picking a storage location, putting a riduculously high number in a quantity, saving, confirming, Comes back with an error as expected, but the inventory values do not appear.

Same with the case of an error on a new distribution.

@coalest
Copy link
Collaborator Author

coalest commented Dec 2, 2024

@cielf Yea, I took that fix out because I thought we were trying to keep each PR small. so this is now just a PR to fix #4547. Not anything else.

I will add that back in.

@coalest
Copy link
Collaborator Author

coalest commented Dec 2, 2024

@cielf I put that change back in. So it's ready for review again. Sorry for the confusion.

@coalest coalest requested a review from cielf December 2, 2024 19:46
@cielf
Copy link
Collaborator

cielf commented Dec 2, 2024

(nods) it's still part of fixing the item dropdown selection, imo. I can see where it might not have been clear where to draw the line, though.

Copy link
Collaborator

@cielf cielf left a comment

Choose a reason for hiding this comment

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

LGTM from a functional pov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Problem with item dropdowns
3 participants