| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  | import { SessionStorage } from 'quasar'; | 
					
						
							| 
									
										
										
										
											2020-10-15 09:23:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  | export interface UserStateInterface { | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |   updateUserLoading: boolean; | 
					
						
							|  |  |  |   getUserLoading: boolean; | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   currentUser?: FG.User; | 
					
						
							|  |  |  |   users: FG.User[]; | 
					
						
							| 
									
										
										
										
											2020-10-16 06:45:40 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-28 23:19:39 +00:00
										 |  |  | const state: UserStateInterface = { | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   users: [], | 
					
						
							|  |  |  |   currentUser: SessionStorage.getItem<FG.User>('currentUser') || undefined, | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |   updateUserLoading: false, | 
					
						
							|  |  |  |   getUserLoading: false | 
					
						
							| 
									
										
										
										
											2020-10-15 09:23:41 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 06:45:40 +00:00
										 |  |  | const mutations: MutationTree<UserStateInterface> = { | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   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
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   setUsers(state, data: FG.User[]) { | 
					
						
							|  |  |  |     state.users = data; | 
					
						
							| 
									
										
										
										
											2020-10-16 06:45:40 +00:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +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> = { | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   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.'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-28 23:19:39 +00:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   getUsers({ commit }) { | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |     axios | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |       .get(`/users`) | 
					
						
							|  |  |  |       .then((response: AxiosResponse<FG.User[]>) => { | 
					
						
							|  |  |  |         commit('setUsers', response.data); | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |       }) | 
					
						
							|  |  |  |       .catch(err => { | 
					
						
							|  |  |  |         console.warn(err); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   updateUser({ commit, state, dispatch }, data) { | 
					
						
							|  |  |  |     commit('setLoading', { key: 'updateUserLoading', data: true }); | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |     if (!state.currentUser) throw 'Not logged in'; | 
					
						
							| 
									
										
										
										
											2020-10-27 12:49:45 +00:00
										 |  |  |     axios | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |       .put(`/users/${state.currentUser.userid}`, data) | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |       .then(() => { | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |         void dispatch('getCurrentUser'); | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |       }) | 
					
						
							| 
									
										
										
										
											2020-10-27 12:49:45 +00:00
										 |  |  |       .catch(error => { | 
					
						
							|  |  |  |         console.log(error); | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .finally(() => { | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |         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> = { | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   currentUser({ currentUser }) { | 
					
						
							|  |  |  |     return currentUser; | 
					
						
							| 
									
										
										
										
											2020-10-16 06:45:40 +00:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   users({ users }) { | 
					
						
							|  |  |  |     return users; | 
					
						
							| 
									
										
										
										
											2020-10-16 06:45:40 +00:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-10-30 11:08:33 +00:00
										 |  |  |   loading({ updateUserLoading, getUserLoading }) { | 
					
						
							|  |  |  |     return updateUserLoading || getUserLoading; | 
					
						
							| 
									
										
										
										
											2020-10-31 16:33:09 +00:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2020-11-04 22:53:10 +00:00
										 |  |  |   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; |