import { Action, Module } from 'vuex-module-decorators'
import { BaseUserModule } from '@/store/BaseUserModule'
import { UserService } from '@/services'
import { NotifyHelper } from '@/helpers'

@Module({
  name: 'TaskUserModule',
  namespaced: true,
})
export class TaskUserModule extends BaseUserModule {
  @Action
  async loadUser() {
    if (this.user) {
      return
    }

    this.setLoading(true)

    return UserService.loadUser(process.env.TASKS_API_URL)
      .then(user => this.setUser(user))
      .catch(() => NotifyHelper.error('Вы не авторизованы'))
      .finally(() => this.setLoading(false))
  }

  @Action
  logout() {
    return UserService.logout()
      .then(() => this.setUser(null))
      .catch(() => NotifyHelper.serverError())
  }
}
