-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvue_instance.js
136 lines (129 loc) · 3.62 KB
/
vue_instance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* vue_instance.js
* Copyright (C) 2021 damian <damian@damian-desktop>
*
* Distributed under terms of the MIT license.
*/
let config = {
apiBaseUrl: "https://xeyvfe7639.execute-api.eu-west-1.amazonaws.com/dev",
}
var removeByAttr = function(arr, attr, value){
var i = arr.length;
while(i--){
if( arr[i]
&& arr[i].hasOwnProperty(attr)
&& (arguments.length > 2 && arr[i][attr] === value ) ){
arr.splice(i,1);
}
}
return arr;
}
var vm = new Vue({
el: '#app',
data() {
return {
books : null,
newBook:{
book_id:0,
title:"",
description:"",
author_id:0,
},
authors : null,
newAuthor:{
author_id:0,
first_name:"",
last_name:"",
},
loading: true,
errored_get: false,
errored_delete: false,
errored_post: false
}
},
methods: {
addBook:function() {
console.log("Adding new book");
axios
.post(
config.apiBaseUrl + '/books/',
{
"title": this.newBook.title,
"description": this.newBook.description,
"author_id": parseInt(this.newBook.author_id),
},
{ headers: {"Content-Type": "application/json" } }
)
.then(response => {this.books.push(response.data)})
.catch(error => {
console.log(error)
this.errored_post = true
});
this.newBook.title = "";
this.newBook.description = "";
this.newBook.author_id = 0;
},
addAuthor:function() {
console.log("Adding new author");
axios
.post(
config.apiBaseUrl + '/authors/',
{
"first_name": this.newAuthor.first_name,
"last_name": this.newAuthor.last_name,
},
{ headers: {"Content-Type": "application/json" } }
)
.then(response => {this.authors.push(response.data)})
.catch(error => {
console.log(error)
this.errored_post = true
});
this.newAuthor.first_name = "";
this.newAuthor.last_name = "";
},
getAuthors:function() {
console.log("Getting authors");
axios
.get(
config.apiBaseUrl + '/authors/',
)
.then(response => {this.authors = response.data})
.catch(error => {
console.log(error)
this.errored_get = true
})
.finally(() => this.loading = false);
},
getBooks:function() {
console.log("Getting books");
axios
.get(
config.apiBaseUrl + '/books/',
)
.then(response => {this.books = response.data})
.catch(error => {
console.log(error)
this.errored_get = true
})
.finally(() => this.loading = false);
},
removeBook:function(book) {
console.log("Remove book with id: " + book.book_id);
axios
.delete(
config.apiBaseUrl + '/books/' + book.book_id,
)
.then(response => {removeByAttr(this.books, 'book_id', book.book_id)})
.catch(error => {
console.log(error)
this.errored_delete = true
})
.finally(() => this.loading = false);
}
},
mounted () {
this.getBooks();
this.getAuthors();
}
})