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}`); 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) ); } };