import Store from 'herculex';
import { updateUserInfo } from '../../utils/service';
import { toast, verifyEmail } from '../../utils';

export default new Store({
  connectGlobal: true,
  state: {
    inputValue: '',
  },
  getters: {
    tabbarList: (state, getters, global) => global.getIn(['tabbarList'], []),
  },
  actions: {
    async onConfirm({ state, commit, global }) {
      try {
        if (verifyEmail(state.inputValue)) {
          return toast('邮箱格式不正确');
        }
        // eslint-disable-next-line no-unused-vars
        const [err, res] = await updateUserInfo({
          email: state.inputValue,
        });
        if (err) throw err;
        commit('$global:updateState', {
          userInfo: {
            ...global.userInfo,
            email: state.inputValue,
          },
        });
        my.navigateBack();
      } catch (error) {
        //
      }
    },
  },
});
