-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
146 lines (123 loc) · 4.23 KB
/
index.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
137
138
139
140
141
142
143
144
145
146
// eslint-disable-next-line max-classes-per-file
document.addEventListener('DOMContentLoaded', () => {
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
}
class BookList {
constructor() {
this.booksInfo = JSON.parse(localStorage.getItem('books') || '[]');
}
renderBooks = (renderFn) => {
this.booksInfo.forEach((book, index) => {
renderFn(book, () => {
this.removeBook(index);
});
});
};
removeBook = (index) => {
this.booksInfo.splice(index, 1);
this.updateStorage();
};
addBook = (book) => {
this.booksInfo.push(book);
this.updateStorage();
};
updateStorage = () => {
localStorage.setItem('books', JSON.stringify(this.booksInfo));
};
}
class BooksListUI {
constructor(bookList) {
this.bookList = bookList;
this.bookTemplate = document.getElementById('bookInfoTemplate');
this.booksContainer = document.querySelector('.booksContainer');
}
render = () => {
this.booksContainer.innerHTML = '';
this.bookList.renderBooks((book, removeBookFn) => {
const bookInstance = this.bookTemplate.content.cloneNode(true);
bookInstance.querySelector('h4').textContent = book.title;
bookInstance.querySelector('p').textContent = book.author;
bookInstance.querySelector('.removeButton').addEventListener('click', () => {
removeBookFn();
this.render();
});
this.booksContainer.appendChild(bookInstance);
});
};
}
const bookList = new BookList();
const booksListUI = new BooksListUI(bookList);
const isValidInput = (input) => {
if (input.trim() === '') return false;
const regex = /^[a-zA-Z0-9\s(),.\\-]+$/;
return regex.test(input);
};
document.getElementById('addBookButton').addEventListener('click', (event) => {
event.preventDefault();
const bookTitleInput = document.getElementById('bookTitle');
const authorNameInput = document.getElementById('bookAuthor');
const bookTitle = bookTitleInput.value;
const authorName = authorNameInput.value;
const errorMessage = document.getElementById('error-message');
const successMessage = document.getElementById('success-message');
errorMessage.textContent = '';
successMessage.textContent = '';
if (!isValidInput(bookTitle) || !isValidInput(authorName)) {
errorMessage.textContent = 'Please enter a valid title and author name (letters and numbers only).';
setTimeout(() => {
errorMessage.textContent = '';
}, 2000);
return;
}
const newBook = new Book(bookTitle, authorName);
bookList.addBook(newBook);
booksListUI.render();
bookTitleInput.value = '';
authorNameInput.value = '';
successMessage.textContent = 'Book successfully created.';
setTimeout(() => {
successMessage.textContent = '';
}, 2000);
});
booksListUI.render();
const navBooks = document.getElementById('navBooks');
const navAddBook = document.getElementById('navAddBook');
const navContact = document.getElementById('navContact');
const booksSection = document.getElementById('booksSection');
const addBookSection = document.getElementById('addBookSection');
const contactSection = document.getElementById('contactSection');
const showSection = (section) => {
booksSection.classList.add('hidden');
addBookSection.classList.add('hidden');
contactSection.classList.add('hidden');
section.classList.remove('hidden');
};
navBooks.addEventListener('click', () => {
showSection(booksSection);
});
navAddBook.addEventListener('click', () => {
showSection(addBookSection);
});
navContact.addEventListener('click', () => {
showSection(contactSection);
});
const displayLiveDate = () => {
const liveDateElement = document.querySelector('.live-date');
const now = new Date();
const options = {
month: 'long',
day: 'numeric',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
};
const formattedDate = new Intl.DateTimeFormat('en-US', options).format(now);
liveDateElement.textContent = formattedDate;
};
displayLiveDate();
setInterval(displayLiveDate, 30000);
});