Create basic Person component + collect data by getter

This commit is contained in:
2020-12-21 22:05:53 +01:00
parent a9771243a1
commit 188625a413
4 changed files with 80 additions and 96 deletions

View File

@@ -1,18 +1,31 @@
export const state = () => ({
list: [],
person: {}
});
export const mutations = {
setList: (state, people) => {
state.list = people;
},
setPerson: (state, person) => {
state.person = person;
}
};
export const actions = {
async getPerson({ commit }, { id, callback = false }) {
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}`);
if (callback) return person;
commit("setPerson", person);
} catch (e) {
throw Error(`API Error occurred: ${e.message}`);
@@ -21,5 +34,11 @@ export const actions = {
};
export const getters = {
person: state => state.person
list: state => state.list,
person: state => state.person,
getPeopleByFilmId: state => id => {
return state.list.filter(person =>
person.films.find(film => film.split("/")[4] === id)
);
}
};