Skip to content

Display consistent results after user searches for an address#7620

Merged
eileen-nava merged 10 commits intomainfrom
em/8475-clear-search-results
Jan 18, 2023
Merged

Display consistent results after user searches for an address#7620
eileen-nava merged 10 commits intomainfrom
em/8475-clear-search-results

Conversation

@eileen-nava
Copy link
Contributor

@eileen-nava eileen-nava commented Jan 11, 2023

🎫 Ticket

LG-8475: Clear previous search results

🛠 Summary of changes

  1. If a user searches for an address that returns post office results, and then searches for invalid input that returns nothing, the first search's results won't persist.
  2. If a user searches for an address that returns post office results, and then searches for another address that returns post office results, the new address and new number of post office results will update at the same time.

📜 Testing Plan

Scenario 1

  • Search for an address that returns post office results, like 1600 Pennsylvania Ave NW. Observe the results.
  • Search for invalid input, like asdkf.
  • Observe that no results display.

Scenario 2

  • Search for an address that returns post office results, like 1600 Pennsylvania Ave NW. Observe the results.
  • Search for an address that returns a different number of post office results than the previous search, like 125 N Dallas, Mentone, TX 79754.
  • Note that the new address and new number of post office results update simultaneously.

👀 Screenshots

If relevant, include a screenshot or screen capture of the changes.

Before (Scenario 1):
8575Scenario1BeforeNoAudio.mov
After (Scenario 1):
8475Scenario1AfterNoAudio.mov
Before (Scenario 2):
8475Scenario2BeforeNoAudio.mov
After (Scenario 2):
8475Scenario2AfterNoAudio.mov

@eileen-nava eileen-nava force-pushed the em/8475-clear-search-results branch from ec263c2 to abf497b Compare January 11, 2023 16:21
@eileen-nava eileen-nava changed the title Em/8475 clear search results Display consistent results after user searches for an address Jan 11, 2023
@eileen-nava eileen-nava marked this pull request as ready for review January 11, 2023 17:16
@eileen-nava eileen-nava requested review from a team and sheldon-b January 11, 2023 17:16
}, [locationResults, foundAddress]);
!isLoading && locationResults && onFoundLocations(locationResults);
!isLoading && onFoundAddress(foundAddress);
}, [isLoading, locationResults, foundAddress]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I found it a little simpler to change it this way:

  useEffect(() => {
    onFoundLocations(locationResults);

    foundAddress && onFoundAddress(foundAddress);
  }, [locationResults]);

Copy link
Contributor

Choose a reason for hiding this comment

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

This allows consumer to deal with a null state instead of blocking communication of null state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking into this! 👍🏻

@eileen-nava
Copy link
Contributor Author

Update: I responded to outstanding feedback.

Comment on lines -189 to +192
}, [locationResults, foundAddress]);
}, [locationResults]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should foundAddress stay here? Do we want onFoundAddress to be called when foundAddress changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO, yes.

Copy link
Contributor

@NavaTim NavaTim Jan 17, 2023

Choose a reason for hiding this comment

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

@eileen-nava @allthesignals There's an exhaustive-deps rule published by Facebook that if enabled would flag this as a violation b/c foundAddress is missing from the hook dependencies.

Generally when the dependencies don't match the values used in a hook like this, it's considered a code smell in React. This is a very likely location for a bug to occur because the value is missing.

Copy link
Contributor

Choose a reason for hiding this comment

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

That’s a good rule of thumb to follow, but in this case, we don’t want it to update when either value is updated. We only want it to update when usps locations are found for a given valid address.

Copy link
Contributor

Choose a reason for hiding this comment

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

How about we split this into two effects, one for locationResults/onFoundLocations and a separate one for foundAddress/onFoundAddress?

Copy link
Contributor

Choose a reason for hiding this comment

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

I will want to check that it doesn't produce the edge case I was describing earlier. I'm also not totally sure it's worth the trade off in code complexity (is exhaustive-deps even enforced by Login.gov lint rules?)

@eileen-nava obvi it's your call on updating it or not! I know this PR has had a lot of feedback since you opened it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To second what Matt is saying, if I add foundAddress to the dependency array without adding back in some sort of conditional logic, it causes the address to update before the locations update. You can see this bug in the “before video” for scenario 2.

I'm not against adding the conditional logic back in and re-adding the foundAddress dependency to the dependency array. I’m mostly concerned about getting this PR approved and merged by sprint’s end.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ticket created@eileen-nava and I are going to look at it. Thanks Tim!

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 discussed this with Matt in Slack and I am also going to share here for posterity.

When I set the dependencies array to [locationResults, foundAddress, isLoading], I found a bug where the first search’s results would display before the second search’s results.

I poked through debugger and found that isLoading incorrectly equaled true before both the second search’s address candidates and usps locations had loaded. (I think this has to do with swr caching? More investigation is needed.) This caused the useDidUpdateEffect hook to incorrectly set the foundAddress and locationResults, even when there was a conditional like

    if (isLoading) {
      return;
    }

The plan is to
A) merge working code that doesn’t have exhaustive dependencies
B) make a ticket to satisfy the exhaustive dependencies requirement and investigate what’s going on with isLoading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about we split this into two effects, one for locationResults/onFoundLocations and a separate one for foundAddress/onFoundAddress?

When I tried that, it made the updated address display before the updated locations results. That contradicts the ticket's AC.

@allthesignals allthesignals self-requested a review January 18, 2023 15:45
Copy link
Contributor

@allthesignals allthesignals left a comment

Choose a reason for hiding this comment

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

I think it's good to go but I think we should address @NavaTim's concerns in a separate ticket

@eileen-nava eileen-nava merged commit 6118877 into main Jan 18, 2023
@eileen-nava eileen-nava deleted the em/8475-clear-search-results branch January 18, 2023 17:47
@mdiarra3 mdiarra3 mentioned this pull request Jan 23, 2023
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.

4 participants