Files
ghibli-api/store/vehicles/index.js

55 lines
1.4 KiB
JavaScript

export const state = () => ({
list: [],
vehicle: {}
});
export const mutations = {
setList: (state, vehicles) => {
state.list = vehicles;
},
setVehicle: (state, vehicle) => {
state.vehicle = vehicle;
}
};
export const actions = {
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}?fields=id,name,description,vehicle_class,length,pilot,films`
);
commit("setVehicle", vehicle);
} catch (e) {
throw Error(`API Error occurred: ${e.message}`);
}
}
};
export const getters = {
list: state => state.list,
vehicle: state => state.vehicle,
getVehiclesByFilmId: state => id => {
return state.list.filter(vehicle => vehicle.films.split("/")[4] === id);
},
getPilot: (state, getters, rootState) => {
return rootState.people.list.find(
people => people.id === state.vehicle.pilot.split("/")[4]
);
},
getFilm: (state, getters, rootState) => {
return rootState.films.list.find(
film => film.id === state.vehicle.films.split("/")[4]
);
}
};