add pagination
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
2022-01-16 16:36:54 +01:00
parent 04cb8a7217
commit bac8731e17
3 changed files with 124 additions and 6 deletions

View File

@ -12,8 +12,10 @@ export default {
profiles: [],
error: false,
errorMessage: '',
pages: 1,
query: {
search: ''
search: '',
page: 1
}
}
},
@ -40,11 +42,17 @@ export default {
state.errorMessage = errorMessage
},
setQuerySearch(state, search) {
state.query.search = search
state.query = {...state.query, search}
},
setPages(state, pages) {
state.pages = pages
},
setQueryPage(state, page) {
state.query = {...state.query, page}
}
},
actions: {
async search({state, commit, rootState}) {
async search({state, commit, rootState, dispatch}) {
if (state.searching) {
return
}
@ -60,11 +68,13 @@ export default {
commit('setErrorMessage', '')
const url = new URL(`${window.ki.apiUrl}/users/profiles`)
if (state.query.search) {
url.searchParams.append('search', state.query.search)
}
url.searchParams.append('page', state.query.page)
const headers = {
Authorization: `Bearer ${rootState.token}`,
}
@ -81,8 +91,19 @@ export default {
return
}
console.log(response.ok)
console.log(response.status)
console.log(state.query.page)
clearTimeout(timeoutId)
if (!response.ok && response.status === 404 && state.query.page != 1) {
commit('setQueryPage', 1)
commit('setSearching', false)
await dispatch('search')
return
}
if (!response.ok) {
commit('setError', true)
commit('clearProfiles')
@ -93,6 +114,7 @@ export default {
const responseData = await response.json()
commit('setProfiles', responseData.profiles)
commit('setPages', responseData.pages)
commit('setSearching', false)
commit('hideSpinner')
}