mirror of
https://github.com/crater-invoice/crater.git
synced 2026-02-09 12:22:40 -05:00
build version 400
This commit is contained in:
119
resources/assets/js/store/modules/users/actions.js
Normal file
119
resources/assets/js/store/modules/users/actions.js
Normal file
@@ -0,0 +1,119 @@
|
||||
import * as types from './mutation-types'
|
||||
import * as searchTypes from '../search/mutation-types'
|
||||
|
||||
export const fetchUsers = ({ commit, dispatch, state }, params) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.get(`/api/v1/users`, { params })
|
||||
.then((response) => {
|
||||
commit(types.BOOTSTRAP_USERS, response.data.users.data)
|
||||
commit(types.SET_TOTAL_USERS, response.data.users.total)
|
||||
commit(
|
||||
'search/' + searchTypes.SET_USER_LISTS,
|
||||
response.data.users.data,
|
||||
{ root: true }
|
||||
)
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const fetchUser = ({ commit, dispatch }, id) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.get(`/api/v1/users/${id}`)
|
||||
.then((response) => {
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const addUser = ({ commit, dispatch, state }, data) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.post('/api/v1/users', data)
|
||||
.then((response) => {
|
||||
commit(types.ADD_USER, response.data)
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const updateUser = ({ commit, dispatch, state }, data) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.put(`/api/v1/users/${data.id}`, data)
|
||||
.then((response) => {
|
||||
if (response.data.success) {
|
||||
commit(types.UPDATE_USER, response.data)
|
||||
}
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteUser = ({ commit, dispatch, state }, user) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.post(`/api/v1/users/delete`, {
|
||||
users: user,
|
||||
})
|
||||
.then((response) => {
|
||||
commit(types.DELETE_USER, user)
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteMultipleUsers = ({ commit, dispatch, state }, id) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
window.axios
|
||||
.post(`/api/v1/users/delete`, { users: state.selectedUsers })
|
||||
.then((response) => {
|
||||
commit(types.DELETE_MULTIPLE_USERS, state.selectedUsers)
|
||||
resolve(response)
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export const setSelectAllState = ({ commit, dispatch, state }, data) => {
|
||||
commit(types.SET_SELECT_ALL_STATE, data)
|
||||
}
|
||||
|
||||
export const selectAllUsers = ({ commit, dispatch, state }) => {
|
||||
if (state.selectedUsers.length === state.users.length) {
|
||||
commit(types.SET_SELECTED_USERS, [])
|
||||
commit(types.SET_SELECT_ALL_STATE, false)
|
||||
} else {
|
||||
let allUserIds = state.users.map((user) => user.id)
|
||||
commit(types.SET_SELECTED_USERS, allUserIds)
|
||||
commit(types.SET_SELECT_ALL_STATE, true)
|
||||
}
|
||||
}
|
||||
|
||||
export const selectedUser = ({ commit, dispatch, state }, data) => {
|
||||
commit(types.SET_SELECTED_USERS, data)
|
||||
if (state.selectedUsers.length === state.users.length) {
|
||||
commit(types.SET_SELECT_ALL_STATE, true)
|
||||
} else {
|
||||
commit(types.SET_SELECT_ALL_STATE, false)
|
||||
}
|
||||
}
|
||||
4
resources/assets/js/store/modules/users/getters.js
Normal file
4
resources/assets/js/store/modules/users/getters.js
Normal file
@@ -0,0 +1,4 @@
|
||||
export const users = (state) => state.users
|
||||
export const selectAllField = (state) => state.selectAllField
|
||||
export const selectedUsers = (state) => state.selectedUsers
|
||||
export const totalUsers = (state) => state.totalUsers
|
||||
22
resources/assets/js/store/modules/users/index.js
Normal file
22
resources/assets/js/store/modules/users/index.js
Normal file
@@ -0,0 +1,22 @@
|
||||
import mutations from './mutations'
|
||||
import * as actions from './actions'
|
||||
import * as getters from './getters'
|
||||
|
||||
const initialState = {
|
||||
users: [],
|
||||
totalUsers: 0,
|
||||
selectAllField: false,
|
||||
selectedUsers: [],
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
|
||||
state: initialState,
|
||||
|
||||
getters: getters,
|
||||
|
||||
actions: actions,
|
||||
|
||||
mutations: mutations,
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
export const BOOTSTRAP_USERS = 'BOOTSTRAP_USERS'
|
||||
export const ADD_USER = 'ADD_USER'
|
||||
export const UPDATE_USER = 'UPDATE_USER'
|
||||
export const DELETE_USER = 'DELETE_USER'
|
||||
export const DELETE_MULTIPLE_USERS = 'DELETE_MULTIPLE_USERS'
|
||||
export const SET_SELECTED_USERS = 'SET_SELECTED_USERS'
|
||||
export const SET_TOTAL_USERS = 'SET_TOTAL_USERS'
|
||||
export const SET_SELECT_ALL_STATE = 'SET_SELECT_ALL_STATE'
|
||||
43
resources/assets/js/store/modules/users/mutations.js
Normal file
43
resources/assets/js/store/modules/users/mutations.js
Normal file
@@ -0,0 +1,43 @@
|
||||
import * as types from './mutation-types'
|
||||
|
||||
export default {
|
||||
[types.BOOTSTRAP_USERS](state, users) {
|
||||
state.users = users
|
||||
},
|
||||
|
||||
[types.SET_TOTAL_USERS](state, totalUsers) {
|
||||
state.totalUsers = totalUsers
|
||||
},
|
||||
|
||||
[types.ADD_USER](state, data) {
|
||||
state.users.push(data.user)
|
||||
},
|
||||
|
||||
[types.UPDATE_USER](state, data) {
|
||||
let pos = state.users.findIndex((user) => user.id === data.user.id)
|
||||
|
||||
state.users[pos] = data.user
|
||||
},
|
||||
|
||||
[types.DELETE_USER](state, id) {
|
||||
let index = state.users.findIndex((user) => user.id === id[0])
|
||||
state.users.splice(index, 1)
|
||||
},
|
||||
|
||||
[types.DELETE_MULTIPLE_USERS](state, selectedUsers) {
|
||||
selectedUsers.forEach((user) => {
|
||||
let index = state.users.findIndex((_user) => _user.id === user.id)
|
||||
state.users.splice(index, 1)
|
||||
})
|
||||
|
||||
state.selectedUsers = []
|
||||
},
|
||||
|
||||
[types.SET_SELECTED_USERS](state, data) {
|
||||
state.selectedUsers = data
|
||||
},
|
||||
|
||||
[types.SET_SELECT_ALL_STATE](state, data) {
|
||||
state.selectAllField = data
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user