feat: save last searched region in localStorage

This commit is contained in:
Valentin Kaelin 2020-02-06 19:45:22 +01:00
parent 941c7850a9
commit 6129086ef6
7 changed files with 43 additions and 34 deletions

View file

@ -31,10 +31,11 @@ export default {
created() { created() {
this.updatePercent() this.updatePercent()
this.updateSettings({name: 'region'})
}, },
methods: { methods: {
...mapActions('settings', ['updatePercent']), ...mapActions('settings', ['updatePercent', 'updateSettings']),
} }
} }
</script> </script>

View file

@ -14,7 +14,7 @@
:class="{'border-teal-200': dropdown}" :class="{'border-teal-200': dropdown}"
class="border-2 border-transparent cursor-pointer flex items-center px-2 py-1 rounded transition-all transition-fast ease-in-quad ease-out-quad hover:text-white" class="border-2 border-transparent cursor-pointer flex items-center px-2 py-1 rounded transition-all transition-fast ease-in-quad ease-out-quad hover:text-white"
> >
<span class="selected font-bold select-none">{{ selectedRegion }}</span> <span class="selected font-bold uppercase select-none">{{ selectedRegion }}</span>
<svg class="ml-1 -mr-1 w-4 h-4"> <svg class="ml-1 -mr-1 w-4 h-4">
<use xlink:href="#caret-down" /> <use xlink:href="#caret-down" />
</svg> </svg>
@ -36,12 +36,12 @@
<div <div
v-for="(region, index) in regions" v-for="(region, index) in regions"
:key="region" :key="region"
@click="selectedRegion = region" @click="updateSettings({name: 'region', value: region.toLowerCase()})"
:class="classRegions(index)" :class="classRegions(index)"
class="relative pr-2 pl-5 py-1 text-xs text-right bg-blue-1000 hover:bg-blue-800" class="relative pr-2 pl-5 py-1 text-xs text-right bg-blue-1000 hover:bg-blue-800"
> >
<svg <svg
v-if="region === selectedRegion" v-if="region.toLowerCase() === selectedRegion"
class="absolute vertical-center offsetIcon w-3 h-3 fill-current" class="absolute vertical-center offsetIcon w-3 h-3 fill-current"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 512 512" viewBox="0 0 512 512"
@ -68,6 +68,8 @@
</template> </template>
<script> <script>
import { mapActions, mapState } from 'vuex'
export default { export default {
props: { props: {
size: { size: {
@ -92,7 +94,6 @@ export default {
'TR', 'TR',
'RU' 'RU'
], ],
selectedRegion: 'EUW'
} }
}, },
computed: { computed: {
@ -120,7 +121,10 @@ export default {
'offsetDropDown': this.size === 'small', 'offsetDropDown': this.size === 'small',
'offsetDropDownXl': this.size === 'xl' 'offsetDropDownXl': this.size === 'xl'
} }
} },
...mapState({
selectedRegion: state => state.settings.region
}),
}, },
methods: { methods: {
classRegions(index) { classRegions(index) {
@ -131,9 +135,10 @@ export default {
formSubmit() { formSubmit() {
const search = this.summoner.split(' ').join('') const search = this.summoner.split(' ').join('')
if (search.length) { if (search.length) {
this.$emit('formSubmit', search, this.selectedRegion.toLowerCase()) this.$emit('formSubmit', search, this.selectedRegion)
}
} }
},
...mapActions('settings', ['updateSettings']),
} }
} }
</script> </script>

View file

@ -161,13 +161,12 @@ export default {
watch: { watch: {
uri() { uri() {
console.log('route changed') console.log('route changed')
this.updateCurrentRegion(this.region) this.updateSettings({ name: 'region', value: this.region.toLowerCase() })
this.apiCall() this.apiCall()
} }
}, },
created() { created() {
this.updateCurrentRegion(this.region)
this.apiCall() this.apiCall()
}, },
@ -183,7 +182,7 @@ export default {
redirect(summoner, region) { redirect(summoner, region) {
this.$router.push(`/summoner/${region}/${summoner}`) this.$router.push(`/summoner/${region}/${summoner}`)
}, },
...mapActions(['updateCurrentRegion']), ...mapActions('settings', ['updateSettings']),
...mapActions('summoner', ['basicRequest']), ...mapActions('summoner', ['basicRequest']),
} }
} }

View file

@ -19,7 +19,6 @@ export default new Vuex.Store({
summoner summoner
}, },
state: { state: {
currentRegion: 'euw1',
regionsList: { regionsList: {
'br': 'br1', 'br': 'br1',
'eune': 'eun1', 'eune': 'eun1',
@ -35,15 +34,5 @@ export default new Vuex.Store({
}, },
roles: ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT'] roles: ['TOP', 'JUNGLE', 'MIDDLE', 'BOTTOM', 'SUPPORT']
}, },
mutations: {
UPDATE_REGION(state, newRegion) {
state.currentRegion = state.regionsList[newRegion]
}
},
actions: {
updateCurrentRegion({ commit }, newRegion) {
commit('UPDATE_REGION', newRegion)
}
},
strict: debug strict: debug
}) })

View file

@ -8,19 +8,19 @@ export const state = {
} }
export const mutations = { export const mutations = {
MATCHE_LOADING(state, gameId) { MATCH_LOADING(state, gameId) {
const alreadyIn = state.matches.find(m => m.gameId === gameId) const alreadyIn = state.matches.find(m => m.gameId === gameId)
if (!alreadyIn) { if (!alreadyIn) {
state.matches.push({ gameId: gameId, status: 'loading' }) state.matches.push({ gameId: gameId, status: 'loading' })
} }
}, },
MATCHE_FOUND(state, matchDetails) { MATCH_FOUND(state, matchDetails) {
matchDetails.status = 'loaded' matchDetails.status = 'loaded'
const index = state.matches.findIndex(m => m.gameId === matchDetails.gameId) const index = state.matches.findIndex(m => m.gameId === matchDetails.gameId)
Vue.set(state.matches, index, matchDetails) Vue.set(state.matches, index, matchDetails)
}, },
MATCHE_RANKS_FOUND(state, { gameId, blueTeam, redTeam }) { MATCH_RANKS_FOUND(state, { gameId, blueTeam, redTeam }) {
const match = state.matches.find(m => m.gameId === gameId) const match = state.matches.find(m => m.gameId === gameId)
match.blueTeam.players = blueTeam match.blueTeam.players = blueTeam
match.redTeam.players = redTeam match.redTeam.players = redTeam
@ -29,21 +29,21 @@ export const mutations = {
export const actions = { export const actions = {
async matchDetails({ commit, rootState }, gameId) { async matchDetails({ commit, rootState }, gameId) {
commit('MATCHE_LOADING', gameId) commit('MATCH_LOADING', gameId)
console.log('MATCH DETAILS STORE', gameId, rootState.currentRegion) const region = rootState.regionsList[rootState.settings.region]
console.log('MATCH DETAILS STORE', gameId, region)
const resp = await axios(({ url: 'match-details', data: { gameId, region: rootState.currentRegion }, method: 'POST' })).catch(() => { }) const resp = await axios(({ url: 'match-details', data: { gameId, region }, method: 'POST' })).catch(() => { })
console.log('--- DETAILS INFOS ---') console.log('--- DETAILS INFOS ---')
console.log(resp.data) console.log(resp.data)
// const detailedMatch = createDetailedMatchData(resp.data.matchDetails) commit('MATCH_FOUND', resp.data.matchDetails)
commit('MATCHE_FOUND', resp.data.matchDetails)
// If the ranks of the players are not yet known // If the ranks of the players are not yet known
if (resp.data.matchDetails.blueTeam.players[0].rank === undefined) { if (resp.data.matchDetails.blueTeam.players[0].rank === undefined) {
const ranks = await axios(({ url: 'match-details-ranks', data: { gameId, region: rootState.currentRegion }, method: 'POST' })).catch(() => { }) const ranks = await axios(({ url: 'match-details-ranks', data: { gameId, region }, method: 'POST' })).catch(() => { })
console.log('--- RANK OF MATCH DETAILS ---') console.log('--- RANK OF MATCH DETAILS ---')
console.log(ranks.data) console.log(ranks.data)
commit('MATCHE_RANKS_FOUND', { gameId, ...ranks.data }) commit('MATCH_RANKS_FOUND', { gameId, ...ranks.data })
} }
} }
} }

View file

@ -1,12 +1,16 @@
export const namespaced = true export const namespaced = true
export const state = { export const state = {
percent: false percent: false,
region: 'euw',
} }
export const mutations = { export const mutations = {
UPDATE_PERCENT(state, percent) { UPDATE_PERCENT(state, percent) {
state.percent = percent state.percent = percent
},
UPDATE_REGION(state, region) {
state.region = region
} }
} }
@ -18,5 +22,14 @@ export const actions = {
localStorage.setItem('settings-percent', percent) localStorage.setItem('settings-percent', percent)
} }
commit('UPDATE_PERCENT', percent) commit('UPDATE_PERCENT', percent)
},
async updateSettings({ commit }, { name, value }) {
if (!value) {
value = localStorage.getItem(name)
if (!value) return
} else {
localStorage.setItem(name, value)
}
commit(`UPDATE_${name.toUpperCase()}`, value)
} }
} }

View file

@ -144,7 +144,9 @@ export const actions = {
}, },
async liveMatchRequest({ commit, rootState }) { async liveMatchRequest({ commit, rootState }) {
commit('LIVE_LOADING') commit('LIVE_LOADING')
const resp = await axios(({ url: 'summoner-live', data: { account: state.basic.account, region: rootState.currentRegion }, method: 'POST' })).catch(() => { }) const region = rootState.regionsList[rootState.settings.region]
const resp = await axios(({ url: 'summoner-live', data: { account: state.basic.account, region }, method: 'POST' })).catch(() => { })
console.log('---LIVE---') console.log('---LIVE---')
console.log(resp.data) console.log(resp.data)