-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
75 lines (59 loc) · 1.57 KB
/
storage.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
const STORAGE_KEY = 'BOOK_APPS';
let books = [];
const checkStorage = () => {
if (typeof Storage == undefined) {
alert('Your Browser not support web storage');
return false;
}
return true;
};
const saveData = () => {
const parseData = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parseData);
document.dispatchEvent(new Event('ondatasaved'));
};
const loadDatafromStorage = () => {
const serializedData = localStorage.getItem(STORAGE_KEY);
const data = JSON.parse(serializedData);
if (data !== null) books = data;
document.dispatchEvent(new Event('ondataloaded'));
};
const updateDataToStorage = () => {
if (checkStorage()) saveData();
};
const composeBookObject = (bookTitle, bookAuthor, bookYear, isCompleted) => {
return {
id: +new Date(),
bookTitle,
bookAuthor,
bookYear,
isCompleted,
};
};
const findBook = (bookId) => {
for (book of books) {
if (book.id === bookId) return book;
}
return null;
};
const findBookIndex = (bookId) => {
let index = 0;
for (book of books) {
if (book.id === bookId) return index;
index++;
}
return -1;
};
const refreshDataFromBooks = () => {
const bookUncompleted = document.getElementById(UNCOMPLETED_BOOK_ID);
let bookCompleted = document.getElementById(COMPLETED_BOOK_ID);
for (book of books) {
const newBook = makeBook(book.bookTitle, book.bookAuthor, book.bookYear, book.isCompleted);
newBook[BOOK_ITEMID] = book.id;
if (book.isCompleted) {
bookCompleted.append(newBook);
} else {
bookUncompleted.append(newBook);
}
}
};