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

86 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-09-08 20:08:49 +00:00
import { axios } from '@/plugins/axios'
import { createMatchData, createSummonerData } from '@/helpers/summoner'
2019-09-08 20:08:49 +00:00
export const namespaced = true
export const state = {
infos: {
account: {},
matchIndex: 0,
matchList: [],
matches: [],
2019-10-08 19:54:32 +00:00
ranked: {}
},
matchesLoading: false,
2019-09-11 20:02:05 +00:00
status: '',
2019-09-08 20:08:49 +00:00
}
export const mutations = {
MATCHES_LOADING(state) {
state.matchesLoading = true
},
MATCHES_FOUND(state, newMatches) {
state.matchesLoading = false
state.infos.matches = [...state.infos.matches, ...newMatches]
state.infos.matchIndex += newMatches.length
},
2019-09-08 20:08:49 +00:00
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.account = infos.account
state.infos.matchList = infos.matchList
state.infos.matches = infos.matches
2019-10-08 19:54:32 +00:00
state.infos.ranked = infos.ranked
state.infos.matchIndex = infos.matches.length
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 moreMatches({ commit }) {
commit('MATCHES_LOADING')
const account = state.infos.account
const gameIds = state.infos.matchList.slice(state.infos.matchIndex, state.infos.matchIndex + 10).map(({ gameId }) => gameId)
const resp = await axios(({ url: 'match', data: { account, gameIds }, method: 'POST' })).catch(() => { })
commit('MATCHES_FOUND', createMatchData(resp.data))
},
2019-09-08 20:08:49 +00:00
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 = {
matchesLoading: state => state.matchesLoading,
moreMatchesToFetch: state => state.infos.matchIndex < state.infos.matchList.length,
2019-09-11 20:02:05 +00:00
summonerFound: state => state.status === 'found',
summonerNotFound: state => state.status === 'error',
summonerLoading: state => state.status === 'loading',
}