import axios from 'axios' import handleApiError from '../handleApiError' const state = { shows: [], timeslots: [], loaded: { shows: false, timeslots: false, schedules: false, }, selected: { index: 0, // index of the currently selected show in our shows array id: 0, // actual id of the currently selected show } } const getters = { shows: state => state.shows, selectedShow: state => state.shows[state.selected.index] } const mutations = { loading(state, item) { state.loaded[item] = false }, finishLoading(state, item) { state.loaded[item] = true }, setShows(state, shows) { state.shows = shows }, setTimeslots(state, slots) { state.timeslots = slots }, switchShow(state, index) { state.selected.index = index state.selected.id = state.shows[index].id } } const actions = { fetchShows (ctx, data) { ctx.commit('loading', 'shows') let uri = process.env.VUE_APP_API_STEERING + 'shows' axios.get(uri, { withCredentials: true, headers: { 'Authorization': 'Bearer ' + ctx.rootState.auth.user.access_token } }).then(response => { ctx.commit('setShows', response.data) ctx.commit('finishLoading', 'shows') if (data && typeof(data.callback) === 'function') { data.callback() } }).catch(error => { handleApiError(this, error, 'could not load shows') if (data && typeof(data.callbackCancel) === 'function') { data.callbackCancel() } }) }, fetchTimeslots (ctx, data) { ctx.commit('loading', 'timeslots') let uri = process.env.VUE_APP_API_STEERING + 'timeslots?start=' + data.start + '&end=' + data.end axios.get(uri, { withCredentials: true, headers: { 'Authorization': 'Bearer ' + ctx.rootState.auth.user.access_token } }).then(response => { ctx.commit('setTimeslots', response.data) ctx.commit('finishLoading', 'timeslots') if (data && typeof(data.callback) === 'function') { data.callback() } }).catch(error => { handleApiError(this, error, 'could not load timeslots') if (data && typeof(data.callbackCancel) === 'function') { data.callbackCancel() } }) }, submitSchedule (ctx, data) { ctx.commit('loading', 'schedules') let uri = process.env.VUE_APP_API_STEERING_SHOWS + data.showId + '/schedules/' axios.post(uri, data.schedule, { withCredentials: true, headers: { 'Authorization': 'Bearer ' + ctx.rootState.auth.user.access_token } }).then(response => { ctx.commit('finishLoading', 'schedules') if (data && typeof(data.callback) === 'function') { data.callback(response) } }).catch(error => { handleApiError(this, error, 'could not load timeslots') if (data && typeof(data.callbackCancel) === 'function') { data.callbackCancel() } }) } } export default { namespaced: true, state, getters, actions, mutations, }