import { phoneLogin } from '../../api/main'

const state = {
  id: localStorage.getItem('id')
}

const mutations = {
  SET_ID: (state, userId) => {
    state.id = userId
  }
}
const actions = {
  // user login
  login({ commit }, userInfo) {
    const { code, phone } = userInfo
    return new Promise((resolve, reject) => {
      phoneLogin({ phone: phone.trim(), code: code }).then(response => {
        commit('SET_ID', response)
        localStorage.setItem('id', response)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}

