Skip to content

Commit 1493915

Browse files
committed
first books request using axios and vuex
1 parent 319576b commit 1493915

File tree

8 files changed

+191
-13
lines changed

8 files changed

+191
-13
lines changed

package-lock.json

Lines changed: 86 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"lint": "vue-cli-service lint"
99
},
1010
"dependencies": {
11+
"axios": "^0.27.2",
1112
"core-js": "^3.8.3",
1213
"primeflex": "^3.2.1",
1314
"primeicons": "^5.0.0",

src/components/header.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Toolbar class="flex align-items-center">
33
<template #start>
44
<img alt="Logo" src="../assets/logo.png" width="40" height="40" />
5-
<div class="text-primary text-3xl font-bold ml-5">
5+
<div class="text-primary text-2xl font-bold ml-5">
66
SPRING - VUEJS CRUD
77
</div>
88
</template>

src/main.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import 'primevue/resources/primevue.min.css' //core css
55
import 'primeicons/primeicons.css' //icons
66
import 'primeflex/primeflex.css'; // primeflex
77

8+
import axios from 'axios';
9+
// becase of CORS error we need to have the same origin
10+
// requests are sent to backend server (see vue.config.js)
11+
axios.defaults.baseURL = 'http://localhost:3000';
12+
813
import { createApp } from 'vue'
914
import App from './App.vue'
1015
import router from './router'
@@ -13,6 +18,7 @@ import store from './store'
1318
import PrimeVue from 'primevue/config';
1419
import Toolbar from 'primevue/toolbar';
1520
import Button from 'primevue/button';
21+
import Card from 'primevue/card';
1622

1723
const app = createApp(App);
1824

@@ -25,6 +31,7 @@ app.use(PrimeVue, { ripple: true })
2531

2632
app.component('Toolbar', Toolbar);
2733
app.component('Button', Button);
34+
app.component('Card', Card);
2835

2936

3037
app.mount('#app');

src/services/book-service.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import axios from 'axios';
2+
3+
class BookService {
4+
getBooks() { return axios.get('/api/books?size=200&projection=bookDetail&sort=title,asc') }
5+
}
6+
7+
export default new BookService();

src/store/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,51 @@
11
import { createStore } from 'vuex'
2+
import BookService from '../services/book-service';
23

34
export default createStore({
5+
6+
// data
7+
48
state: {
9+
books: []
510
},
11+
12+
// computed properties for stores
13+
614
getters: {
715
},
16+
17+
// methods that can change state data, only sychronous code
18+
// mutations expect two arguments: state and payload
19+
// commit a mutation
20+
821
mutations: {
22+
getBooks(state, books) {
23+
state.books = books;
24+
}
925
},
26+
27+
// methods that cannot change state data, asychronous code (e.g. api calls)
28+
// receive a context object which contains mutations, state, getters, actions
29+
// dispach an action
30+
1031
actions: {
32+
getBooks({ commit }) {
33+
console.log('action -> getBooks')
34+
35+
return new Promise((resolve, reject) => {
36+
37+
BookService.getBooks()
38+
.then(response => {
39+
console.log('success')
40+
commit('getBooks', response.data)
41+
resolve();
42+
})
43+
.catch(error => {
44+
console.log('error')
45+
reject(error);
46+
})
47+
})
48+
}
1149
},
1250
modules: {
1351
}

src/views/BookListView.vue

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
11
<template>
2-
<div class="home">
3-
<h1>This is the books page</h1>
2+
<div class="text-color-secondary text-3xl font-semibold m-5">
3+
Books
4+
<Button
5+
icon="pi pi-plus"
6+
class="p-button-rounded p-button-secondary p-button-outlined ml-5"
7+
/>
48
</div>
5-
</template>
9+
10+
<Card>
11+
<template #title>My wonderful book</template>
12+
<template #subtitle>Giannis Kafousis, Vaso Kountouri</template>
13+
<template #footer>
14+
<Button
15+
icon="pi pi-pencil"
16+
label="Edit"
17+
class="p-button-primary"
18+
style="width: 180px"
19+
/>
20+
<Button
21+
icon="pi pi-trash"
22+
label="Delete"
23+
class="p-button-secondary ml-6"
24+
style="width: 180px"
25+
/>
26+
</template>
27+
</Card>
28+
</template>
29+
30+
<script>
31+
export default {
32+
mounted() {
33+
this.$store.dispatch("getBooks").then(
34+
(response) => {
35+
console.log("Got some data");
36+
console.log(response);
37+
},
38+
(error) => {
39+
console.error(
40+
"Got nothing from server. Prompt user to check internet connection and try again"
41+
);
42+
console.log(error);
43+
}
44+
);
45+
},
46+
};
47+
</script>

vue.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
const { defineConfig } = require('@vue/cli-service')
2+
23
module.exports = defineConfig({
3-
transpileDependencies: true
4+
transpileDependencies: true,
5+
devServer: {
6+
// proxy any unknown requests to tomcat
7+
proxy: 'http://localhost:8080',
8+
}
49
})

0 commit comments

Comments
 (0)