import { ActionTree , MutationTree , GetterTree , ActionContext } from 'vuex' ; import { fetchQiitaUser } from '../infrastructure/api/qiita' ; import { RootState } from '~/store/index' ; import { QiitaUser } from '~/domain/qiita' ; export interface State { accessToken : string ; user ? : QiitaUser ; } export const types = { SAVE_ACCESS_TOKEN : 'SAVE_ACCESS_TOKEN' , SAVE_USER : 'SAVE_USER' } ; export const state = ( ) : State => ( { accessToken : '' } ) ; export const getters = { accessToken : state => ( ) : string => { return state . accessToken ; } , isAuthorized : state => ( ) : boolean => { return ! ! state . accessToken ; } , authorizedUser : state => ( ) => { return state . user ; } } ; export interface SaveAccessTokenPayload { accessToken : string ; } export interface FetchUserPayload { accessToken : string ; } export interface SaveUserPayload { user : QiitaUser ; } export interface Actions < S , R > extends ActionTree < S , R > { saveAccessToken ( context , payload ) : void ; fetchUser ( context ) : void ; } export const actions = { saveAccessToken : ( { commit } , { accessToken } ) => { commit ( types . SAVE_ACCESS_TOKEN , { accessToken } ) ; } , fetchUser : async ( { commit , state } ) => { const user = await fetchQiitaUser ( state . accessToken ) ; commit ( types . SAVE_USER , { user } ) ; } } ; export const mutations = { [ types . SAVE_ACCESS_TOKEN ] : ( state , { accessToken } ) : void => { state . accessToken = accessToken ; } , [ types . SAVE_USER ] : ( state , { user } ) : void => { state . user = user ; } } ;