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

127 lines
4 KiB
JavaScript
Raw Normal View History

2019-09-08 20:08:49 +00:00
import { axios } from '@/plugins/axios'
import { createMatchData, createBasicSummonerData } from '@/helpers/summoner'
2019-09-08 20:08:49 +00:00
export const namespaced = true
export const state = {
basic: {
account: {},
matchList: [],
ranked: {},
playing: false,
status: '',
},
overview: {
matchIndex: 0,
matches: [],
stats: {},
loaded: false,
matchesLoading: false,
},
champions: {
list: [],
championsLoaded: false
},
2019-09-08 20:08:49 +00:00
}
export const mutations = {
BASIC_REQUEST(state) {
state.basic.status = 'loading'
},
2019-12-21 16:56:31 +00:00
CHAMPIONS_FOUND(state, { champions }) {
state.champions.list = champions
state.champions.championsLoaded = true
2019-12-21 16:56:31 +00:00
},
MATCHES_LOADING(state) {
state.overview.matchesLoading = true
},
MATCHES_FOUND(state, { newMatches, stats }) {
state.overview.matchesLoading = false
state.overview.matches = [...state.infos.matches, ...newMatches]
state.overview.matchIndex += newMatches.length
state.overview.stats = stats
state.champions.championsLoaded = false
},
OVERVIEW_FOUND(state, infos) {
state.overview.matches = infos.matches
state.overview.matchIndex = infos.matches.length
state.overview.stats = infos.stats
state.overview.loaded = true
2019-09-08 20:08:49 +00:00
},
SUMMONER_FOUND(state, infos) {
state.basic.account = infos.account
state.basic.matchList = infos.matchList
state.basic.ranked = infos.ranked
state.basic.playing = infos.playing
state.basic.status = 'found'
state.champions.championsLoaded = false
2019-09-08 20:08:49 +00:00
},
SUMMONER_NOT_FOUND(state) {
state.basic.status = 'error'
},
2019-09-08 20:08:49 +00:00
}
export const actions = {
async basicRequest({ commit, dispatch, rootState }, { summoner, region }) {
region = rootState.regionsList[region]
commit('BASIC_REQUEST')
2019-09-08 20:08:49 +00:00
try {
const resp = await axios(({ url: 'summoner-basic', data: { summoner, region }, method: 'POST' }))
2019-09-08 20:08:49 +00:00
if (resp.data) {
console.log('--- SUMMONER INFOS ---')
console.log(resp.data)
const infos = createBasicSummonerData(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)
}
},
async championStats({ commit }, queue = null) {
const resp = await axios(({ url: 'summoner-champions', data: { puuid: state.basic.account.puuid, queue: queue }, method: 'POST' })).catch(() => { })
console.log('CHAMPIONS STATS')
console.log('queue: ', queue)
console.log(resp.data)
commit('CHAMPIONS_FOUND', { champions: resp.data })
},
async moreMatches({ commit }) {
commit('MATCHES_LOADING')
const account = state.basic.account
const gameIds = state.basic.matchList.slice(state.overview.matchIndex, state.overview.matchIndex + 10).map(({ gameId }) => gameId)
const resp = await axios(({ url: 'match', data: { account, gameIds }, method: 'POST' })).catch(() => { })
console.log('--- MATCHES INFOS ---')
console.log(resp.data)
const newMatches = createMatchData(resp.data.matches)
commit('MATCHES_FOUND', { newMatches, stats: resp.data.stats })
},
async overviewRequest({ commit }) {
const resp = await axios(({ url: 'summoner-overview', data: { account: state.basic.account }, method: 'POST' })).catch(() => { })
console.log('OVERVIEW')
console.log(resp.data)
resp.data.matches = createMatchData(resp.data.matchesDetails)
commit('OVERVIEW_FOUND', resp.data)
2019-09-08 20:08:49 +00:00
}
}
2019-09-11 20:02:05 +00:00
export const getters = {
matchesLoading: state => state.overview.matchesLoading,
moreMatchesToFetch: state => state.overview.matchIndex < state.basic.matchList.length,
overviewLoaded: state => state.overview.loaded,
playing: state => state.basic.playing,
summonerFound: state => state.basic.status === 'found',
summonerNotFound: state => state.basic.status === 'error',
summonerLoading: state => state.basic.status === 'loading',
2019-09-11 20:02:05 +00:00
}