import {UserState} from '@/types/views/user.interface';
import {GetterTree, MutationTree, ActionTree} from 'vuex';

const state: UserState = {
    author: '三毛'
};

// 强制使用getter获取state
const getters: GetterTree<UserState, any> = {
    author: (state: UserState) => state.author
};

// 更改state
const mutations: MutationTree<UserState> = {
    // 更新state都用该方法
    UPDATE_STATE(state: UserState, data: UserState) {
        for (const key in data) {
            if (!data.hasOwnProperty(key)) {
                return;
            }
            // @ts-ignore
            state[key] = data[key];
        }
    }
};

const actions: ActionTree<UserState, any> = {
    UPDATE_STATE_ASYN({commit, state: UserState}, data: UserState) {
        commit('UPDATE_STATE', data);
    },
    // GET_DATA_ASYN({ commit, state: LoginState }) {
    //   User.getData()
    // }
};

export default {
    state,
    getters,
    mutations,
    actions
};

