From 8dbd63478d089d3249002d89580bf507e76f9669 Mon Sep 17 00:00:00 2001 From: krzysiej Date: Mon, 13 Jun 2022 15:17:02 +0200 Subject: [PATCH] Added vuex as a store and an event bus. --- assets/js/api/book.js | 10 +++++ assets/js/book.js | 3 +- assets/js/pages/booklisting.vue | 19 ++++---- assets/js/pages/booklistingheader.vue | 14 ++---- assets/js/store/index.js | 20 +++++++++ assets/js/store/modules/books.js | 65 +++++++++++++++++++++++++++ package.json | 3 +- yarn.lock | 5 +++ 8 files changed, 118 insertions(+), 21 deletions(-) create mode 100644 assets/js/api/book.js create mode 100644 assets/js/store/index.js create mode 100644 assets/js/store/modules/books.js diff --git a/assets/js/api/book.js b/assets/js/api/book.js new file mode 100644 index 0000000..ac81b15 --- /dev/null +++ b/assets/js/api/book.js @@ -0,0 +1,10 @@ +import axios from 'axios'; + +export default { + booksFetch(searchTitle) { + return axios.get('/api/books', { + params: {title: searchTitle}, + headers: {'accept': 'application/json'} + }) + } +} \ No newline at end of file diff --git a/assets/js/book.js b/assets/js/book.js index ade0f2b..0be5489 100644 --- a/assets/js/book.js +++ b/assets/js/book.js @@ -2,9 +2,10 @@ import Vue from 'vue'; import Book from './pages/book' import BookListing from './pages/booklisting' import BookListingHeader from './pages/booklistingheader' +import store from "./store/index"; Vue.component('Book', Book); Vue.component('BookListing', BookListing); Vue.component('BookListingHeader', BookListingHeader); -new Vue().$mount('#app'); \ No newline at end of file +new Vue({store}).$mount('#app'); \ No newline at end of file diff --git a/assets/js/pages/booklisting.vue b/assets/js/pages/booklisting.vue index 4e4aeec..86fac22 100644 --- a/assets/js/pages/booklisting.vue +++ b/assets/js/pages/booklisting.vue @@ -1,6 +1,6 @@ \ No newline at end of file diff --git a/assets/js/store/index.js b/assets/js/store/index.js new file mode 100644 index 0000000..7cf58a1 --- /dev/null +++ b/assets/js/store/index.js @@ -0,0 +1,20 @@ +import Vue from 'vue' +import Vuex from 'vuex' +import booksmodule from './modules/books'; + +Vue.use(Vuex) + +export default new Vuex.Store({ + state: { + count: 0 + }, + mutations: { + increment(state) { + state.count++ + } + }, + modules:{ + booksmodule + } +}) + diff --git a/assets/js/store/modules/books.js b/assets/js/store/modules/books.js new file mode 100644 index 0000000..e17473b --- /dev/null +++ b/assets/js/store/modules/books.js @@ -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; + } + } + } +}; \ No newline at end of file diff --git a/package.json b/package.json index addfcba..40a8509 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "dependencies": { "@popperjs/core": "^2.11.5", "bootstrap": "^5.1.3", - "popper": "^1.0.1" + "popper": "^1.0.1", + "vuex": "^3" } } diff --git a/yarn.lock b/yarn.lock index 38a36a2..a42ed6b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7431,6 +7431,11 @@ vue@^2.5: resolved "https://registry.yarnpkg.com/vue/-/vue-2.6.14.tgz#e51aa5250250d569a3fbad3a8a5a687d6036e235" integrity sha512-x2284lgYvjOMj3Za7kqzRcUSxBboHqtgRE2zlos1qWaOye5yUmHn42LB1250NJBLRwEcdrB0JRwyPTEPhfQjiQ== +vuex@^3: + version "3.6.2" + resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.6.2.tgz#236bc086a870c3ae79946f107f16de59d5895e71" + integrity sha512-ETW44IqCgBpVomy520DT5jf8n0zoCac+sxWnn+hMe/CzaSejb/eVw2YToiXYX+Ex/AuHHia28vWTq4goAexFbw== + watchpack@^2.3.1: version "2.3.1" resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-2.3.1.tgz#4200d9447b401156eeca7767ee610f8809bc9d25"