-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
74 lines (56 loc) · 2.47 KB
/
script.js
File metadata and controls
74 lines (56 loc) · 2.47 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
const animeApiUrl = "https://api.jikan.moe/v3";
function searchAnime(event){
event.preventDefault();
const form = new FormData(this);
const query = form.get("search");
fetch(`${animeApiUrl}/search/anime?q=${query}&page=1`)
.then(response=>response.json())
.then(animeDisplay)
.catch(err=>console.warn(err.message));
}
function animeDisplay(data){
const searchResults = document.getElementById('search-results');
const animeByCategories = data.results
.reduce((acc, anime)=>{
const {type} = anime;
if(acc[type] === undefined) acc[type] = [];
acc[type].push(anime);
return acc;
}, {});
searchResults.innerHTML = Object.keys(animeByCategories).map(key=>{
const animes = animeByCategories[key]
.sort((a,b)=>a.episodes-b.episodes)
.map(anime=>{
return `
<div class="card">
<div class="card-image">
<img class="image" src="${anime.image_url}">
</div>
<div class="card-content">
<div><h3 class="card-title">${anime.title}</h3></div>
<p class="synopsis">${anime.synopsis}</p>
<span class="card-details">Start Date: ${anime.start_date}</span>
<span class="card-details">End Date: ${anime.end_date}</span>
<span class="card-details">Score: ${anime.score}</span>
<span class="card-details">Type: ${anime.type}</span>
<span class="card-details">Rated: ${anime.rated}</span>
</div>
<div class="card-action">
<button><a href="${anime.url}">More Info</a></button>
</div>
</div>
`
}).join("");
return `
<section>
<h3>${key.toUpperCase()}</h3>
<div class="anime-row">${animes}</div>
</section>
`
}).join("");
}
function pageLoaded(){
const form = document.getElementById('search_form');
form.addEventListener("submit", searchAnime);
}
window.addEventListener("load", pageLoaded);