generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
160 lines (139 loc) · 4.37 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { DateTime } = luxon;// eslint-disable-line
const form = document.querySelector('form');
const booksList = document.querySelector('.books');
const title = document.querySelector('.title');
const author = document.querySelector('.author');
const noBooksText = document.querySelector('.no-books-txt');
class Book {
constructor(title, author, id = 0) {
this.title = title;
this.author = author;
this.id = id;
}
static books = [];
static getDataFromLocalStorage() {
if (!window.localStorage.getItem('booksData')) return null;
const data = JSON.parse(window.localStorage.getItem('booksData'));
data.myBooks = data.myBooks.map((b) => {
const book = new Book(b.title, b.author, b.id);
return book;
});
Book.books = data.myBooks;
return data;
}
static setDataInLocalStorage(data) {
window.localStorage.setItem('booksData', JSON.stringify(data));
}
static renderUI() {
const books = Book.getDataFromLocalStorage();
if(Book.books.length > 0){
noBooksText.style.display = 'none';
booksList.style.display = 'block';
} else {
noBooksText.style.display = 'block';
booksList.style.display = 'none';
};
booksList.innerHTML = '';
Book.books.forEach((book) => {
const bookItem = document.createElement('li');
bookItem.className = 'px-3 py-1';
bookItem.innerHTML = `
<p>"${book.title}" by ${book.author}</p>
<button class="remove-btn ms-auto" id="${book.id}">Remove</button>`;
booksList.appendChild(bookItem);
});
const removeBtns = document.querySelectorAll('.remove-btn');
removeBtns.forEach((btn) => {
btn.addEventListener('click', (e) => {
const bookId = e.target.id;
books.myBooks = books.myBooks.filter(
(book) => book.id !== parseInt(bookId, 10),
);
Book.setDataInLocalStorage(books);
Book.renderUI();
});
});
}
addBook() {
const books = Book.getDataFromLocalStorage();
this.id = books.bookCount + 1;
books.myBooks.push(this);
books.bookCount += 1;
Book.setDataInLocalStorage(books);
Book.renderUI();
title.value = '';
author.value = '';
alert(`"${this.title}" has been added successfully to your booklist.`);
}
removeBook(id = this.id) {
const books = Book.getDataFromLocalStorage();
books.myBooks = books.myBooks.filter((book) => book.id !== id);
Book.setDataInLocalStorage(books);
Book.renderUI();
}
}
form.addEventListener('submit', (e) => {
e.preventDefault();
const newBook = new Book(title.value, author.value);
newBook.addBook();
});
const booksData = Book.getDataFromLocalStorage();
if (!booksData) {
const dataToBeStored = {
myBooks: [],
bookCount: 0,
};
Book.setDataInLocalStorage(dataToBeStored);
} else {
Book.books = booksData;
Book.renderUI();
}
function getNumberSuffix(num) {
const th = 'th';
const rd = 'rd';
const nd = 'nd';
const st = 'st';
if (num === 11 || num === 12 || num === 13) return th;
const lastDigit = num.toString().slice(-1);
switch (lastDigit) {
case '1':
return st;
case '2':
return nd;
case '3':
return rd;
default:
return th;
}
}
window.onload = () => {
const date = DateTime.now();
const dateString = `${date.monthLong} ${date.day}${getNumberSuffix(
date.day,
)} ${date.year}, ${date.toFormat('tt')}`;
document.querySelector('.time').textContent = dateString;
setInterval(() => {
const date = DateTime.now();
const dateString = `${date.monthLong} ${date.day}${getNumberSuffix(
date.day,
)} ${date.year}, ${date.toFormat('tt')}`;
document.querySelector('.time').textContent = dateString;
}, 1000);
const navLinks = document.querySelectorAll('.nav-items li a');
navLinks.forEach((link) => {
link.addEventListener('click', (event) => {
document.querySelector('.active').classList.remove('active');
event.target.classList.add('active');
document.querySelectorAll('section').forEach((sec) => {
sec.classList.add('d-none');
});
if (event.target.id === 'list') {
document.querySelector('.bookList').classList.remove('d-none');
} else if (event.target.id === 'form') {
document.querySelector('.formInput').classList.remove('d-none');
} else {
document.querySelector('.contactForm').classList.remove('d-none');
}
});
});
};