-
Notifications
You must be signed in to change notification settings - Fork 5
[FSSS-180] Fix: facets not being selected #380
Conversation
✔️ Deploy Preview for basestore ready! 🔨 Explore the source changes: 9371abb 🔍 Inspect the deploy log: https://app.netlify.com/sites/basestore/deploys/622b5feaa9db3f0009a62aa9 😎 Browse the preview: https://deploy-preview-380--basestore.netlify.app |
Preview is readyThis pull request generated a Preview👀 Preview: https://preview-380--base.preview.vtex.app |
Gatsby Cloud Build Reportbasestore 🎉 Your build was successful! See the Deploy preview here. Build Details🕐 Build time: 7m PerformanceLighthouse report
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome PR description! It guided me commit-by-commit through the changes.
🤩
I tested on my Android and also confirmed the bug is now gone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent context description! 💯
I tested it again to force the error but everything seems fine now!
What's the purpose of this pull request?
While using the mobile version of the filter on PLP, sometimes the facets were not being applied as we can see below:
CleanShot.2022-03-09.at.15.44.22.mp4
So, this PR aims to look into this problem and fix it.
How does it work?
The
Filter
on the mobile version works in a different way than the desktop version.While on the desktop the result is shown just after the user clicks the
Facet's Checkboxes
, performing thetoggleFacet
(singular, so one facet by time) function from theuseSearch
hook (@faststore/sdk
).On the mobile version, the
Filter
opens inside aSlideOver
component and the user first picks up the facets (onFacetChange
function) and so hits the ApplyButton
(onApply
function). Thus, we need to investigate these 2 functions.The
onFacetChange
aims to keep the actual state once the user selects/unselects the checkboxes.Previously 2 states were being used:
selectedFacets
andfacetsToRemove
. But keeping thefacetsToRemove
can be a little tricky. Once the facet is already selected and the user unselects it, the facet was being added to thefacetsToRemove
array. But the opposite situation, when the facet was unselected and the user selects it again, thefacetsToRemove
state wasn't updated.If the user selects and unselects the same facet multiple times without hitting the Apply
Button
, the finalfacetsToRemove
array is gonna have the same facet multiples times. Making the end result messy afterward on theonApply
function.Furthermore, we don't need to keep the
facetsToRemove
state, once we have the original statesearchState.selectedFacets
and the actual stateselectedFacets
, we can get which facets were added and removed comparing these 2 arrays against each other.So, the first task was remove the
facetsToRemove
state.I took advantage that I was going to change this function and refactor it a little bit trying to improve the readability and performance. Besides, I changed the
setSelectedFacets
to the functional update way, recommended by React doc when we depend on previous values.Another problem is the referential equality while using the
searchState.selectedFacets
from the@faststore/sdk
to set the initial value of theselectedFacets
and also every time we open the filter. The first time wesetSelectedFacets
ononFacetChange
function was also changing thesearchState.selectedFacets
. To fix it we can use a new array spreading thesearchState.selectedFacets
values.Finally, on the
onApply
function, we need to use thetoggleFacets
(plural, multiple facets) function from the@faststore/sdk
to toggle all facets changed (added and removed). Now that we clean the trickyfacetsToRemove
, that was duplicating facets and making the messy. We need to compare the original statesearchState.selectedFacets
and the actual stateselectedFacets
as mentioned before to get the Facets added and removed (like an XOR operation) and passing the results to thetoggleFacets
(task done here).Result:
Screen.Recording.2022-03-10.at.15.55.27.mov
How to test it?
You can open the preview and double-check if the filter works as expected while using the mobile version.
Checklist