import { Module , ActionContext } from 'vuex' ; import store , { RootState } from '@/store' ; import { UserState , UserInfo , LoginProfile , SignupProfile , ChangePasswordProfile } from './types' ; import { axiosPost , errorMessage } from '@/common/util' ; const initialState = { username : '' , errorMessage : '' , userInfo : { username : '' , email : '' } , loginCallback : undefined } ; const mutations = { loginUser : ( state , userInfo ) => { state . userInfo = userInfo ; state . username = userInfo . username ; } , logoutUser : ( state ) => { state . userInfo = { username : '' , email : '' } ; state . username = '' ; } , resetMessage : ( state ) => { state . errorMessage = '' ; } } ; /**
 * Dispatches user related actions upon login.
 */ const onLogin = ( context ) => { context . dispatch ( 'dataflow/listDiagram' , null , { root : true } ) ; context . dispatch ( 'dataset/listDataset' , null , { root : true } ) ; } ; const actions = { /**
     * Request login by showing a login modal.
     * After the user successfully logins, callback will be executed.
     */ requestLogin ( context , options ) { context . state . loginCallback = options . loginCallback ; store . commit ( 'modals/openLoginModal' ) ; } , login ( context , profile ) { return new Promise ( ( resolve , reject ) => { axiosPost < UserInfo > ( '/user/login' , profile ) . then ( res => { context . commit ( 'loginUser' , res . data ) ; onLogin ( context ) ; resolve ( res . data . username ) ; if ( context . state . loginCallback ) { context . state . loginCallback ( ) ; } } ) . catch ( err => reject ( errorMessage ( err ) ) ) ; } ) ; } , logout ( context ) { return new Promise ( ( resolve , reject ) => { axiosPost < void > ( '/user/logout' , { } ) . then ( ( ) => { context . commit ( 'logoutUser' ) ; resolve ( ) ; } ) . catch ( err => reject ( errorMessage ( err ) ) ) ; } ) ; } , signup ( context , profile ) { return new Promise ( ( resolve , reject ) => { axiosPost < UserInfo > ( '/user/signup' , profile ) . then ( res => { context . commit ( 'loginUser' , res . data ) ; resolve ( res . data . username ) ; } ) . catch ( err => reject ( errorMessage ( err ) ) ) ; } ) ; } , /**
     * Queries the currently logged in user from the last session.
     * The promise returnsempty string when no user logged in or the session expires.
     */ whoami ( context ) { return new Promise ( ( resolve , reject ) => { axiosPost < UserInfo > ( '/user/whoami' , { } ) . then ( res => { context . commit ( 'loginUser' , res . data ) ; onLogin ( context ) ; resolve ( res . data . username || '' ) ; } ) . catch ( err => reject ( errorMessage ( err ) ) ) ; } ) ; } , /**
     * Updates password.
     */ changePassword ( context , profile ) { return new Promise ( ( resolve , reject ) => { axiosPost < void > ( '/user/changePassword' , profile ) . then ( ( ) => resolve ( ) ) . catch ( err => reject ( errorMessage ( err ) ) ) ; } ) ; } } ; const user = { namespaced : true , state : initialState , mutations , actions } ; export default user ;