-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (84 loc) · 3 KB
/
app.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
const Url = `https://free-to-play-games-database.p.rapidapi.com/api/games` ;
let container = document.querySelector('.container');
let category;
const catBtns = document.querySelectorAll('.category');
let activeButton = null;
getGames(Url);
async function getGames(Url){
const resp = await fetch(Url, {
"method": "GET",
"headers": {
"x-rapidapi-key": "afc6f0d2a3msh2d0df9a159b63e8p140fd9jsne40877285a0d",
"x-rapidapi-host": "free-to-play-games-database.p.rapidapi.com"
}})
const data = await resp.json();
// console.log(data);
viewGames(data);
}
// viewGames(data);
catBtns.forEach((btn)=>{
btn.addEventListener('click', (e)=>{
// btn.classList.remove('active');
let currBtn = e.target;
currBtn.classList.add('active');
if(activeButton != null && activeButton != currBtn){
activeButton.classList.remove('active');
}
activeButton = currBtn;
category = activeButton.innerText;
getGamesByCategory(category);
// console.log(category);
});
})
async function getGamesByCategory(category){
let catUrl = `https://free-to-play-games-database.p.rapidapi.com/api/games?category=${category}` ;
const resp = await fetch(catUrl, {
"method": "GET",
"headers": {
"x-rapidapi-key": "afc6f0d2a3msh2d0df9a159b63e8p140fd9jsne40877285a0d",
"x-rapidapi-host": "free-to-play-games-database.p.rapidapi.com"
}})
const respData = await resp.json();
// console.log(respData);
viewGames(respData);
}
function viewGames(data){
let html = '';
data.forEach(element =>{
// console.log(element);
let {title, short_description, publisher, release_date, game_url, thumbnail} = element;
html += ` <div class="card">
<img src=${thumbnail} alt="" />
<div class="overview">
<h2 class="game-title">${title.toUpperCase()}</h2>
<div class="description">
${short_description}
</div>
<h3 class="publisher">${publisher}</h3>
<h4 class="releaseDate">
Released on : <span class="date">${release_date}</span>
</h4>
<a href=${game_url} target="_blank">Game Link</a>
</div>
</div>`;
});
container.innerHTML = html;
}
const search = document.getElementById('search');
let searchValue = '';
let searchGame = ()=>{
searchValue = search.value.toUpperCase();
const cards = document.querySelectorAll('.card');
cards.forEach(card =>{
let gameTitle = card.querySelector('.game-title').innerText;
if(gameTitle.includes(searchValue)){
card.style.display = 'block';
}else{
card.style.display = 'none';
}
})
}
search.addEventListener('input', searchGame);
function allGames(){
window.location.reload();
}