2020-12-28 17:08:08 +00:00
|
|
|
import { action, Action } from 'easy-peasy';
|
|
|
|
import { Role } from '@/api/admin/roles/getRoles';
|
|
|
|
|
|
|
|
export interface AdminRoleStore {
|
|
|
|
data: Role[];
|
2021-01-02 00:03:10 +00:00
|
|
|
selectedRoles: number[];
|
2021-01-01 22:55:30 +00:00
|
|
|
|
2020-12-28 17:08:08 +00:00
|
|
|
setRoles: Action<AdminRoleStore, Role[]>;
|
|
|
|
appendRole: Action<AdminRoleStore, Role>;
|
|
|
|
removeRole: Action<AdminRoleStore, number>;
|
2021-01-02 00:03:10 +00:00
|
|
|
|
|
|
|
setSelectedRoles: Action<AdminRoleStore, number[]>;
|
|
|
|
appendSelectedRole: Action<AdminRoleStore, number>;
|
|
|
|
removeSelectedRole: Action<AdminRoleStore, number>;
|
2020-12-28 17:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const roles: AdminRoleStore = {
|
|
|
|
data: [],
|
2021-01-02 00:03:10 +00:00
|
|
|
selectedRoles: [],
|
2020-12-28 17:08:08 +00:00
|
|
|
|
|
|
|
setRoles: action((state, payload) => {
|
|
|
|
state.data = payload;
|
|
|
|
}),
|
|
|
|
|
|
|
|
appendRole: action((state, payload) => {
|
2021-01-01 00:27:16 +00:00
|
|
|
if (state.data.find(role => role.id === payload.id)) {
|
|
|
|
state.data = state.data.map(role => role.id === payload.id ? payload : role);
|
2020-12-28 17:08:08 +00:00
|
|
|
} else {
|
|
|
|
state.data = [ ...state.data, payload ];
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
removeRole: action((state, payload) => {
|
|
|
|
state.data = [ ...state.data.filter(role => role.id !== payload) ];
|
|
|
|
}),
|
2021-01-02 00:03:10 +00:00
|
|
|
|
|
|
|
setSelectedRoles: action((state, payload) => {
|
|
|
|
state.selectedRoles = payload;
|
|
|
|
}),
|
|
|
|
|
|
|
|
appendSelectedRole: action((state, payload) => {
|
|
|
|
state.selectedRoles = state.selectedRoles.filter(id => id !== payload).concat(payload);
|
|
|
|
}),
|
|
|
|
|
|
|
|
removeSelectedRole: action((state, payload) => {
|
|
|
|
state.selectedRoles = state.selectedRoles.filter(id => id !== payload);
|
|
|
|
}),
|
2020-12-28 17:08:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default roles;
|