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

@Module({
  name: 'MessageUserModule',
  namespaced: true,
})
export class MessageUserModule extends BaseUserModule {
  @Action
  loadUser() {
    this.setLoading(true)

    return UserService.loadUser(process.env.MESSAGES_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())
  }
}
