From 2e35d86e1a98a9dfdfbbf9d140e85cfeba093261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Mon, 21 Dec 2020 22:06:36 +0100 Subject: [PATCH] Load Locations/People/Vehicles from layout on app load --- layouts/default.vue | 11 ++++++++++- store/locations/index.js | 18 ++++++++++++++++-- store/vehicles/index.js | 18 ++++++++++++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/layouts/default.vue b/layouts/default.vue index d72ab30..038e97d 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -10,7 +10,16 @@ diff --git a/store/locations/index.js b/store/locations/index.js index f2bae6f..c94b2e6 100644 --- a/store/locations/index.js +++ b/store/locations/index.js @@ -1,18 +1,31 @@ export const state = () => ({ + list: [], location: {} }); export const mutations = { + setList: (state, locations) => { + state.list = locations; + }, setLocation: (state, location) => { state.location = location; } }; export const actions = { - async getLocation({ commit }, { id, callback = false }) { + async getList({ commit }) { + try { + const locations = await this.$axios.$get( + "/api/locations?fields=id,name,climate,terrain,surface_water,residents,films" + ); + commit("setList", locations); + } catch (e) { + throw Error(`API Error occurred: ${e.message}`); + } + }, + async getLocation({ commit }, id) { try { const location = await this.$axios.$get(`/api/locations/${id}`); - if (callback) return location; commit("setLocation", location); } catch (e) { throw Error(`API Error occurred: ${e.message}`); @@ -21,5 +34,6 @@ export const actions = { }; export const getters = { + list: state => state.list, location: state => state.location }; diff --git a/store/vehicles/index.js b/store/vehicles/index.js index 68325e8..8a126c1 100644 --- a/store/vehicles/index.js +++ b/store/vehicles/index.js @@ -1,18 +1,31 @@ export const state = () => ({ + list: [], vehicle: {} }); export const mutations = { + setList: (state, vehicles) => { + state.list = vehicles; + }, setVehicle: (state, vehicle) => { state.vehicle = vehicle; } }; export const actions = { - async getVehicle({ commit }, { id, callback = false }) { + async getList({ commit }) { + try { + const vehicles = await this.$axios.$get( + "/api/vehicles?fields=id,name,description,vehicle_class,length,pilot,films" + ); + commit("setList", vehicles); + } catch (e) { + throw Error(`API Error occurred: ${e.message}`); + } + }, + async getVehicle({ commit }, id) { try { const vehicle = await this.$axios.$get(`/api/vehicles/${id}`); - if (callback) return vehicle; commit("setVehicle", vehicle); } catch (e) { throw Error(`API Error occurred: ${e.message}`); @@ -21,5 +34,6 @@ export const actions = { }; export const getters = { + list: state => state.list, vehicle: state => state.vehicle };