-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (126 loc) · 4.85 KB
/
script.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
let headers = new Headers({
"Accept" : "application/json",
"Content-Type" : "application/json",
"User-Agent" : "DiscogsBlock/1.0",
"Authorization" : api_key
});
let discogsBlockParent = document.createElement('div');
discogsBlockParent.setAttribute('id', `#discogs-block-parent`);
discogsBlockParent.classList.add('discogs-block-parent');
document.body.appendChild(discogsBlockParent);
let discogsBlockContainer = document.createElement('div');
discogsBlockContainer.setAttribute('id', `#discogs-container`);
discogsBlockContainer.classList.add('discogs-container');
discogsBlockParent.appendChild(discogsBlockContainer);
let loader = document.createElement('div');
loader.setAttribute('id', `#loader`);
loader.classList.add('loader');
discogsBlockParent.appendChild(loader);
const getReleases = async (username, page, limit) => {
const response = await fetch(`https://api.discogs.com/users/${username}/collection/folders/0/releases?page=${page}&per_page=${limit}`, {
method: 'GET',
headers: headers
})
if (!response.ok) {
throw new Error(`An error occurred: ${response.status}`);
}
return await response.json();
}
const displayReleases = (discogsReleases) => {
discogsReleases.releases.forEach ( release => {
let artistName = release.basic_information.artists[0].name;
let albumName = release.basic_information.title;
let releaseYear = release.basic_information.year;
let format = release.basic_information.formats[0].name;
let albumCover = release.basic_information.thumb;
//let gridNumber = `gridNumber${i}`;
if (releaseYear == 0 | releaseYear == null) {
releaseYear = 'Unknown';
}
if (albumCover == '') {
albumCover = 'noimage.png';
}
// Parent container
gridNumber = document.createElement('div');
//gridNumber.setAttribute('id', `discogs-release${i}`);
gridNumber.classList.add('discogs-card');
// album title container
albumTitleDiv = document.createElement('div');
//albumTitleDiv.classList.add('album-title-div');
gridNumber.appendChild(albumTitleDiv)
// album cover container
albumCoverDiv = document.createElement('div');
//albumCoverDiv.classList.add('album-cover-div');
gridNumber.appendChild(albumCoverDiv)
// Album title H4
albumNameCard = document.createElement('h4');
albumNameCard.appendChild(document.createTextNode(albumName));
albumTitleDiv.appendChild(albumNameCard)
//Album cover image
albumCoverCard = document.createElement('img');
albumCoverCard.setAttribute('src', albumCover);
albumCoverDiv.appendChild(albumCoverCard)
//Album title H5
artistNameCard = document.createElement('h5');
artistNameCard.appendChild(document.createTextNode(artistName));
gridNumber.appendChild(artistNameCard)
//format P tag
formatCard = document.createElement('p');
formatCard.appendChild(document.createTextNode('Format: ' + format));
gridNumber.appendChild(formatCard)
//format P tag
releaseYearCard = document.createElement('p');
releaseYearCard.appendChild(document.createTextNode('Release Year: ' + releaseYear));
gridNumber.appendChild(releaseYearCard)
discogsBlockContainer.appendChild(gridNumber);
});
}
const showLoader = () => {
loader.classList.add('show');
loading = true;
};
const hideLoader = () => {
loader.classList.remove('show');
loading = false
};
let currentPage = 1;
let username = user;
const limit = 12;
let total = 0;
let loading = false;
const moreReleases = (page, limit, total) => {
const indexStart = (page - 1) * limit + 1;
return total === 0 || indexStart < total;
};
const loadReleases = async (page, limit) => {
//console.log(page, limit, total)
showLoader();
setTimeout(async () => {
try {
if (moreReleases(page, limit, total)){
const discogsResponse = await getReleases(username,currentPage,limit);
displayReleases(discogsResponse);
total = discogsResponse.pagination.items
}
} catch (error) {
console.log(error.message);
} finally {
hideLoader();
}
}, 500);
};
window.addEventListener('scroll', () => {
const {
scrollTop,
scrollHeight,
clientHeight
} = document.documentElement;
if (scrollTop + clientHeight >= scrollHeight - 5 &&
moreReleases(currentPage, limit, total) && !loading) {
currentPage++;
loadReleases(currentPage, limit);
}
}, {
passive: true
});
loadReleases (currentPage, limit);