From 9f0466822a97ce413d200f98a63c3c58caa600d6 Mon Sep 17 00:00:00 2001 From: Marcel Schneider Date: Thu, 5 Apr 2018 22:37:38 +0200 Subject: [PATCH] Use vuex state store --- src/App.vue | 36 +++++++++++++++--------- src/components/Mensa.vue | 51 +++++---------------------------- src/store/index.js | 61 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 60 deletions(-) diff --git a/src/App.vue b/src/App.vue index 61f142a..decfebf 100644 --- a/src/App.vue +++ b/src/App.vue @@ -40,20 +40,10 @@ export default { 4: 'Donnerstag', 5: 'Freitag', 6: 'Samstag' - }, - where: 'zentralmensa', - when: '' + (new Date().getDay() || 1) + } } }, - watch: { - 'where': 'updateRoute', - 'when': 'updateRoute' - }, methods: { - updateRoute () { - this.$router.push(`/${this.where}/${this.when}`) - window.localStorage.setItem('where', this.where) - }, previousDay () { this.when === 1 ? this.when = 6 : this.when-- }, @@ -67,12 +57,30 @@ export default { } }, computed: { - today () { - return '' + new Date().getDay() + where: { + get () { + return this.$store.state.where + }, + set (value) { + this.$store.dispatch('loadMeals', { + when: this.when, + where: value + }) + } + }, + when: { + get () { + return this.$store.state.when + }, + set (value) { + this.$store.dispatch('loadMeals', { + when: value, + where: this.where + }) + } } }, created () { - this.where = window.localStorage.getItem('where') || 'zentralmensa' let vm = this document.onkeydown = (evt) => { evt = evt || window.event diff --git a/src/components/Mensa.vue b/src/components/Mensa.vue index 64f5361..54ed797 100644 --- a/src/components/Mensa.vue +++ b/src/components/Mensa.vue @@ -9,52 +9,15 @@ diff --git a/src/store/index.js b/src/store/index.js index a559b01..f8337f3 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -5,9 +5,66 @@ Vue.use(Vuex) const state = { where: 'zentralmensa', - day: new Date().getDay() || 1 + when: new Date().getDay() || 1, + meals: {}, + date: '' +} + +const mutations = { + updateWhere(state, where) { + state.where = where + }, + + updateWhen(state, when) { + state.when = when + }, + + updateMeals(state, meals) { + state.meals = meals + }, + + updateDate(state, date) { + state.date = date + } +} + +const actions = { + /** + * Loads the meals of the desired day and mensa + * @param {Object} payload The payload for this action + * @param {string} payload.where The desired Mensa + * @param {number} payload.when The desired day + */ + loadMeals({ commit, state }, payload) { + commit('updateWhere', payload.where) + commit('updateWhen', payload.when) + fetch(`/static/${state.where}.${state.when}.json`) + .then(res => res.json()) + .then(menu => { + commit('updateMeals', menu.meals) + commit('updateDate', menu.date) + }) + } +} + +const getters = { + getWhere(state) { + return state.where + }, + getWhen(state) { + return state.when + }, + getMeals(state) { + return state.meals + }, + getDate(state) { + return state.date + } } export default new Vuex.Store({ - state + state, + mutations, + actions, + getters })