From 67680804e2e20e62a0e9d325e1631c1b95e7a562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Mon, 21 Dec 2020 13:57:18 +0100 Subject: [PATCH] Collect Film's nested data on mounted (Locations/Vehicles/People) --- pages/films/_id.vue | 43 ++++++++++++++++++++-- store/films/index.js | 79 +++++++++++++++++++++++++++++++++++++++- store/locations/index.js | 4 +- store/people/index.js | 2 +- store/vehicles/index.js | 4 +- 5 files changed, 121 insertions(+), 11 deletions(-) diff --git a/pages/films/_id.vue b/pages/films/_id.vue index e38872f..e4c8a52 100644 --- a/pages/films/_id.vue +++ b/pages/films/_id.vue @@ -1,7 +1,36 @@ diff --git a/store/films/index.js b/store/films/index.js index 1b8c58a..a408aac 100644 --- a/store/films/index.js +++ b/store/films/index.js @@ -9,6 +9,15 @@ export const mutations = { }, 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; } }; @@ -18,7 +27,7 @@ export const actions = { const films = await this.$axios.$get("/api/films"); commit("setList", films); } catch (e) { - throw Error("API Error occurred."); + throw Error(`API Error occurred: ${e.message}`); } }, async getFilm({ commit }, id) { @@ -26,7 +35,73 @@ export const actions = { const film = await this.$axios.$get(`/api/films/${id}`); commit("setFilm", film); } catch (e) { - throw Error("API Error occurred."); + 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}`); } } }; diff --git a/store/locations/index.js b/store/locations/index.js index 63ab5ae..f2bae6f 100644 --- a/store/locations/index.js +++ b/store/locations/index.js @@ -3,7 +3,7 @@ export const state = () => ({ }); export const mutations = { - setlocation: (state, location) => { + setLocation: (state, location) => { state.location = location; } }; @@ -15,7 +15,7 @@ export const actions = { if (callback) return location; commit("setLocation", location); } catch (e) { - throw Error("API Error occurred."); + throw Error(`API Error occurred: ${e.message}`); } } }; diff --git a/store/people/index.js b/store/people/index.js index 75e8617..88a023c 100644 --- a/store/people/index.js +++ b/store/people/index.js @@ -15,7 +15,7 @@ export const actions = { if (callback) return person; commit("setPerson", person); } catch (e) { - throw Error("API Error occurred."); + throw Error(`API Error occurred: ${e.message}`); } } }; diff --git a/store/vehicles/index.js b/store/vehicles/index.js index e58f259..68325e8 100644 --- a/store/vehicles/index.js +++ b/store/vehicles/index.js @@ -9,13 +9,13 @@ export const mutations = { }; export const actions = { - async getvehicle({ commit }, { id, callback = false }) { + async getVehicle({ commit }, { id, callback = false }) { try { const vehicle = await this.$axios.$get(`/api/vehicles/${id}`); if (callback) return vehicle; commit("setVehicle", vehicle); } catch (e) { - throw Error("API Error occurred."); + throw Error(`API Error occurred: ${e.message}`); } } };