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

45 lines
1.0 KiB
JavaScript

export const state = () => ({
list: [],
location: {}
});
export const mutations = {
setList: (state, locations) => {
state.list = locations;
},
setLocation: (state, location) => {
state.location = location;
}
};
export const actions = {
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}`);
commit("setLocation", location);
} catch (e) {
throw Error(`API Error occurred: ${e.message}`);
}
}
};
export const getters = {
list: state => state.list,
location: state => state.location,
getLocationsByFilmId: state => id => {
return state.list.filter(location =>
location.films.find(film => film.split("/")[4] === id)
);
}
};