-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (62 loc) · 2.01 KB
/
main.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
// FETCH DATA
const proxy = 'https://cors-anywhere.herokuapp.com/';
const wantRead = `${proxy}https://www.skoob.com.br/v1/bookcase/books/4355417/shelf_id:0/page:1/limit:36/`;
const alreadyRead = `${proxy}https://www.skoob.com.br/v1/bookcase/books/4355417/shelf_id:1/page:1/limit:36/`;
const reading = `${proxy}https://www.skoob.com.br/v1/bookcase/books/4355417/shelf_id:2/page:1/limit:36/`;
const errorHandler = (data, status, error) => console.log(`error: ${error}`);
const fetchWantRead = () => {
return $.ajax({
type: 'GET',
url: wantRead,
dataType: "JSON",
});
}
const fetchAlreadyRead = () => {
return $.ajax({
type: 'GET',
url: alreadyRead,
dataType: "JSON",
});
}
const fetchReading = () => {
return $.ajax({
type: 'GET',
url: reading,
dataType: "JSON",
});
}
$.when(fetchWantRead(), fetchAlreadyRead(), fetchReading())
.done((wantRead, alreadyRead, reading) => {
const books = {};
books.wantRead = wantRead[0].response;
books.alreadyRead = alreadyRead[0].response;
books.reading = reading[0].response;
console.log('fetched');
renderBookshelf(books);
}
);
// RENDER DATA
const renderBookshelf = (books) => {
const $bookShelf = $('#js-books');
renderShelf(books.reading, $bookShelf);
renderShelf(books.alreadyRead, $bookShelf);
renderShelf(books.alreadyRead, $bookShelf);
}
const renderShelf = (books = [], $bookShelf) => {
books.forEach((book) => {
const cover = book.edicao.capa_media;
const title = book.edicao.titulo;
const subtitle = book.edicao.subtitulo;
const author = book.edicao.autor;
const publishingCompany = book.edicao.editora;
const link = book.edicao.url;
const bookTemplate = `
<article>
<h2>${title}</h2>
<p>${author}</p>
<img src="${cover}">
</article>
`;
$bookShelf.append(bookTemplate);
});
}