Added vuex as a store and an event bus.
This commit is contained in:
10
assets/js/api/book.js
Normal file
10
assets/js/api/book.js
Normal file
@@ -0,0 +1,10 @@
|
||||
import axios from 'axios';
|
||||
|
||||
export default {
|
||||
booksFetch(searchTitle) {
|
||||
return axios.get('/api/books', {
|
||||
params: {title: searchTitle},
|
||||
headers: {'accept': 'application/json'}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
new Vue({store}).$mount('#app');
|
||||
@@ -1,6 +1,6 @@
|
||||
<template>
|
||||
<div>
|
||||
<input type="text" v-model="searchTerm" @keydown.enter="search"/>
|
||||
<input type="text" class="form-control" v-model="searchTerm" @keydown.enter="search"/>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -39,27 +39,28 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import axios from 'axios';
|
||||
import {mapActions, mapState} from "vuex";
|
||||
|
||||
export default {
|
||||
name: 'BookListing',
|
||||
data() {
|
||||
return {
|
||||
searchTerm: null,
|
||||
books: [],
|
||||
selectedBook: null,
|
||||
manualMode: false
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
...mapState('booksmodule', ['books']),
|
||||
},
|
||||
methods: {
|
||||
...mapActions('booksmodule', [
|
||||
'findAll'
|
||||
]),
|
||||
search: function () {
|
||||
this.$store.commit('increment')
|
||||
this.updateHistory();
|
||||
axios.get('/api/books', {
|
||||
params: {title: this.searchTerm},
|
||||
headers: {'accept': 'application/json'}
|
||||
}).then(response => {
|
||||
this.books = response.data;
|
||||
}).finally(() => window.EventBus.$emit('updateBookListingHeader', {searchTerm: this.searchTerm}));
|
||||
this.findAll(this.searchTerm);
|
||||
},
|
||||
updateHistory: function () {
|
||||
if (history.pushState) {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
<template>
|
||||
<div>
|
||||
<h1 v-if="searchTerm" v-html="'Book search: '+searchTerm"></h1>
|
||||
<h1 v-if="searchTitle" v-html="'Book search: '+searchTitle"></h1>
|
||||
<h1 v-else>Book list</h1>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {EventBus} from "../event-bus";
|
||||
import {mapState} from "vuex";
|
||||
|
||||
export default {
|
||||
name: 'BookListingHeader',
|
||||
@@ -15,14 +15,8 @@ export default {
|
||||
searchTerm: null,
|
||||
}
|
||||
},
|
||||
created() {
|
||||
window.EventBus.$on('updateBookListingHeader', (data) => {
|
||||
this.searchTerm = data.searchTerm;
|
||||
});
|
||||
computed: {
|
||||
...mapState('booksmodule', ['searchTitle']),
|
||||
},
|
||||
mounted() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
this.searchTerm = urlParams.get('search') || "";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
20
assets/js/store/index.js
Normal file
20
assets/js/store/index.js
Normal file
@@ -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
|
||||
}
|
||||
})
|
||||
|
||||
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