import { axios } from '@/plugins/axios' import { createSummonerData } from '@/helpers/summoner' export const namespaced = true export const state = { infos: [], status: '', } export const mutations = { SUMMONER_REQUEST(state) { state.status = 'loading' }, SUMMONER_FOUND(state, infos) { state.infos = infos state.status = 'found' }, SUMMONER_NOT_FOUND(state) { state.status = 'error' } } export const actions = { async summonerRequest({ commit, dispatch, rootState }, { summoner, region }) { region = rootState.regionsList[region] commit('SUMMONER_REQUEST') try { const resp = await axios(({ url: 'api', data: { summoner, region }, method: 'POST' })) if (resp.data) { const infos = createSummonerData(resp.data, rootState.ddragon.champions, rootState.ddragon.runes) 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) } } } export const getters = { summonerFound: state => state.status === 'found', summonerNotFound: state => state.status === 'error', summonerLoading: state => state.status === 'loading', }