Load Locations/People/Vehicles from layout on app load
This commit is contained in:
@@ -10,7 +10,16 @@
|
||||
|
||||
<script>
|
||||
export default {
|
||||
middleware: ["redirect"]
|
||||
middleware: ["redirect"],
|
||||
fetchOnServer: false,
|
||||
async fetch() {
|
||||
const { store } = this.$nuxt.context;
|
||||
if (!store.state.people.list.length) await store.dispatch("people/getList");
|
||||
if (!store.state.vehicles.list.length)
|
||||
await store.dispatch("vehicles/getList");
|
||||
if (!store.state.locations.list.length)
|
||||
await store.dispatch("locations/getList");
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user