c3lf-system-3/src/store/index.js

86 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-11-13 21:40:14 +00:00
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
//import * as _ from 'lodash/fp';
import router from '../router';
2019-11-13 21:40:14 +00:00
2019-12-05 21:26:45 +00:00
import getAuth from './auth';
2019-11-13 21:40:14 +00:00
Vue.use(Vuex);
2019-12-05 21:26:45 +00:00
const store = new Vuex.Store({
2019-11-14 02:59:17 +00:00
state: {
2019-12-12 18:00:58 +00:00
events: [],
2019-11-15 19:00:40 +00:00
layout: 'cards',
2019-12-12 18:00:58 +00:00
loadedItems: [],
loadedBoxes: [],
2019-12-17 13:32:45 +00:00
apiUrl: 'https://c3lf.de/api',
2019-11-14 03:14:47 +00:00
},
getters: {
2019-12-12 18:25:55 +00:00
getEventSlug: state => state.route && state.route.params.event? state.route.params.event : state.events.length ? state.events[0].slug : '36C3',
2019-12-12 18:00:58 +00:00
getActiveView: state => state.route.name || 'items',
2019-12-12 23:06:22 +00:00
getFilters: state => state.route.query,
},
2019-11-14 03:14:47 +00:00
mutations: {
replaceEvents(state, events) {
state.events = events;
},
2019-12-12 18:00:58 +00:00
changeView(state, {view, slug}) {
router.push({path: `/${slug}/${view}`});
},
replaceLoadedItems(state, newItems) {
2019-12-15 19:55:42 +00:00
state.loadedItems = newItems.map(e => ({...e, uid: e.item_uid}));
2019-11-15 19:00:40 +00:00
},
setLayout(state, layout) {
state.layout = layout;
2019-12-12 18:25:55 +00:00
},
replaceBoxes(state, loadedBoxes) {
state.loadedBoxes = loadedBoxes;
},
2019-11-14 03:14:47 +00:00
},
actions: {
2019-12-17 13:32:45 +00:00
async loadEvents({ commit, state }) {
const resp = await axios.get(`${state.apiUrl}/1/events`, {
2019-12-05 21:26:45 +00:00
auth: getAuth(),
});
commit('replaceEvents', resp.data);
},
2019-12-12 18:00:58 +00:00
changeEvent({ dispatch, getters}, eventName) {
router.push({path: `/${eventName.slug}/${getters.getActiveView}`});
dispatch('loadEventItems', eventName);
},
2019-12-12 18:00:58 +00:00
changeView({ getters }, link) {
router.push({path: `/${getters.getEventSlug}/${link.path}`});
2019-12-05 22:27:24 +00:00
},
2019-12-17 13:32:45 +00:00
async loadEventItems({ commit, state, getters }) {
const resp = await axios.get(`${state.apiUrl}/1/${getters.getEventSlug}/items`, {
2019-12-05 21:26:45 +00:00
auth: getAuth(),
});
commit('replaceLoadedItems', resp.data);
2019-12-12 18:25:55 +00:00
},
2019-12-17 13:32:45 +00:00
async loadBoxes({ commit, state }) {
const resp = await axios.get(`${state.apiUrl}/1/boxes`, {
2019-12-12 18:25:55 +00:00
auth: getAuth(),
});
commit('replaceBoxes', resp.data);
},
2019-12-17 13:32:45 +00:00
async updateItem({ getters, state }, item) {
axios.put(`${state.apiUrl}/1/${getters.getEventSlug}/item/${item.iid}`, item, {
2019-12-14 22:51:06 +00:00
auth: getAuth(),
});
}
2019-11-14 02:59:17 +00:00
}
2019-11-13 21:40:14 +00:00
});
export default store;
2019-12-12 18:33:58 +00:00
store.dispatch('loadEvents').then(() =>{
store.dispatch('loadEventItems');
store.dispatch('loadBoxes');
}
2019-12-12 18:26:58 +00:00
);