add users data call/storing

This commit is contained in:
Axolotle 2020-07-16 16:30:19 +02:00
parent b5898d41aa
commit 687d93882a
2 changed files with 18 additions and 16 deletions

View file

@ -2,17 +2,23 @@ import api from './api'
export default {
state: () => ({
domains: undefined
domains: undefined,
users: undefined
}),
mutations: {
'SET_DATA' (state, { key, data }) {
state[key] = data
'SET_DOMAINS' (state, domains) {
state.domains = domains
},
'SET_USERS' (state, users) {
console.log(users)
state.users = Object.keys(users).length === 0 ? null : Object.values(users)
}
},
actions: {
async 'FETCH' ({ commit }, uri) {
return api.get('/' + uri).then(data => {
commit('SET_DATA', { data: data[uri], key: uri })
return api.get(uri).then(responseData => {
const data = responseData[uri] ? responseData[uri] : responseData
commit('SET_' + uri.toUpperCase(), data)
})
}
},

View file

@ -54,30 +54,26 @@
</template>
<script>
import api from '@/helpers/api'
export default {
name: 'UserList',
data: function () {
return {
search: '',
users: undefined
search: ''
}
},
computed: {
users () {
return this.$store.state.data.users
},
filteredUser () {
const search = this.search.toLowerCase()
return this.users.filter(user => {
return user.username.toLowerCase().includes(this.search.toLowerCase())
return user.username.toLowerCase().includes(search)
})
}
},
async created () {
const data = await api.get('users')
if (!data || Object.keys(data.users).length === 0) {
this.users = null
} else {
this.users = Object.values(data.users)
}
this.$store.dispatch('FETCH', 'users')
}
}
</script>