admin-fe/src/store/modules/users.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-03-01 17:32:03 +00:00
import { fetchUsers, toggleUserActivation, searchUsers } from '@/api/users'
2019-02-23 21:40:26 +00:00
const user = {
state: {
2019-02-24 22:31:05 +00:00
fetchedUsers: [],
loading: true
2019-02-23 21:40:26 +00:00
},
mutations: {
SET_USERS: (state, users) => {
2019-03-01 17:32:03 +00:00
state.fetchedUsers = users
2019-02-24 22:31:05 +00:00
},
SET_LOADING: (state, status) => {
state.loading = status
2019-02-26 19:50:25 +00:00
},
SWAP_USER: (state, user) => {
2019-03-01 17:32:03 +00:00
const usersWithoutSwapped = state.fetchedUsers.filter(u => {
2019-02-26 19:50:25 +00:00
return u.id !== user.id
})
2019-03-01 17:32:03 +00:00
state.fetchedUsers = [...usersWithoutSwapped, user].sort((a, b) =>
a.id.localeCompare(b.id)
)
2019-02-27 22:13:20 +00:00
},
SET_COUNT: (state, count) => {
state.totalUsersCount = count
},
SET_PAGE_SIZE: (state, pageSize) => {
state.pageSize = pageSize
2019-02-23 21:40:26 +00:00
}
},
actions: {
2019-02-27 22:13:20 +00:00
async FetchUsers({ commit }, page = 1) {
const response = await fetchUsers(page)
2019-02-23 21:40:26 +00:00
2019-02-27 22:13:20 +00:00
commit('SET_USERS', response.data.users)
commit('SET_COUNT', response.data.count)
commit('SET_PAGE_SIZE', response.data.page_size)
2019-02-24 22:31:05 +00:00
commit('SET_LOADING', false)
2019-02-26 19:50:25 +00:00
},
async ToggleUserActivation({ commit }, nickname) {
const response = await toggleUserActivation(nickname)
commit('SWAP_USER', response.data)
2019-03-01 17:32:03 +00:00
},
async SearchUsers({ commit, dispatch }, searchValue) {
if (searchValue.length === 0) {
dispatch('FetchUsers')
} else {
const response = await searchUsers(searchValue)
commit('SET_USERS', response.data.users)
}
2019-02-23 21:40:26 +00:00
}
}
}
export default user