Skip to content

Commit

Permalink
WEBDEV-6637 Ensure sort bar dropdowns update on default sort change (#…
Browse files Browse the repository at this point in the history
…346)

* Ensure sort bar dropdowns update on default sort change

* Only promote the default sort direction when a sort is selected

* Ensure installation state updates do not push extra history
  • Loading branch information
latonv committed Jan 31, 2024
1 parent 8e2b201 commit 556f884
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
27 changes: 23 additions & 4 deletions src/collection-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ export class CollectionBrowser
*/
private isResizeToMobile = false;

/**
* Flag indicating that a new data source is currently being installed.
* During the install, any URL persistence operation should replace the current entry
* instead of creating a new one.
*/
private dataSourceInstallInProgress = false;

private leftColIntersectionObserver?: IntersectionObserver;

private facetsIntersectionObserver?: IntersectionObserver;
Expand Down Expand Up @@ -1009,8 +1016,12 @@ export class CollectionBrowser
this.selectedTitleFilter = queryState.selectedTitleFilter;
this.selectedCreatorFilter = queryState.selectedCreatorFilter;

// We set this flag during the update to prevent the URL state persistence
// from creating an unwanted extra history entry.
this.dataSourceInstallInProgress = true;
this.requestUpdate();
await this.updateComplete;
this.dataSourceInstallInProgress = false;

if (!this.searchResultsLoading) {
this.setTotalResultCount(this.dataSource.totalResults);
Expand All @@ -1021,7 +1032,6 @@ export class CollectionBrowser
}

firstUpdated(): void {
this.setupStateRestorationObserver();
this.restoreState();
}

Expand Down Expand Up @@ -1175,6 +1185,11 @@ export class CollectionBrowser
}
}

connectedCallback(): void {
super.connectedCallback?.();
this.setupStateRestorationObserver();
}

disconnectedCallback(): void {
if (this.resizeObserver) {
this.disconnectResizeObserver(this.resizeObserver);
Expand Down Expand Up @@ -1435,8 +1450,9 @@ export class CollectionBrowser
}

private setupStateRestorationObserver() {
if (this.boundNavigationHandler) return;
this.boundNavigationHandler = this.historyNavigationHandler.bind(this);
if (!this.boundNavigationHandler) {
this.boundNavigationHandler = this.historyNavigationHandler.bind(this);
}
// when the user navigates back, we want to update the UI to match the URL
window.addEventListener('popstate', this.boundNavigationHandler);
}
Expand Down Expand Up @@ -1483,7 +1499,10 @@ export class CollectionBrowser
selectedTitleFilter: this.selectedTitleFilter ?? undefined,
selectedCreatorFilter: this.selectedCreatorFilter ?? undefined,
};
this.restorationStateHandler.persistState(restorationState);
this.restorationStateHandler.persistState(
restorationState,
this.dataSourceInstallInProgress
);
}

private emitSearchResultsLoadingChanged(): void {
Expand Down
15 changes: 10 additions & 5 deletions src/restoration-state-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface RestorationState {
}

export interface RestorationStateHandlerInterface {
persistState(state: RestorationState): void;
persistState(state: RestorationState, forceReplace?: boolean): void;
getRestorationState(): RestorationState;
}

Expand All @@ -50,9 +50,9 @@ export class RestorationStateHandler
this.context = options.context;
}

persistState(state: RestorationState): void {
persistState(state: RestorationState, forceReplace = false): void {
if (state.displayMode) this.persistViewStateToCookies(state.displayMode);
this.persistQueryStateToUrl(state);
this.persistQueryStateToUrl(state, forceReplace);
}

getRestorationState(): RestorationState {
Expand Down Expand Up @@ -85,7 +85,10 @@ export class RestorationStateHandler
return 'list-compact';
}

private persistQueryStateToUrl(state: RestorationState) {
private persistQueryStateToUrl(
state: RestorationState,
forceReplace = false
) {
const url = new URL(window.location.href);
const oldParams = new URLSearchParams(url.searchParams);
const newParams = this.removeRecognizedParams(url.searchParams);
Expand Down Expand Up @@ -176,7 +179,9 @@ export class RestorationStateHandler
// - If the state has changed, we push a new history entry.
// - If only the page number has changed, we replace the current history entry.
// - If the state hasn't changed, then do nothing.
let historyMethod: 'pushState' | 'replaceState' = 'pushState';
let historyMethod: 'pushState' | 'replaceState' = forceReplace
? 'replaceState'
: 'pushState';
const nonQueryParamsMatch = this.paramsMatch(oldParams, newParams, [
'sin',
'sort',
Expand Down
10 changes: 8 additions & 2 deletions src/sort-filter-bar/sort-filter-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ export class SortFilterBar
}

willUpdate(changed: PropertyValues) {
if (changed.has('selectedSort')) {
if (this.sortDirection === null) {
if (changed.has('selectedSort') || changed.has('defaultSortField')) {
// If the sort is changed from its default without a direction set,
// we adopt the default sort direction for that sort type.
if (
this.selectedSort &&
this.selectedSort !== SortField.default &&
this.sortDirection === null
) {
const sortOption = SORT_OPTIONS[this.finalizedSortField];
this.sortDirection = sortOption.defaultSortDirection;
}
Expand Down

0 comments on commit 556f884

Please sign in to comment.