Skip to content

Commit ccc4db2

Browse files
authored
Merge pull request #322 from fiji-flo/search-more
Add a more button for > 100 search results.
2 parents 3a115f9 + 4acbe9b commit ccc4db2

2 files changed

Lines changed: 53 additions & 12 deletions

File tree

src/components/search/SearchResultList.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<ul class="search-result-list">
33
<SearchResult
4-
v-for="(result, index) in results.dinos"
4+
v-for="(dino, index) in dinos"
55
:key="index"
6-
v-bind="result"
6+
v-bind="dino"
77
></SearchResult>
88
</ul>
99
</template>
@@ -14,7 +14,7 @@ import SearchResult from './SearchResult.vue';
1414
export default {
1515
name: 'SearchResultList',
1616
props: {
17-
results: Object,
17+
dinos: Array,
1818
},
1919
components: {
2020
SearchResult,

src/pages/PageSearchResult.vue

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<template v-if="!this.$route.query.query">
66
<p>You have not searched.</p>
77
</template>
8-
<LoadingSpinner v-else-if="loading"></LoadingSpinner>
8+
<LoadingSpinner v-else-if="loading && !more"></LoadingSpinner>
99
<Error v-else-if="error">
1010
<template slot="image">
1111
<img
@@ -60,13 +60,25 @@
6060
{{ results.total }} results for
6161
<strong>{{ this.$route.query.query }}</strong>
6262
</p>
63-
<SearchResultList :results="results"></SearchResultList>
63+
<SearchResultList :dinos="results.dinos"></SearchResultList>
64+
<LoadingSpinner v-if="loading"></LoadingSpinner>
65+
<Button
66+
v-else-if="results.dinos.length < results.total"
67+
type="button"
68+
class="search-results__button-more button button--text-only button--icon-only"
69+
@click="fetchMore"
70+
>
71+
<Icon id="chevron-down" :width="24" :height="24" />
72+
<span>Show More</span>
73+
</Button>
6474
</template>
6575
</main>
6676
</template>
6777

6878
<script>
6979
import Error from '@/components/ui/Error.vue';
80+
import Icon from '@/components/ui/Icon.vue';
81+
import Button from '@/components/ui/Button.vue';
7082
import LoadingSpinner from '@/components/ui/LoadingSpinner.vue';
7183
import SearchResultList from '@/components/search/SearchResultList.vue';
7284
import SearchToggle from '@/components/search/SearchToggle.vue';
@@ -77,6 +89,8 @@ const fetcher = new Fetcher({ failoverOn: [302] });
7789
export default {
7890
name: 'PageSearchResult',
7991
components: {
92+
Icon,
93+
Button,
8094
Error,
8195
LoadingSpinner,
8296
SearchResultList,
@@ -87,37 +101,54 @@ export default {
87101
loading: false,
88102
post: null,
89103
error: null,
104+
more: false,
105+
results: { dinos: [], total: 0, next: null },
90106
};
91107
},
92108
created() {
93-
this.fetchData();
109+
this.fetchNew();
94110
},
95111
watch: {
96-
$route: 'fetchData',
112+
$route: 'fetchNew',
97113
},
98114
methods: {
99-
async fetchData() {
115+
async fetchNew() {
116+
this.loading = true;
117+
const results = await this.fetchData();
118+
this.results = results;
119+
this.loading = false;
120+
},
121+
async fetchMore() {
122+
this.loading = true;
123+
const results = await this.fetchData(true);
124+
this.results.dinos.push(...results.dinos);
125+
this.results.next = results.next;
126+
this.loading = false;
127+
},
128+
async fetchData(more = false) {
129+
this.more = more;
100130
this.error = null;
101131
this.post = null;
102-
this.loading = true;
103132
try {
104133
const params = new URLSearchParams([
105134
['q', this.$route.query.query],
106135
['w', this.$route.query.who],
107136
]);
137+
if (more) {
138+
params.append('a', this.results.next);
139+
}
108140
const data = await fetcher.fetch(
109141
`/api/v4/search/simple/?${params.toString()}`,
110142
);
111143
const results = await data.json();
112-
this.results = results;
144+
return results;
113145
} catch (e) {
114146
if (e instanceof TypeError && e.message.startsWith('NetworkError')) {
115147
window.location.reload();
116-
return;
117148
}
118149
this.error = e;
119150
}
120-
this.loading = false;
151+
return { dinos: [], total: 0, next: null };
121152
},
122153
},
123154
updated() {
@@ -132,5 +163,15 @@ export default {
132163
.search-results {
133164
padding-top: 1em;
134165
align-self: start;
166+
display: flex;
167+
flex-direction: column;
168+
}
169+
.search-results__button-more {
170+
margin: auto;
171+
border: none;
172+
font-size: 0.9em;
173+
}
174+
.search-results__button-more > svg {
175+
margin-right: 0.5em;
135176
}
136177
</style>

0 commit comments

Comments
 (0)