LeagueStats/client/src/store/modules/summoner.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-09-08 20:08:49 +00:00
import { axios } from '@/plugins/axios'
2019-09-09 18:42:10 +00:00
import { createSummonerData } from '@/helpers/summoner'
2019-09-08 20:08:49 +00:00
export const namespaced = true
export const state = {
infos: [],
2019-09-11 20:02:05 +00:00
status: '',
2019-09-08 20:08:49 +00:00
}
export const mutations = {
SUMMONER_REQUEST(state) {
2019-09-11 20:02:05 +00:00
state.status = 'loading'
2019-09-08 20:08:49 +00:00
},
SUMMONER_FOUND(state, infos) {
state.infos = infos
2019-09-11 20:02:05 +00:00
state.status = 'found'
2019-09-08 20:08:49 +00:00
},
SUMMONER_NOT_FOUND(state) {
2019-09-11 20:02:05 +00:00
state.status = 'error'
2019-09-08 20:08:49 +00:00
}
}
export const actions = {
async summonerRequest({ commit, dispatch, rootState }, { summoner, region }) {
region = rootState.regionsList[region]
2019-09-08 20:08:49 +00:00
commit('SUMMONER_REQUEST')
try {
const resp = await axios(({ url: 'api', data: { summoner, region }, method: 'POST' }))
if (resp.data) {
const infos = createSummonerData(resp.data)
2019-09-08 20:08:49 +00:00
commit('SUMMONER_FOUND', infos)
} else {
commit('SUMMONER_NOT_FOUND')
dispatch('notification/add', {
type: 'error',
message: 'Summoner not found.'
}, { root: true })
console.log('Summoner not found - store')
}
} catch (error) {
commit('SUMMONER_NOT_FOUND')
console.log(error)
}
}
}
2019-09-11 20:02:05 +00:00
export const getters = {
summonerFound: state => state.status === 'found',
summonerNotFound: state => state.status === 'error',
summonerLoading: state => state.status === 'loading',
}