refactor: vuex settings module mutation names

This commit is contained in:
Valentin Kaelin 2020-02-13 20:50:45 +01:00
parent 13e12f7ccf
commit 8fffd9c10f
2 changed files with 8 additions and 17 deletions

View file

@ -32,7 +32,7 @@ export default {
created() { created() {
this.updatePercent() this.updatePercent()
this.updateSettings({name: 'region'}) this.updateSettings({name: 'region'})
this.updateSettings({name: 'recent_searches', isJson: true}) this.updateSettings({name: 'recentSearches', isJson: true})
this.updateSettings({name: 'favorites', isJson: true}) this.updateSettings({name: 'favorites', isJson: true})
}, },

View file

@ -34,28 +34,19 @@ export const mutations = {
REMOVE_SEARCH(state, summonerName) { REMOVE_SEARCH(state, summonerName) {
state.recentSearches = state.recentSearches.filter(s => s.name !== summonerName) state.recentSearches = state.recentSearches.filter(s => s.name !== summonerName)
}, },
UPDATE_FAVORITES(state, favorites) { UPDATE_SETTING(state, { name, value }) {
state.favorites = favorites state[name] = value
}, }
UPDATE_PERCENT(state, percent) {
state.percent = percent
},
UPDATE_RECENT_SEARCHES(state, recentSearches) {
state.recentSearches = recentSearches
},
UPDATE_REGION(state, region) {
state.region = region
},
} }
export const actions = { export const actions = {
addRecentSearch({ commit, dispatch, state }, summoner) { addRecentSearch({ commit, dispatch, state }, summoner) {
commit('ADD_SEARCH', summoner) commit('ADD_SEARCH', summoner)
dispatch('updateSettings', { name: 'recent_searches', value: state.recentSearches, isJson: true }) dispatch('updateSettings', { name: 'recentSearches', value: state.recentSearches, isJson: true })
}, },
removeRecentSearch({ commit, dispatch }, summoner) { removeRecentSearch({ commit, dispatch }, summoner) {
commit('REMOVE_SEARCH', summoner.name) commit('REMOVE_SEARCH', summoner.name)
dispatch('updateSettings', { name: 'recent_searches', value: state.recentSearches, isJson: true }) dispatch('updateSettings', { name: 'recentSearches', value: state.recentSearches, isJson: true })
}, },
updateFavorite({ commit, dispatch, state }, summoner) { updateFavorite({ commit, dispatch, state }, summoner) {
const alreadyFav = state.favorites.find(s => s.name === summoner.name) const alreadyFav = state.favorites.find(s => s.name === summoner.name)
@ -80,7 +71,7 @@ export const actions = {
} else { } else {
localStorage.setItem('settings-percent', percent) localStorage.setItem('settings-percent', percent)
} }
commit('UPDATE_PERCENT', percent) commit('UPDATE_SETTING', { name: 'percent', value: percent })
}, },
updateSettings({ commit }, { name, value, isJson = false }) { updateSettings({ commit }, { name, value, isJson = false }) {
if (!value) { if (!value) {
@ -90,6 +81,6 @@ export const actions = {
} else { } else {
localStorage.setItem(name, isJson ? JSON.stringify(value) : value) localStorage.setItem(name, isJson ? JSON.stringify(value) : value)
} }
commit(`UPDATE_${name.toUpperCase()}`, value) commit('UPDATE_SETTING', { name, value })
} }
} }