Skip to content

Commit 685077e

Browse files
committed
trigger watches on numberPerPage & sortBy only on actual change; change response item from "search-metadata" to "searchMetadata"
1 parent 2f8bf61 commit 685077e

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/stores/cveListSearch.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
179179
let payLoad = this.createSearchPayload();
180180
const response = await axios.post(searchUrl, payLoad);
181181

182-
const searchMetaData = response.data['search-metadata'];
182+
const searchMetaData = response.data.searchMetadata;
183183

184184
if (searchMetaData.searchStatus === 'ok') {
185185
this.totalSearchResultCount = response.data.resultsTotal;
@@ -188,6 +188,11 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
188188
// There's likely something invalid in the search string.
189189
// Error message(s) and note(s) returned in the response will be
190190
// displayed to the user.
191+
//
192+
// We could reset the error message store here, because this is a
193+
// "new" search, but resetting may also mask an issue where the
194+
// search is inadvertently done multiple times (resulting in
195+
// duplicate error messages displayed).
191196

192197
const ems = useErrorMessageStore();
193198

@@ -215,7 +220,6 @@ export const useCveListSearchStore = defineStore('cveListSearch ', {
215220
relevancyScore: result?._score
216221
});
217222
});
218-
219223
}
220224

221225
this.searchResults = parsedResults;

src/views/CVERecord/SearchResults.vue

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@
125125
<label class="label">Show:</label>
126126
<div class="control">
127127
<div class="select">
128-
<select v-model="cveListSearchStore.pagination.numberPerPage" aria-label="Select how many search results to show">
128+
<select v-model="cveListSearchStore.pagination.numberPerPage"
129+
aria-label="Select how many search results to show">
129130
<option v-for="num in cveListSearchStore.pagination.numberPerPageOptions" :key="`page-${num}`" :value="num">
130131
{{ num }}
131132
</option>
@@ -135,10 +136,10 @@
135136
<label for="sort" class="label">Sort by:</label>
136137
<div class="control">
137138
<div class="select">
138-
<select name="partner" v-model="cveListSearchStore.pagination.sortBy" aria-label="Select a sort option">
139+
<select name="partner" v-model="cveListSearchStore.pagination.sortBy"
140+
aria-label="Select a sort option">
139141
<option v-for="sort in cveListSearchStore.pagination.sortByOptions" :key="sort.key"
140-
:value="sort"
141-
>
142+
:value="sort">
142143
{{ sort.label }}
143144
</option>
144145
</select>
@@ -274,7 +275,7 @@
274275
import { useCveListSearchStore } from '@/stores/cveListSearch';
275276
import { useGenericGlobalsStore } from '@/stores/genericGlobals';
276277
import { usePartnerStore } from '@/stores/partners';
277-
import { computed, createApp, watch } from 'vue';
278+
import { computed, createApp, ref, watch } from 'vue';
278279
import { useRouter } from 'vue-router';
279280
import ServiceUnavailable from '@/components/ServiceUnavailable.vue'
280281
@@ -286,12 +287,29 @@ const app = createApp({});
286287
287288
app.component('ServiceUnavailable', ServiceUnavailable);
288289
290+
// Keeping track of the current settings for the results per page and
291+
// sort option prevents the watches from doing searches just because the
292+
// cveListSearchStore is reset (which triggers a watch). We really only
293+
// want one of these watches triggered if the user actually changes the
294+
// value.
295+
296+
let currNumPerPage = ref(cveListSearchStore.pagination.numberPerPage);
297+
let currSortByLabel = ref(cveListSearchStore.pagination.sortBy.label);
298+
289299
watch(()=>cveListSearchStore.pagination.numberPerPage, ()=>{
290-
if (!cveListSearchStore.isSearching) cveListSearchStore.handleNumberPerPageChange();
300+
if (!cveListSearchStore.isSearching
301+
&& cveListSearchStore.pagination.numberPerPage !== currNumPerPage.value) {
302+
currNumPerPage.value = cveListSearchStore.pagination.numberPerPage;
303+
cveListSearchStore.handleNumberPerPageChange();
304+
}
291305
}, { deep: true });
292306
293307
watch(()=>cveListSearchStore.pagination.sortBy, ()=>{
294-
if (!cveListSearchStore.isSearching) cveListSearchStore.sortResults();
308+
if (!cveListSearchStore.isSearching
309+
&& cveListSearchStore.pagination.sortBy.label !== currSortByLabel.value) {
310+
currSortByLabel.value = cveListSearchStore.pagination.sortBy.label;
311+
cveListSearchStore.sortResults();
312+
}
295313
}, { deep: true });
296314
297315
function backToTop() {

0 commit comments

Comments
 (0)