populate events trough apicall and save active event in url

This commit is contained in:
j3d1 2019-12-05 04:32:33 +01:00
parent fbf6d9dc91
commit cc9ba38dac
11 changed files with 137 additions and 61 deletions

View file

@ -1,16 +1,14 @@
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
//import * as _ from 'lodash/fp';
import router from '../router';
Vue.use(Vuex);
export default new Vuex.Store({
const store = new Vuex.Store({
state: {
events: [
{'slug': '35C3'},
{'slug': 'camp19'},
{'slug': '36C3'}
],
activeEvent: {'slug': '36C3'},
events: Array,
layout: 'cards',
loadedItems: [
{ uid: 1, description: 'sleeping bag', box: 7, image: 41 },
@ -29,19 +27,55 @@ export default new Vuex.Store({
{ uid: 14, description: 'toy truck', box: 6, image: 51 }
]
},
getters: {
getEventSlug: state => state.route.params.event,
},
mutations: {
replaceEvents(state, events) {
state.events = events;
//if (!state.activeEvent || !events.includes(state.activeEvent))
// state.activeEvent = _.reverse(events)[0];
},
changeEvent(state, event) {
state.activeEvent = event;
router.push({path: `/${event.slug}`});
},
replaceLoadedItems(state, newItems) {
state.loadedItems = newItems;
},
setLayout(state, layout) {
state.layout = layout;
}
},
actions: {
changeEvent({ commit }, event) {
// todo: load items from server
// todo: load items from server
commit('changeEvent', event);
async loadEvents({ commit }) {
const resp = await axios.get('https://c3lf.de/api/1/events', {
auth: {
username: process.env.VUE_APP_CONFIG_API_USER,
password: process.env.VUE_APP_CONFIG_API_PASSWORD
}
});
commit('replaceEvents', resp.data);
},
changeEvent({ commit, dispatch }, eventName) {
commit('changeEvent', eventName);
dispatch('loadEventItems', eventName);
},
async loadEventItems({ commit, state }) {
const resp = await axios.get(`https://c3lf.de/api/1/${state.route.params.event}/items`, {
auth: {
username: process.env.VUE_APP_CONFIG_API_USER,
password: process.env.VUE_APP_CONFIG_API_PASSWORD
}
});
console.log(resp.data);
commit('replaceLoadedItems', resp.data);
}
}
});
export default store;
store.dispatch('loadEvents');