Skip to content

Commit

Permalink
feat(frontend): update search to reset to page 1 (#3192)
Browse files Browse the repository at this point in the history
Fixes #3176

**Changes**:

- Sets the pagination to page one if the search is updated. This is the norm on most websites, and how users expect it to work.
- Join `setPage` into `updateSearchResults`
  - Take a page number in `updateSearchResults`
- Remove unused param to `updateSearchResults`
- Update `watch` to not double requests
- use `scrollToTop` utility function
  • Loading branch information
Erb3 authored Jan 29, 2025
1 parent b195536 commit 79d131c
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions apps/frontend/src/pages/search/[searchProjectType].vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
name="Sort by"
:options="sortTypes"
:display-name="(option) => option?.display"
@change="updateSearchResults(1)"
@change="updateSearchResults()"
>
<span class="font-semibold text-primary">Sort by: </span>
<span class="font-semibold text-secondary">{{ selected }}</span>
Expand All @@ -181,7 +181,7 @@
:default-value="maxResults"
:model-value="maxResults"
class="!w-auto flex-grow md:flex-grow-0"
@change="updateSearchResults(1)"
@change="updateSearchResults()"
>
<span class="font-semibold text-primary">View: </span>
<span class="font-semibold text-secondary">{{ selected }}</span>
Expand All @@ -206,7 +206,7 @@
:page="currentPage"
:count="pageCount"
class="mx-auto sm:ml-auto sm:mr-0"
@switch-page="setPage"
@switch-page="updateSearchResults"
/>
</div>
<SearchFilterControl
Expand Down Expand Up @@ -296,7 +296,7 @@
:page="currentPage"
:count="pageCount"
class="justify-end"
@switch-page="setPage"
@switch-page="updateSearchResults"
/>
</div>
</div>
Expand Down Expand Up @@ -545,19 +545,13 @@ const pageCount = computed(() =>
results.value ? Math.ceil(results.value.total_hits / results.value.limit) : 1,
);
function setPage(newPageNumber) {
currentPage.value = newPageNumber;
window.scrollTo({ top: 0, behavior: "smooth" });
updateSearchResults();
}
function scrollToTop(behavior = "smooth") {
window.scrollTo({ top: 0, behavior });
}
function updateSearchResults() {
function updateSearchResults(pageNumber) {
currentPage.value = pageNumber || 1;
scrollToTop();
noLoad.value = true;
if (query.value === null) {
Expand Down Expand Up @@ -590,8 +584,8 @@ function updateSearchResults() {
}
}
watch([currentFilters, requestParams], () => {
updateSearchResults();
watch([currentFilters], () => {
updateSearchResults(1);
});
function cycleSearchDisplayMode() {
Expand Down

0 comments on commit 79d131c

Please sign in to comment.