-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
185 lines (160 loc) · 6.37 KB
/
popup.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Filters date format from year-month-date to month-date-year.
*
* @param {String} dateStr The date string to format.
* @returns {String} Returns the new formatted date string.
*/
const filterDate = (dateStr) => {
let date = new Date(dateStr);
return date.toLocaleDateString();
};
/**
* Creates a book tile for each book item passed.
*
* @param {String} id The id of the book, a unique string of numbers and characters generated by Google Books API.
* @param {String} title The title of the book.
* @param {String} author The author of the book.
* @param {String} publishedDate The date-string of the book's pubish date.
* @param {String} coverImage The link for the thumbnail image of the cover.
* @returns {String Template} template Returns the html template to create the book tile.
*/
const buildBookItemTemplate = (id, title, author, publishedDate, coverImage) => {
const template =
'<div class="book-card">'
+ ` <img src=${coverImage} alt="${title}" title="${title}" style="width:90%">`
+ ` <h4 class="publish-date">Coming: ${filterDate(publishedDate)}</h4>`
+ ` <p class="author">By: ${author}</p>`
+ ' <form>'
+ ' Remind Me On:'
+ ` <input type="date" id=${id} name="alarmDate" value=${publishedDate}>`
+ ' </form>'
+ ` <p><button type="button"
class="add-to-list"
data-id=${id}
data-title="${title}"
data-author="${author}"
data-published=${publishedDate}
data-cover=${coverImage}>Add To Watch List
</button></p>`
+ '</div>'
;
return template;
};
/**
* Event hander for the search function. Returns results from Google Books API and displays results as individual book tiles.
*/
const handleSearch = () => {
event.preventDefault();
$('#results').empty();
$('#myBooks').empty();
const BASE_URL = 'https://www.googleapis.com/books/v1/volumes?q=';
const OPTIONS = '&orderBy=newest'
let query = $('#query').val();
$('#query').val(' ');
if(query) {
fetch(`${BASE_URL}${query}${OPTIONS}`, { method: 'GET'} )
.then(response => response.json())
.then(json => {
let { items } = json;
items.map((item) => {
const id = item.id,
title = item.volumeInfo.title,
author = item.volumeInfo.authors[0],
publishedDate = item.volumeInfo.publishedDate,
imageLinks = item.volumeInfo.imageLinks,
coverImage = imageLinks !== undefined ? imageLinks.thumbnail : 'notebook.png';
let bookItem = buildBookItemTemplate(id, title, author, publishedDate, coverImage);
$('#results').append(bookItem);
});
});
}
return false;
};
/**
* Creates new Chrome alarm to notify user of book release.
*
* @param {String} id The id of the book, a unique string of numbers and characters generated by Google Books API.
* @param {String} title The title of the book.
* @param {String} alarmDate Date for the alarm chose by the user. Defaults to published date.
*/
const createAlarm = (id, title, alarmDate) => {
alert(`A new notification was created for ${title} on ${filterDate(alarmDate)}.`);
chrome.alarms.create(id, {when: Date.parse(alarmDate)});
};
/**
* Saves book info to Chrome Storage for the user's watch list.
*
* @param {String} id The id of the book, a unique string of numbers and characters generated by Google Books API.
* @param {String} title The title of the book.
* @param {String} author The author of the book.
* @param {String} publishedDate The date-string of the book's pubish date.
* @param {String} coverImage The link for the thumbnail image of the cover.
*/
const saveToWatchList = (id, title, author, publishedDate, coverImage) => {
chrome.storage.sync.set({[id]: { title, author, publishedDate, coverImage }});
};
/**
* Event handler to display user's watch list.
*
* @param {String} id The id of the book, a unique string of numbers and characters generated by Google Books API.
* @param {String} title The title of the book.
* @param {String} author The author of the book.
* @param {String} publishedDate The date-string of the book's pubish date.
* @param {String} coverImage The link for the thumbnail image of the cover.
* @returns {String Template} template Returns the html template to create the book tile.
*/
const displayWatchList = () => {
chrome.storage.sync.get(null, function (result) {
$('#results').empty();
$('#myBooks').empty();
const booksArray = Object.entries(result);
booksArray.map((subArray) => {
const id = subArray[0],
title = subArray[1].title,
publishedDate = subArray[1].publishedDate,
coverImage = subArray[1].coverImage;
let bookItem =
' <div class="book-card">'
+ ` <img src=${coverImage} alt="${title}" title="${title}" style="width:90%">`
+ ` <h4 class="publish-date">Coming: ${filterDate(publishedDate)}</h4>`
+ ` <p><button type="button"
class="remove-from-list"
data-id=${id}>Remove
</button></p>`
+ '</div>'
;
$('#myBooks').append(bookItem);
});
});
};
/**
* Event handler to delete a book from a user's watch list. Book is removed from the user's Chrome Storage and the associated alarm is also deleted.
*
* @param {String} id The id of the book, a unique string of numbers and characters generated by Google Books API.
*/
const removeFromWatchList = (id) => {
chrome.storage.sync.remove(id, displayWatchList);
chrome.alarms.clear(id);
};
$(document).ready(function () {
$('#watchList').click(displayWatchList);
$('#searchForm').submit(handleSearch);
$('#results').on('click', 'button', function (e) {
e.preventDefault();
const id = $(this).data('id'),
title = $(this).data('title'),
author = $(this).data('author'),
publishedDate = $(this).data('published'),
coverImage = $(this).data('cover');
//get date from user using book id
const alarmDate = $('#' + id).val();
createAlarm(id, title, alarmDate);
saveToWatchList(id, title, author, publishedDate, coverImage);
});
$('#myBooks').on('click', 'button', function (e) {
e.preventDefault();
const id = $(this).data('id');
removeFromWatchList(id.toString());
});
return false;
});