flaschengeist-frontend/src/plugins/user/store/user.ts

122 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-10-16 06:45:40 +00:00
import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
2020-10-15 09:23:41 +00:00
import { StateInterface } from 'src/store';
import { axios } from 'boot/axios';
2020-10-16 06:45:40 +00:00
import { AxiosResponse } from 'axios';
import { SessionStorage } from 'quasar';
2020-10-15 09:23:41 +00:00
export interface UserStateInterface {
updateUserLoading: boolean;
getUserLoading: boolean;
currentUser?: FG.User;
users: FG.User[];
2020-10-16 06:45:40 +00:00
}
const state: UserStateInterface = {
users: [],
currentUser: SessionStorage.getItem<FG.User>('currentUser') || undefined,
updateUserLoading: false,
getUserLoading: false
2020-10-15 09:23:41 +00:00
};
2020-10-16 06:45:40 +00:00
const mutations: MutationTree<UserStateInterface> = {
setCurrentUser(state, data: FG.User) {
SessionStorage.set('currentUser', data);
state.currentUser = data;
},
clearCurrentUser(state) {
SessionStorage.remove('currentUser');
state.currentUser = undefined;
2020-10-16 06:45:40 +00:00
},
setUsers(state, data: FG.User[]) {
state.users = data;
2020-10-16 06:45:40 +00:00
},
setLoading(
state,
data: { key: 'updateUserLoading' | 'getUserLoading'; data: boolean }
) {
state[data.key] = data.data;
2020-10-15 09:23:41 +00:00
}
};
const actions: ActionTree<UserStateInterface, StateInterface> = {
getCurrentUser({ commit, rootState }) {
if (rootState.session.currentSession) {
commit('setLoading', { key: 'getUserLoading', data: true });
axios
.get(`/users/${rootState.session.currentSession.userid}`)
.then((response: AxiosResponse<FG.User>) => {
commit('setCurrentUser', response.data);
})
.catch(err => {
console.warn(err);
})
.finally(() => {
commit('setLoading', { key: 'getUserLoading', data: false });
});
} else {
console.debug('User not logged in, can not get current_user.');
}
},
getUsers({ commit }) {
axios
.get(`/users`)
.then((response: AxiosResponse<FG.User[]>) => {
commit('setUsers', response.data);
})
.catch(err => {
console.warn(err);
});
},
updateUser({ commit, state, dispatch }, data) {
commit('setLoading', { key: 'updateUserLoading', data: true });
if (!state.currentUser) throw 'Not logged in';
2020-10-27 12:49:45 +00:00
axios
.put(`/users/${state.currentUser.userid}`, data)
.then(() => {
void dispatch('getCurrentUser');
})
2020-10-27 12:49:45 +00:00
.catch(error => {
console.log(error);
})
.finally(() => {
commit('setLoading', { key: 'updateUserLoading', data: false });
2020-10-27 12:49:45 +00:00
});
2020-10-16 06:45:40 +00:00
}
};
const getters: GetterTree<UserStateInterface, StateInterface> = {
currentUser({ currentUser }) {
return currentUser;
2020-10-16 06:45:40 +00:00
},
users({ users }) {
return users;
2020-10-16 06:45:40 +00:00
},
loading({ updateUserLoading, getUserLoading }) {
return updateUserLoading || getUserLoading;
},
displayName({ currentUser }) {
return currentUser?.display_name;
},
permissions({ currentUser }) {
let permissions: string[] = [];
if (currentUser) {
currentUser.roles.forEach(role => {
permissions = permissions.concat(role.permissions);
});
}
return permissions;
2020-10-15 09:23:41 +00:00
}
};
2020-10-16 06:45:40 +00:00
const userStore: Module<UserStateInterface, StateInterface> = {
namespaced: true,
actions,
getters,
mutations,
state
};
export default userStore;