This commit is contained in:
parent
04cb8a7217
commit
b2c6a2e525
43
src/components/Paginator.vue
Normal file
43
src/components/Paginator.vue
Normal file
@ -0,0 +1,43 @@
|
||||
<template>
|
||||
<ul class="pagination">
|
||||
<li
|
||||
class="page-item"
|
||||
:class="{ active: page === current }"
|
||||
v-for="page in pages"
|
||||
:key="page"
|
||||
>
|
||||
<span
|
||||
class="page-link pointer"
|
||||
@click="onPageClicked(page)"
|
||||
>
|
||||
{{ page }}
|
||||
</span>
|
||||
</li>
|
||||
</ul>
|
||||
</template>
|
||||
<script>
|
||||
|
||||
export default {
|
||||
name: 'Paginator',
|
||||
props: {
|
||||
page: Number,
|
||||
current: Number,
|
||||
pages: Number
|
||||
},
|
||||
methods: {
|
||||
onPageClicked(page) {
|
||||
if (page == this.current) {
|
||||
return
|
||||
}
|
||||
|
||||
this.$emit('page', page)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
<style>
|
||||
.pointer {
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
@ -12,8 +12,10 @@ export default {
|
||||
profiles: [],
|
||||
error: false,
|
||||
errorMessage: '',
|
||||
pages: 1,
|
||||
query: {
|
||||
search: ''
|
||||
search: '',
|
||||
page: 1
|
||||
}
|
||||
}
|
||||
},
|
||||
@ -41,6 +43,12 @@ export default {
|
||||
},
|
||||
setQuerySearch(state, search) {
|
||||
state.query.search = search
|
||||
},
|
||||
setPages(state, pages) {
|
||||
state.pages = pages
|
||||
},
|
||||
setQueryPage(state, page) {
|
||||
state.query.page = page
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
@ -65,6 +73,8 @@ export default {
|
||||
url.searchParams.append('search', state.query.search)
|
||||
}
|
||||
|
||||
url.searchParams.append('page', state.query.page)
|
||||
|
||||
const headers = {
|
||||
Authorization: `Bearer ${rootState.token}`,
|
||||
}
|
||||
@ -93,6 +103,7 @@ export default {
|
||||
|
||||
const responseData = await response.json()
|
||||
commit('setProfiles', responseData.profiles)
|
||||
commit('setPages', responseData.pages)
|
||||
commit('setSearching', false)
|
||||
commit('hideSpinner')
|
||||
}
|
||||
|
@ -53,12 +53,26 @@ SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
<p v-if="searchText !== ''">Probiere eine andere Suche.</p>
|
||||
</div>
|
||||
<div v-else-if="showResults">
|
||||
<div class="d-flex justify-content-around">
|
||||
<Paginator
|
||||
:pages="pages"
|
||||
:current="currentPage"
|
||||
@page="handlePageSelected"
|
||||
/>
|
||||
</div>
|
||||
<SearchResult
|
||||
v-for="profile in profiles"
|
||||
:key="profile.user_id"
|
||||
class="mb-3"
|
||||
:profile="profile"
|
||||
/>
|
||||
<div class="d-flex justify-content-around">
|
||||
<Paginator
|
||||
:pages="pages"
|
||||
:current="currentPage"
|
||||
@page="handlePageSelected"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@ -67,21 +81,29 @@ SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
<script>
|
||||
import { mapState } from 'vuex'
|
||||
|
||||
import Paginator from '@/components/Paginator'
|
||||
import SearchResult from '@/components/SearchResult'
|
||||
import Spinner from '@/components/Spinner'
|
||||
|
||||
export default {
|
||||
name: 'Search',
|
||||
components: {
|
||||
Paginator,
|
||||
SearchResult,
|
||||
Spinner,
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
textChanged: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
...mapState({
|
||||
searching: state => state.search.searching,
|
||||
profiles: state => state.search.profiles,
|
||||
error: state => state.search.error,
|
||||
showSpinner: state => state.search.showSpinner,
|
||||
pages: state => state.search.pages,
|
||||
}),
|
||||
searchText: {
|
||||
get() {
|
||||
@ -89,6 +111,15 @@ export default {
|
||||
},
|
||||
set(text) {
|
||||
this.$store.commit('search/setQuerySearch', text)
|
||||
this.textChanged = true
|
||||
}
|
||||
},
|
||||
currentPage: {
|
||||
get() {
|
||||
return this.$store.state.search.query.page
|
||||
},
|
||||
set(page) {
|
||||
this.$store.commit('search/setQueryPage', page)
|
||||
}
|
||||
},
|
||||
showNoResults() {
|
||||
@ -109,13 +140,24 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
handleSubmit() {
|
||||
this.$router.push({ query: { query: this.searchText }})
|
||||
if (this.textChanged === true) {
|
||||
this.$store.commit('search/setQueryPage', 1)
|
||||
}
|
||||
this.pushState()
|
||||
this.$store.dispatch('search/search')
|
||||
},
|
||||
focusSearchText() {
|
||||
this.$nextTick(() => {
|
||||
this.$refs.searchTextInput.focus()
|
||||
})
|
||||
},
|
||||
handlePageSelected(page) {
|
||||
this.currentPage = page
|
||||
this.pushState()
|
||||
this.$store.dispatch('search/search')
|
||||
},
|
||||
pushState() {
|
||||
this.$router.push({ query: { query: this.searchText, page: this.currentPage }})
|
||||
}
|
||||
},
|
||||
created() {
|
||||
|
Loading…
Reference in New Issue
Block a user