-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (79 loc) · 2.85 KB
/
script.js
File metadata and controls
92 lines (79 loc) · 2.85 KB
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
console.log('This is ITSOURCECODE Project');
class Library {
constructor(name, author, publication, price) {
this.name = name;
this.author = author;
this.publication = publication;
this.price = price;
}
}
class Display {
add(bookoflibrary) {
console.log("Adding to UI");
let tableBody = document.getElementById('tableBody');
let uiString = `<tr>
<td>${bookoflibrary.name}</td>
<td>${bookoflibrary.author}</td>
<td>${bookoflibrary.publication}</td>
<td>${bookoflibrary.price}</td>
</tr>`;
tableBody.innerHTML += uiString;
}
clear() {
let libraryForm = document.getElementById('libraryForm');
libraryForm.reset();
}
validate(bookoflibrary) {
if (bookoflibrary.name.length < 2 || bookoflibrary.author.length < 2) {
return false
}
else {
return true;
}
if (bookoflibrary.publication.length < 2 || bookoflibrary.price.length < 2) {
return false
}
else {
return true;
}
}
show(type, displayMessage) {
let message = document.getElementById('message');
let boldText;
if(type==='success'){
boldText = 'Success';
}
else{
boldText = 'Error!';
}
message.innerHTML = `<div class="alert alert-${type} alert-dismissible fade show" role="alert">
<strong>${boldText}:</strong> ${displayMessage}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
setTimeout(function () {
message.innerHTML = ''
}, 5000);
}
}
function libraryFormSubmit(event) {
event.preventDefault();
console.log('YOu have submitted library form');
let name = document.getElementById('bookName').value;
let author = document.getElementById('author').value;
let publication = document.getElementById('publication').value;
let price = document.getElementById('price').value;
let bookoflibrary = new Library(name, author, publication,price);
console.log(bookoflibrary);
let display = new Display();
if (display.validate(bookoflibrary)) {
display.add(bookoflibrary);
display.clear();
display.show('success', 'Your book has been successfully added')
}
else {
// Show error to the user
display.show('danger', 'Sorry you cannot add this book');
}
}