import { MutationTree , ActionTree , Module } from 'vuex' ; import defaultConfig , { base , preference , translation , web , template } from '@/defaults/config' ; import { State as RootState } from '../index' ; import { update , clear } from '@/stores/mutations' ; import { QUERY_CONFIG , SET_CONFIG , RESET_CONFIG } from '@/types' ; import { configKeysReducer , istype } from '@/functions' ; import debug from '@/functions/debug' ; export const namespaced = true ; export let PAGE ; export let KEYS ; const filterConfig = ( changes ) => { const config = { } ; for ( const [ k , v ] of Object . entries ( changes ) ) { if ( istype ( v , 'undefined' ) || ! KEYS ! . includes ( k as keyof DefaultConfig ) ) { continue ; } config [ k ] = v ; } return config ; } ; export const actionGenerator = ( type , payload ? ) : IpcAction => ( { type , payload , meta : { from : PAGE } , } ) ; export const state = { } ; export const mutations = { update , clear , init : ( state , { page , keys } ) => { state . page = page ; state . keys = keys ; } , } ; type configKey = keyof DefaultConfig ; export const webActions = { reset : ( { dispatch } , keys ) => { let config ; const configs = { base , preference , translation , web , template } ; if ( ! ! keys && istype ( keys , 'string' ) ) { config = configs [ keys as configCat ] ; } else if ( istype ( keys , [ 'array' , 'object' ] ) ) { config = configKeysReducer ( keys , defaultConfig ) [ 1 ] as DefaultConfig ; } else { config = { ... defaultConfig } ; } dispatch ( 'merge' , config ) ; } , query : ( { dispatch , commit } , keys ? ) => { if ( ! localStorage . getItem ( 'config' ) ) { dispatch ( 'reset' ) ; } let config ; if ( ! keys ) { config = JSON . parse ( localStorage . getItem ( 'config' ) ! ) ; } else { config = JSON . parse ( localStorage . getItem ( 'config' ) ! ) ; config = configKeysReducer ( keys , config ) [ 1 ] as DefaultConfig ; } commit ( 'update' , config ) ; } , save : ( { state , commit } , config ) => { const getConfig = JSON . parse ( localStorage . getItem ( 'config' ) ! ) ; localStorage . setItem ( 'config' , JSON . stringify ( Object . assign ( { } , getConfig , config ) ) ) ; return true ; } , } ; export const ipcActions = { reset : async ( { commit , dispatch } , keys ) => { const action = actionGenerator ( RESET_CONFIG , keys ) ; const { payload : config } = await dispatch ( 'ipc' , action , { root : true } ) ; commit ( 'update' , config ) ; } , query : async ( { commit , dispatch } , keys ? ) => { const action = actionGenerator ( QUERY_CONFIG , keys ) ; const { payload : config } = await dispatch ( 'ipc' , action , { root : true } ) ; commit ( 'update' , config ) ; } , save : async ( { dispatch } , config ) => { const action = actionGenerator ( SET_CONFIG , config ) ; const { error , payload : success } = await dispatch ( 'ipc' , action , { root : true } ) ; if ( error !== null ) { return dispatch ( 'notify' , error , { root : true } ) ; } return success ; } , } ; export const actions = { ... ( TARGET_BROWSER === 'web' ? webActions : ipcActions ) , init : async ( { dispatch , commit } , { page = '' , keys = [ ] } ) => { [ PAGE , KEYS ] = [ page , keys ] ; await dispatch ( 'fetch' , keys ) ; } , fetch : async ( { dispatch } , keys ) => { await dispatch ( 'query' , keys ) ; } , merge : async ( { state , dispatch , commit } , changes ) => { const config = filterConfig ( changes ) as DefaultConfig ; const success = await dispatch ( 'save' , config ) ; if ( ! success ) { debug . error ( 'Does not save config changes. Maybe background server occur problem.' ) ; dispatch ( 'notify' , 'Config saved failed.' , { root : true } ) ; return false ; } commit ( 'update' , config ) ; return true ; } , update : async ( { commit } , changes ) => { commit ( 'update' , filterConfig ( changes ) ) ; } , } ; export const storage = { namespaced , state , actions , mutations , } ; export default storage ; export interface State { [ name ] : any ; }