-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
84 lines (78 loc) · 2.6 KB
/
helpers.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
/* eslint-disable no-undef */
/* eslint-disable class-methods-use-this */
import Utils from './utils.js';
const utilsObj = new Utils();
const noBook = document.querySelector('#no-data');
/**
* @object helperObj to hold all helper methods
*/
export default class Helper {
/**
* @function add - helper method for addition of books to localStorage
* @param {string} title - The title of the book to be added
* @param {string} author - The author of the book to be added
*/
add(title, author) {
const books = JSON.parse(localStorage.getItem('books')) || [];
let id;
if (books.length === 0) {
id = 0;
} else {
id = books.length;
}
books.push({ id, title, author });
localStorage.setItem('books', JSON.stringify(books));
utilsObj.render(title, author, id);
utilsObj.clearInput();
document.querySelector('#title').focus();
noBook.textContent = '';
}
/**
* @function
* @param {Object} e - The event object
* @param {string} id - The string id of the button dataset
*/
remove(e, id) {
id = parseInt(id, 10);
const stack = JSON.parse(localStorage.getItem('books'));
const remStack = stack.filter((item) => item.id !== id);
localStorage.setItem('books', JSON.stringify(remStack));
e.currentTarget.parentElement.remove();
if (remStack.length === 0) noBook.textContent = 'No book added yet';
}
/**
* @function display - helper function to display books on load
*/
display() {
const stack = JSON.parse(localStorage.getItem('books')) || [];
if (stack.length === 0) {
noBook.textContent = 'No book added yet';
} else {
stack.forEach((item) => {
const { id, title, author } = item;
utilsObj.render(title, author, id);
document.querySelector('#title').focus();
noBook.textContent = '';
});
}
document.querySelectorAll('.remove').forEach((elem) => {
elem.addEventListener('click', (e) => {
this.remove(e, e.currentTarget.dataset.id);
});
});
}
/**
* @fucntion navHandler - handles the single page navigation
* @param {event Object} e - the event object
*/
navHandler(e) {
const { content } = e.currentTarget.dataset;
document.querySelector('.books.active').classList.remove('active');
document.querySelector(`${content}`).classList.add('active');
document.querySelector('.links.active').classList.remove('active');
e.currentTarget.classList.add('active');
}
dateHandler() {
document.querySelector('.date p').innerText = luxon.DateTime.now().toLocaleString(luxon.DateTime.DATETIME_FULL_WITH_SECONDS);
}
}