import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const state = { where: 'zentralmensa', when: new Date().getDay() || 1, meals: {}, date: '' } const mutations = { updateWhere(state, where) { state.where = where window.localStorage.setItem('mensa_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, mutations, actions, getters })