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

47 lines
1.0 KiB
JavaScript

export const state = () => ({
list: [],
person: {}
});
export const mutations = {
setList: (state, people) => {
state.list = people;
},
setPerson: (state, person) => {
state.person = person;
}
};
export const actions = {
async getList({ commit }) {
try {
const people = await this.$axios.$get(
"/api/people?fields=id,name,gender,age,eye_color,hair_color,films"
);
commit("setList", people);
} catch (e) {
throw Error(`API Error occurred: ${e.message}`);
}
},
async getPerson({ commit }, id) {
try {
const person = await this.$axios.$get(
`/api/people/${id}?fields=id,name,gender,age,eye_color,hair_color,films`
);
commit("setPerson", person);
} catch (e) {
throw Error(`API Error occurred: ${e.message}`);
}
}
};
export const getters = {
list: state => state.list,
person: state => state.person,
getPeopleByFilmId: state => id => {
return state.list.filter(person =>
person.films.find(film => film.split("/")[4] === id)
);
}
};