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

86 lines
2.3 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
2019-03-22 20:58:58 +00:00
const users = {
2019-02-23 21:40:26 +00:00
state: {
2019-02-24 22:31:05 +00:00
fetchedUsers: [],
2019-03-03 20:01:00 +00:00
loading: true,
searchQuery: '',
totalUsersCount: 0,
2019-03-06 19:44:37 +00:00
currentPage: 1,
2019-03-22 20:58:58 +00:00
showLocalUsersOnly: false
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
},
2019-03-03 20:46:14 +00:00
SET_PAGE: (state, page) => {
state.currentPage = page
},
2019-02-27 22:13:20 +00:00
SET_PAGE_SIZE: (state, pageSize) => {
state.pageSize = pageSize
2019-03-03 20:01:00 +00:00
},
SET_SEARCH_QUERY: (state, query) => {
state.searchQuery = query
2019-03-06 19:44:37 +00:00
},
SET_LOCAL_USERS_FILTER: (state, value) => {
2019-03-22 20:58:58 +00:00
state.showLocalUsersOnly = value
2019-02-23 21:40:26 +00:00
}
},
actions: {
2019-03-22 20:58:58 +00:00
async FetchUsers({ commit, state, getters }, { page }) {
const response = await fetchUsers(state.showLocalUsersOnly, getters.token, page)
2019-03-03 20:01:00 +00:00
commit('SET_LOADING', true)
2019-02-23 21:40:26 +00:00
2019-03-03 20:46:14 +00:00
loadUsers(commit, page, response.data)
2019-02-26 19:50:25 +00:00
},
2019-03-22 20:58:58 +00:00
async ToggleUserActivation({ commit, getters }, nickname) {
const response = await toggleUserActivation(nickname, getters.token)
2019-02-26 19:50:25 +00:00
commit('SWAP_USER', response.data)
2019-03-01 17:32:03 +00:00
},
2019-03-22 20:58:58 +00:00
async SearchUsers({ commit, dispatch, state, getters }, { query, page }) {
2019-03-03 20:01:00 +00:00
if (query.length === 0) {
commit('SET_SEARCH_QUERY', query)
dispatch('FetchUsers', page)
2019-03-01 17:32:03 +00:00
} else {
2019-03-03 20:01:00 +00:00
commit('SET_LOADING', true)
commit('SET_SEARCH_QUERY', query)
2019-03-22 20:58:58 +00:00
const response = await searchUsers(query, state.showLocalUsersOnly, getters.token, page)
2019-03-03 20:01:00 +00:00
2019-03-03 20:46:14 +00:00
loadUsers(commit, page, response.data)
2019-03-01 17:32:03 +00:00
}
2019-03-06 19:44:37 +00:00
},
async ToggleLocalUsersFilter({ commit, dispatch, state }, value) {
commit('SET_LOCAL_USERS_FILTER', value)
dispatch('SearchUsers', { query: state.searchQuery, page: 1 })
2019-02-23 21:40:26 +00:00
}
}
}
2019-03-03 20:46:14 +00:00
const loadUsers = (commit, page, { users, count, page_size }) => {
2019-03-03 20:01:00 +00:00
commit('SET_USERS', users)
commit('SET_COUNT', count)
2019-03-03 20:46:14 +00:00
commit('SET_PAGE', page)
2019-03-03 20:01:00 +00:00
commit('SET_PAGE_SIZE', page_size)
commit('SET_LOADING', false)
}
2019-03-22 20:58:58 +00:00
export default users