Added vuex as a store and an event bus.
This commit is contained in:
65
assets/js/store/modules/books.js
Normal file
65
assets/js/store/modules/books.js
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user