import api from '@/api.service'

export const state = () => {
  return {
    user: {
      name: ''
    }
  }
}

const SET_WORKSPACE = 'setWorkspace'
const SET_USER = 'setUser'

export const actions = {
  async loginUser({ commit }) {
    const { data } = await api.get('users/1')
    commit(SET_USER, data)
  },

  async changeActiveWorkspace({ commit, state }, workspace) {
    const newWorkspace = { ...state.user.workspace, ...workspace }
    await api.patch('users/1', { workspace: newWorkspace })
    commit(SET_WORKSPACE, newWorkspace)
  }
}

export const mutations = {
  [SET_WORKSPACE](state, workspace) {
    state.user.workspace = workspace
  },

  [SET_USER](state, user) {
    state.user = user
  }
}
