-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
444 lines (376 loc) · 17.1 KB
/
main.js
File metadata and controls
444 lines (376 loc) · 17.1 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const container = document.getElementById('container');
let contentHtml = '';
const loader = document.getElementById('loader');
const results = document.getElementById('results');
let resultsHtml = '';
const API_KEY = 'd0bac062c296448e513e8a3147810f19';
const TS = '1';
const API_HASH = '5e6eef74abfa220a102df0024190f6e7';
let currentPage = 1;
const resultsPerPage = 20;
let totalPages = 1;
let searchType = document.getElementById('search-type').value;
let searchValue = document.getElementById('search-input').value.trim();
let sortValue = document.getElementById('search-sort').value;
// Store previous search parameters
let previousSearchValue = '';
let previousSortValue = '';
let previousSearchType = '';
const urlBase = 'https://gateway.marvel.com:443/v1/public/';
// Update sorting options based on search type
function updateSortOptions() {
const sortSelect = document.getElementById('search-sort');
const searchType = document.getElementById('search-type').value;
sortSelect.innerHTML = '';
if (searchType === 'comics') {
sortSelect.innerHTML = `
<option value="title-asc">A-Z</option>
<option value="title-desc">Z-A</option>
<option value="date-asc">Oldest</option>
<option value="date-desc">Newest</option>
`;
} else if (searchType === 'characters') {
sortSelect.innerHTML = `
<option value="name-asc">A-Z</option>
<option value="name-desc">Z-A</option>
`;
}
}
// Event listener to update sorting options when search type changes
document.getElementById('search-type').addEventListener('change', updateSortOptions);
// Function to fetch data (comics or characters) with pagination
function getData(searchValue, sortValue, searchType) {
const baseUrl = new URL(`${urlBase}${searchType}`);
const offset = (currentPage - 1) * resultsPerPage;
const params = new URLSearchParams();
contentHtml = '';
loader.classList.remove('d-none');
container.classList.add('loading');
// Add sorting first
if (sortValue) {
const sortOptions = {
'title-asc': 'title',
'title-desc': '-title',
'name-asc': 'name',
'name-desc': '-name',
'date-asc': 'focDate',
'date-desc': '-focDate'
};
params.set('orderBy', sortOptions[sortValue] || '');
}
// Add limit and offset
params.set('limit', resultsPerPage);
params.set('offset', offset);
// Add search filtering
if (searchValue) {
if (searchType === 'comics') {
params.set('titleStartsWith', searchValue);
} else if (searchType === 'characters') {
params.set('nameStartsWith', searchValue);
}
}
// Add authentication parameters
params.set('apikey', API_KEY);
params.set('ts', TS);
params.set('hash', API_HASH);
// Append search params to the base URL
baseUrl.search = params.toString();
// Fetch and display results
fetch(baseUrl)
.then(res => res.json())
.then(info => {
if (info.code !== 200) {
throw new Error(`Error ${info.code}: ${info.status}`);
}
const totalResults = info.data.total;
totalPages = Math.ceil(totalResults / resultsPerPage);
console.log(`Total results: ${totalResults}, Total pages: ${totalPages}, Actual page: ${currentPage}`);
resultsHtml = `
<h5 class="results-title">RESULTS</h5>
<p class="results-counter">${totalResults} TOTAL RESULTS</p>
`;
results.innerHTML = resultsHtml;
// Render results based on searchType
if (searchType === 'comics') {
setComics(info.data.results);
} else {
setCharacters(info.data.results);
}
updatePaginationButtons();
})
.catch(err => console.log(err))
.finally(() => {
loader.classList.add('d-none');
container.classList.remove('loading');
});
}
// Function to display comics
function setComics(comics) {
contentHtml = '';
comics.forEach(comic => {
const comicTitle = comic.title;
const comicAuthors = comic.creators.items;
const authorName = comicAuthors.length > 0 ? comicAuthors[0].name : 'Marvel';
const comicImg = `${comic.thumbnail.path}.${comic.thumbnail.extension}`;
const comicUrl = comic.urls.length > 0 ? comic.urls[0].url : '#';
const comicId = comic.id;
contentHtml += `
<div class="card col-md-4 mt-3 mb-3 comic-card" data-id="${comicId}">
<div class="img-container">
<img src="${comicImg}" class="card-img img-fluid" alt="${comicTitle}">
</div>
<div class="card-body">
<h5 class="card-title"><a href="${comicUrl}" target="_blank">${comicTitle}</a></h5>
<p class="card-text">Creator: ${authorName}</p>
</div>
</div>`;
});
container.innerHTML = contentHtml;
document.querySelectorAll('.comic-card').forEach(card => {
card.addEventListener('click', function () {
const comicId = this.getAttribute('data-id');
displayComicDetails(comicId);
});
});
}
// Function to display comic details
function displayComicDetails(comicId) {
document.getElementById('main').classList.add('d-none');
container.classList.add('loading');
fetch(`https://gateway.marvel.com:443/v1/public/comics/${comicId}?ts=${TS}&apikey=${API_KEY}&hash=${API_HASH}`)
.then(res => res.json())
.then(info => {
const comic = info.data.results[0];
console.log(comic);
console.log(comic.characters);
const comicTitle = comic.title;
const comicImg = `${comic.thumbnail.path}.${comic.thumbnail.extension}`;
const comicReleaseDate = comic.dates.find(date => date.type === 'onsaleDate').date;
const comicWriters = comic.creators.items
.filter(creator => creator.role.toLowerCase() === 'writer')
.map(writer => writer.name)
.join(', ');
const comicDescription = comic.description || 'No description available.';
const comicCharacters = comic.characters.items;
const comicDetailsHtml = `
<div class="col-12 d-flex flex-column gap-5 mt-3 mb-3" id="comic-details">
<div class="col-12 d-flex col-md-8 gap-3 mt-3 mb-3 comic">
<div class="img-character col-md-4 mt-3 mb-3">
<img src="${comicImg}" class="card-img img-fluid" alt="${comicTitle}">
</div>
<div class="col-md-8 mt-3 mb-3 info">
<h2>${comicTitle}</h2>
<p class="card-text mt-3 mb-3"><strong>Release Date:</strong> ${new Date(comicReleaseDate).toLocaleDateString()}</p>
<p class="card-text mt-3 mb-3"><strong>Writers:</strong> ${comicWriters || 'Unknown'}</p>
<p class="card-text mt-3 mb-3"><strong>Description:</strong> ${comicDescription}</p>
<button id="back-button" class="btn btn-dark mt-5">Back to Comics</button>
</div>
</div>
<div id="characters-section" class="characters-section col gap-3 mt-3 mb-3">
<h4>Characters:</h4>
<p class="results-counter">${comic.characters.available} RESULTS</p>
<div class="d-flex col gap-3 flex-wrap" id="characters-container"></div>
</div>
</div>
`;
results.innerHTML = comicDetailsHtml;
// If there are characters, fetch their details
if (comicCharacters.length > 0) {
displayComicCharacters(comicCharacters); // New function to fetch character details
} else {
document.getElementById('characters-container').innerHTML = '<p>No characters available.</p>';
}
// Back button to return to comics list
document.getElementById('back-button').addEventListener('click', function () {
document.getElementById('main').classList.remove('d-none');
document.getElementById('comic-details').classList.add('d-none');
getData(searchValue, sortValue, 'comics');
});
})
.catch(err => console.log(err))
.finally(() => {
container.classList.remove('loading');
});
}
// Function to display characters
function setCharacters(characters) {
contentHtml = '';
characters.forEach(character => {
const characterId = character.id;
const characterImg = `${character.thumbnail.path}.${character.thumbnail.extension}`;
const characterName = character.name;
const characterInfo = character.description || 'No description available';
const characterComics = character.comics.available;
contentHtml += `
<div class="card col-md-4 mt-3 mb-3 comic-card" data-id="${characterId}" data-name="${characterName}" data-img="${characterImg}" data-info="${characterInfo}">
<div class="img-container">
<img src="${characterImg}" class="card-img img-fluid" alt="${characterName}">
</div>
<div class="card-body mt-2">
<h5 class="card-title">${characterName}</h5>
<p class="card-text">Comics: ${characterComics}</p>
</div>
</div>`;
});
container.innerHTML = contentHtml;
// Add event listener to each character card
document.querySelectorAll('.comic-card').forEach(card => {
card.addEventListener('click', function () {
const characterId = this.getAttribute('data-id');
const characterName = this.getAttribute('data-name');
const characterImg = this.getAttribute('data-img');
const characterInfo = this.getAttribute('data-info');
setCharacterComics(characterId, characterName, characterImg, characterInfo);
});
});
}
// Function to display character's comics and add back button
function setCharacterComics(characterId, characterName, characterImg, characterInfo) {
contentHtml = ``;
document.getElementById('pagination-controls').classList.add('d-none');
console.log(previousSearchValue, previousSearchType, previousSortValue);
container.classList.add('loading');
fetch(`https://gateway.marvel.com:443/v1/public/characters/${characterId}/comics?ts=${TS}&apikey=${API_KEY}&hash=${API_HASH}`)
.then(res => res.json())
.then(info => {
const comics = info.data.results;
const comicsCount = comics.length;
contentHtml += `
<div class="col-12 col-md-8 d-flex flex-column character-container gap-5 mt-3 mb-3">
<div class="col-12 d-flex flex-wrap gap-4 mb-3 character-info">
<div class="card-header col-md-4 mt-3 mb-3">
<img src="${characterImg}" class="card-img img-fluid character-img" alt="${characterName}">
</div>
<div class="col-md-6 mt-2 mb-3 flex-column gap-4 info">
<h3 class="mt-3 mb-3">${characterName}</h3>
<p class="card-text text-wrap-balance mt-3 mb-3">${characterInfo}</p>
<div class="col-12 mt-4">
<button id="back-button" class="btn btn-dark">Back to Characters</button>
</div>
</div>
</div>
</div>
<div class="container results">
<h4 class="mt-2 mb-3">COMICS</h4>
<p class="results-comics">${comicsCount} RESULTS</p>
</div>
`;
if (comicsCount > 0) {
comics.forEach(comic => {
const comicTitle = comic.title;
const comicImg = `${comic.thumbnail.path}.${comic.thumbnail.extension}`;
const comicUrl = comic.urls[0].url;
contentHtml += `
<div class="card col-md-4 mt-3 mb-3 comic-card character-card bg-dark">
<div class="character-img">
<img src="${comicImg}" class="card-img img-fluid" alt="${comicTitle}">
</div>
<div class="card-body">
<p class="card-title"><a href="${comicUrl}" class="text-white" target="_blank">${comicTitle}</a></p>
</div>
</div>`;
});
} else {
contentHtml += `<p>No comics available for this character.</p>`;
}
container.innerHTML = contentHtml;
document.getElementById('back-button').addEventListener('click', function () {
document.getElementById('pagination-controls').classList.remove('d-none');
getData(previousSearchValue, previousSortValue, 'characters');
});
})
.catch(err => console.log(err))
.finally(() => {
container.classList.remove('loading');
});
}
// Function to fetch and display characters based on resourceURI
function displayComicCharacters(characters) {
let contentHtml = '';
characters.forEach(character => {
const characterName = character.name;
let characterResourceURI = character.resourceURI;
if (characterResourceURI.startsWith('http://')) {
characterResourceURI = characterResourceURI.replace('http://', 'https://');
}
console.log(characterResourceURI);
// Fetch each character's details (including the image)
fetch(`${characterResourceURI}?ts=${TS}&apikey=${API_KEY}&hash=${API_HASH}`)
.then(res => res.json())
.then(info => {
const characterDetails = info.data.results[0];
const characterImg = `${characterDetails.thumbnail.path}.${characterDetails.thumbnail.extension}`;
contentHtml += `
<div class="card col-md-4 mt-3 mb-3 bg-dark text-bg-dark character-card">
<div class="character-img">
<img src="${characterImg}" class="card-img img-fluid" alt="${characterName}">
</div>
<div class="card-body">
<h5 class="card-title">${characterName}</h5>
</div>
</div>
`;
// Insert characters into the container
document.getElementById('characters-container').innerHTML = contentHtml;
})
.catch(err => console.log(err));
});
}
// Event listener for search form submission
document.querySelector('form').addEventListener('submit', function (event) {
event.preventDefault();
currentPage = 1; // Reset to page 1 for new searches
// Update search variables based on user input
searchValue = document.getElementById('search-input').value.trim();
searchType = document.getElementById('search-type').value;
sortValue = document.getElementById('search-sort').value;
// Execute the search with updated values
getData(searchValue, sortValue, searchType);
});
// Pagination logic
const add = document.querySelector('#add');
const subs = document.querySelector('#subs');
const firstPage = document.querySelector('#first-page');
const lastPage = document.querySelector('#last-page');
// Event listeners for pagination buttons
add.addEventListener('click', () => changePage(1));
subs.addEventListener('click', () => changePage(-1));
firstPage.addEventListener('click', () => goToPage(1));
lastPage.addEventListener('click', () => goToPage(totalPages));
// Function to change page (next or previous)
function changePage(step) {
const nextPage = currentPage + step;
if (nextPage >= 1 && nextPage <= totalPages) {
currentPage = nextPage;
getData(searchValue, sortValue, searchType);
}
}
// Function to go directly to a specific page
function goToPage(pageNumber) {
if (pageNumber >= 1 && pageNumber <= totalPages) {
currentPage = pageNumber;
getData(searchValue, sortValue, searchType);
}
}
// Function to update the pagination buttons
function updatePaginationButtons() {
// Disable/enable buttons based on the current page
toggleButtonState(subs, currentPage === 1);
toggleButtonState(firstPage, currentPage === 1);
toggleButtonState(add, currentPage === totalPages);
toggleButtonState(lastPage, currentPage === totalPages);
// Update the page number display
document.getElementById('pages').textContent = currentPage;
}
// Helper function to toggle button state
function toggleButtonState(button, shouldDisable) {
button.disabled = shouldDisable;
}
// Initial call to load comics on page load
window.onload = function () {
currentPage = 1;
searchType = 'comics';
searchValue = '';
sortValue = '';
getData(searchValue, sortValue, searchType);
};