From 6129086ef611f886dac8a66378199d95c16d8ec7 Mon Sep 17 00:00:00 2001 From: Valentin Kaelin Date: Thu, 6 Feb 2020 19:45:22 +0100 Subject: [PATCH] feat: save last searched region in localStorage --- client/src/App.vue | 3 ++- client/src/components/SearchForm.vue | 19 ++++++++++++------- client/src/layouts/Default.vue | 5 ++--- client/src/store/index.js | 11 ----------- client/src/store/modules/detailedMatch.js | 20 ++++++++++---------- client/src/store/modules/settings.js | 15 ++++++++++++++- client/src/store/modules/summoner.js | 4 +++- 7 files changed, 43 insertions(+), 34 deletions(-) diff --git a/client/src/App.vue b/client/src/App.vue index 53f0413..20aed67 100644 --- a/client/src/App.vue +++ b/client/src/App.vue @@ -31,10 +31,11 @@ export default { created() { this.updatePercent() + this.updateSettings({name: 'region'}) }, methods: { - ...mapActions('settings', ['updatePercent']), + ...mapActions('settings', ['updatePercent', 'updateSettings']), } } diff --git a/client/src/components/SearchForm.vue b/client/src/components/SearchForm.vue index e49ea74..c8bcb57 100644 --- a/client/src/components/SearchForm.vue +++ b/client/src/components/SearchForm.vue @@ -14,7 +14,7 @@ :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" > - {{ selectedRegion }} + {{ selectedRegion }} @@ -36,12 +36,12 @@
diff --git a/client/src/layouts/Default.vue b/client/src/layouts/Default.vue index 4a85b61..bef43ac 100644 --- a/client/src/layouts/Default.vue +++ b/client/src/layouts/Default.vue @@ -161,13 +161,12 @@ export default { watch: { uri() { console.log('route changed') - this.updateCurrentRegion(this.region) + this.updateSettings({ name: 'region', value: this.region.toLowerCase() }) this.apiCall() } }, created() { - this.updateCurrentRegion(this.region) this.apiCall() }, @@ -183,7 +182,7 @@ export default { redirect(summoner, region) { this.$router.push(`/summoner/${region}/${summoner}`) }, - ...mapActions(['updateCurrentRegion']), + ...mapActions('settings', ['updateSettings']), ...mapActions('summoner', ['basicRequest']), } } diff --git a/client/src/store/index.js b/client/src/store/index.js index 00faafd..3f9e7bc 100644 --- a/client/src/store/index.js +++ b/client/src/store/index.js @@ -19,7 +19,6 @@ export default new Vuex.Store({ summoner }, state: { - currentRegion: 'euw1', regionsList: { 'br': 'br1', 'eune': 'eun1', @@ -35,15 +34,5 @@ export default new Vuex.Store({ }, 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 }) diff --git a/client/src/store/modules/detailedMatch.js b/client/src/store/modules/detailedMatch.js index c7c0d67..eff2095 100644 --- a/client/src/store/modules/detailedMatch.js +++ b/client/src/store/modules/detailedMatch.js @@ -8,19 +8,19 @@ export const state = { } export const mutations = { - MATCHE_LOADING(state, gameId) { + MATCH_LOADING(state, gameId) { const alreadyIn = state.matches.find(m => m.gameId === gameId) if (!alreadyIn) { state.matches.push({ gameId: gameId, status: 'loading' }) } }, - MATCHE_FOUND(state, matchDetails) { + MATCH_FOUND(state, matchDetails) { matchDetails.status = 'loaded' const index = state.matches.findIndex(m => m.gameId === matchDetails.gameId) 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) match.blueTeam.players = blueTeam match.redTeam.players = redTeam @@ -29,21 +29,21 @@ export const mutations = { export const actions = { async matchDetails({ commit, rootState }, gameId) { - commit('MATCHE_LOADING', gameId) - console.log('MATCH DETAILS STORE', gameId, rootState.currentRegion) + commit('MATCH_LOADING', gameId) + 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(resp.data) - // const detailedMatch = createDetailedMatchData(resp.data.matchDetails) - commit('MATCHE_FOUND', resp.data.matchDetails) + commit('MATCH_FOUND', resp.data.matchDetails) // If the ranks of the players are not yet known 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(ranks.data) - commit('MATCHE_RANKS_FOUND', { gameId, ...ranks.data }) + commit('MATCH_RANKS_FOUND', { gameId, ...ranks.data }) } } } diff --git a/client/src/store/modules/settings.js b/client/src/store/modules/settings.js index 8710437..2409b91 100644 --- a/client/src/store/modules/settings.js +++ b/client/src/store/modules/settings.js @@ -1,12 +1,16 @@ export const namespaced = true export const state = { - percent: false + percent: false, + region: 'euw', } export const mutations = { UPDATE_PERCENT(state, percent) { state.percent = percent + }, + UPDATE_REGION(state, region) { + state.region = region } } @@ -18,5 +22,14 @@ export const actions = { localStorage.setItem('settings-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) } } diff --git a/client/src/store/modules/summoner.js b/client/src/store/modules/summoner.js index 024edfa..355da43 100644 --- a/client/src/store/modules/summoner.js +++ b/client/src/store/modules/summoner.js @@ -144,7 +144,9 @@ export const actions = { }, async liveMatchRequest({ commit, rootState }) { 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(resp.data)