Skip to content

Commit

Permalink
fix(quantic): fix unexpected back browser button behaviour with stand…
Browse files Browse the repository at this point in the history
…alone search box (#4077)

## [SFINT-5539](https://coveord.atlassian.net/browse/SFINT-5539)

There were mainly two issues(issues recorded from the staging package): 

## Issue 01:

When doing an empty search from the standalone search box, clicking the
back button one time would not take you to the home page:


https://github.com/coveo/ui-kit/assets/86681870/08461748-bd25-4f0b-8fac-d9592c9f6b54

To solve this issue, I'm simply making the standalone url redirect to
`redirectUrl` instead of `redirectUrl?#={query}` when the query is
empty.


https://github.com/coveo/ui-kit/assets/86681870/e774ffb7-8d12-4415-b208-36f771174269




## Issue 02:

After doing multiple queries and clicking the back button several times,
there re situations where SF will disconnect the component and connect
it again, disconnecting the component causes the removal for the event
listener of hash change which makes the component completely loose
control of the changes happening in the URL:
 

https://github.com/coveo/ui-kit/assets/86681870/0d0a5ebf-10f3-4409-a4ae-5adba07e186b

To solve this issue, I'm setting `this.initialized` to `false` in the
`QuanticSearchInterface` component to make sure we redo the
initialization when the component disconnects and reconnect again, the
reinitialization would allow adding back the event listener to hash
changes so our component will not loose track.
Additionally when the component get's connected after being disconnected
I'm calling the `synchronize` method to make sure the search parameters
are always up to date with what we have in the hash.



https://github.com/coveo/ui-kit/assets/86681870/7a0ef4f5-4aef-40d8-b945-493f7151a061


### Try it yourself:
https://momentum-force-3625-dev-ed.scratch.my.site.com/s/


[SFINT-5539]:
https://coveord.atlassian.net/browse/SFINT-5539?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

---------

Co-authored-by: Sylvie Allain <[email protected]>
  • Loading branch information
mmitiche and sallainCoveo authored Jun 12, 2024
1 parent 51848ee commit afa8303
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export default class QuanticSearchInterface extends LightningElement {
}

disconnectedCallback() {
this.initialized = false;
this.unsubscribeUrlManager?.();
window.removeEventListener('hashchange', this.onHashChange);
if (this.ariaLiveEventsBound) {
Expand Down Expand Up @@ -174,6 +175,13 @@ export default class QuanticSearchInterface extends LightningElement {
this.urlManager = CoveoHeadless.buildUrlManager(engine, {
initialState: {fragment: this.fragment},
});

const isFirstSearchExecuted = engine.state.search.response.searchUid !== '';
if (isFirstSearchExecuted) {
// Make sure to re-synchronize the search interface when the component gets disconnected and reconnected again.
this.urlManager.synchronize(this.fragment);
}

this.unsubscribeUrlManager = this.urlManager.subscribe(() =>
this.updateHash()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export default class QuanticStandaloneSearchBox extends NavigationMixin(
{
type: 'standard__webPage',
attributes: {
url: `${this.redirectUrl}#q=${encodeURIComponent(value)}`,
url: `${this.redirectUrl}${value ? `#q=${encodeURIComponent(value)}` : ''}`,
},
},
false
Expand Down

0 comments on commit afa8303

Please sign in to comment.