Client - add/remove admin rights
This commit is contained in:
@ -5,6 +5,7 @@ import { ROOT_STORE, USERS_STORE } from '@/store/constants'
|
||||
import { IRootState } from '@/store/modules/root/types'
|
||||
import { IUsersActions, IUsersState } from '@/store/modules/users/types'
|
||||
import { IPaginationPayload } from '@/types/api'
|
||||
import { IAdminUserPayload } from '@/types/user'
|
||||
import { handleError } from '@/utils'
|
||||
|
||||
export const actions: ActionTree<IUsersState, IRootState> & IUsersActions = {
|
||||
@ -35,4 +36,27 @@ export const actions: ActionTree<IUsersState, IRootState> & IUsersActions = {
|
||||
context.commit(USERS_STORE.MUTATIONS.UPDATE_USERS_LOADING, false)
|
||||
)
|
||||
},
|
||||
[USERS_STORE.ACTIONS.UPDATE_USER](
|
||||
context: ActionContext<IUsersState, IRootState>,
|
||||
payload: IAdminUserPayload
|
||||
): void {
|
||||
console.log('payload', payload)
|
||||
context.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES)
|
||||
authApi
|
||||
.patch(`users/${payload.username}`, { admin: payload.admin })
|
||||
.then((res) => {
|
||||
if (res.data.status === 'success') {
|
||||
context.commit(
|
||||
USERS_STORE.MUTATIONS.UPDATE_USER,
|
||||
res.data.data.users[0]
|
||||
)
|
||||
} else {
|
||||
handleError(context, null)
|
||||
}
|
||||
})
|
||||
.catch((error) => handleError(context, error))
|
||||
.finally(() =>
|
||||
context.commit(USERS_STORE.MUTATIONS.UPDATE_USERS_LOADING, false)
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
export enum UsersActions {
|
||||
GET_USERS = 'GET_USERS',
|
||||
UPDATE_USER = 'UPDATE_USER',
|
||||
}
|
||||
|
||||
export enum UsersGetters {
|
||||
@ -9,6 +10,7 @@ export enum UsersGetters {
|
||||
}
|
||||
|
||||
export enum UsersMutations {
|
||||
UPDATE_USER = 'UPDATE_USER',
|
||||
UPDATE_USERS = 'UPDATE_USERS',
|
||||
UPDATE_USERS_LOADING = 'UPDATE_USERS_LOADING',
|
||||
UPDATE_USERS_PAGINATION = 'UPDATE_USERS_PAGINATION',
|
||||
|
@ -6,6 +6,17 @@ import { IPagination } from '@/types/api'
|
||||
import { IUserProfile } from '@/types/user'
|
||||
|
||||
export const mutations: MutationTree<IUsersState> & TUsersMutations = {
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USER](
|
||||
state: IUsersState,
|
||||
updatedUser: IUserProfile
|
||||
) {
|
||||
state.users = state.users.map((user) => {
|
||||
if (user.username === updatedUser.username) {
|
||||
return updatedUser
|
||||
}
|
||||
return user
|
||||
})
|
||||
},
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USERS](
|
||||
state: IUsersState,
|
||||
users: IUserProfile[]
|
||||
|
@ -8,7 +8,7 @@ import {
|
||||
import { USERS_STORE } from '@/store/constants'
|
||||
import { IRootState } from '@/store/modules/root/types'
|
||||
import { IPagination, IPaginationPayload } from '@/types/api'
|
||||
import { IUserProfile } from '@/types/user'
|
||||
import { IAdminUserPayload, IUserProfile } from '@/types/user'
|
||||
|
||||
export interface IUsersState {
|
||||
users: IUserProfile[]
|
||||
@ -21,6 +21,10 @@ export interface IUsersActions {
|
||||
context: ActionContext<IUsersState, IRootState>,
|
||||
payload: IPaginationPayload
|
||||
): void
|
||||
[USERS_STORE.ACTIONS.UPDATE_USER](
|
||||
context: ActionContext<IUsersState, IRootState>,
|
||||
payload: IAdminUserPayload
|
||||
): void
|
||||
}
|
||||
|
||||
export interface IUsersGetters {
|
||||
@ -30,6 +34,7 @@ export interface IUsersGetters {
|
||||
}
|
||||
|
||||
export type TUsersMutations<S = IUsersState> = {
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USER](state: S, updatedUser: IUserProfile): void
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USERS](state: S, users: IUserProfile[]): void
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USERS_LOADING](state: S, loading: boolean): void
|
||||
[USERS_STORE.MUTATIONS.UPDATE_USERS_PAGINATION](
|
||||
|
Reference in New Issue
Block a user