export const state = () => ({ list: [], film: {} }); export const mutations = { setList: (state, films) => { state.list = films; }, setFilm: (state, film) => { state.film = film; }, setPeople: (state, people) => { state.film.people = people; }, setVehicles: (state, vehicles) => { state.film.vehicles = vehicles; }, setLocations: (state, locations) => { state.film.locations = locations; } }; export const actions = { async getList({ commit }) { try { const films = await this.$axios.$get("/api/films"); commit("setList", films); } catch (e) { throw Error(`API Error occurred: ${e.message}`); } }, async getFilm({ commit }, id) { try { const film = await this.$axios.$get(`/api/films/${id}`); commit("setFilm", film); } catch (e) { throw Error(`API Error occurred: ${e.message}`); } }, async getPeople({ commit, dispatch, state }) { let people = {}; try { if (state.film.people[0].split("/")[4] !== "") { const promises = state.film.people.map(async person => { const id = person.split("/")[4]; return await dispatch( "people/getPerson", { id: id, callback: true }, { root: true } ); }); people = await Promise.all(promises); } commit("setPeople", people); } catch (e) { throw Error(`API Error occurred: ${e.message}`); } }, async getVehicles({ commit, dispatch, state }) { let vehicles = {}; try { if (state.film.vehicles[0].split("/")[4] !== "") { const promises = state.film.vehicles.map(async vehicle => { const id = vehicle.split("/")[4]; return await dispatch( "vehicles/getVehicle", { id: id, callback: true }, { root: true } ); }); vehicles = await Promise.all(promises); } commit("setVehicles", vehicles); } catch (e) { throw Error(`API Error occurred: ${e.message}`); } }, async getLocations({ commit, dispatch, state }) { let locations = {}; try { if (state.film.locations[0].split("/")[4] !== "") { const promises = state.film.locations.map(async location => { const id = location.split("/")[4]; return await dispatch( "locations/getLocation", { id: id, callback: true }, { root: true } ); }); locations = await Promise.all(promises); } commit("setLocations", locations); } catch (e) { throw Error(`API Error occurred: ${e.message}`); } } }; export const getters = { list: state => state.list, film: state => state.film };