Added vuex as a store and an event bus.

This commit is contained in:
krzysiej
2022-06-13 15:17:02 +02:00
parent 5c154b740c
commit 8dbd63478d
8 changed files with 118 additions and 21 deletions

View File

@@ -0,0 +1,65 @@
import BookApi from "../../api/book";
const
FETCHING_BOOKS = "FETCHING_BOOKS",
FETCHING_BOOKS_SUCCESS = "FETCHING_BOOKS_SUCCESS",
FETCHING_BOOKS_ERROR = "FETCHING_BOOKS_ERROR";
export default {
namespaced: true,
state: {
isLoading: false,
error: null,
books: [],
searchTitle: null
},
getters: {
isLoading(state) {
return state.isLoading;
},
hasError(state) {
return state.error !== null;
},
error(state) {
return state.error;
},
hasBooks(state) {
return state.books.length > 0;
},
books(state) {
return state.books;
}
},
mutations: {
[FETCHING_BOOKS](state) {
state.isLoading = true;
state.error = null;
state.books = [];
},
[FETCHING_BOOKS_SUCCESS](state, books) {
state.isLoading = false;
state.error = null;
state.books = books;
},
[FETCHING_BOOKS_ERROR](state, error) {
state.isLoading = false;
state.error = error;
state.books = [];
}
},
actions: {
async findAll({commit, state}, searchTitle) {
commit(FETCHING_BOOKS);
try {
state.searchTitle = searchTitle;
console.info(state.searchTitle);
let response = await BookApi.booksFetch(state.searchTitle);
commit(FETCHING_BOOKS_SUCCESS, response.data);
return response.data;
} catch (error) {
commit(FETCHING_BOOKS_ERROR, error);
return null;
}
}
}
};