-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (133 loc) · 4.64 KB
/
index.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
const gamesUrl = 'https://free-to-play-games-database.p.rapidapi.com/api/games';
const display = document.querySelector("#cardList");
let selectedCategory
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': 'cb7c281af5mshed3ac801f114632p183454jsn726a4e5c6429',
'X-RapidAPI-Host': 'free-to-play-games-database.p.rapidapi.com'
}
};
let selectedGame = 0
/*-----------------creating each card to display the games------------------*/
const createGamesDisplay = (thumbnail, title, id) => {
const ul = document.querySelector("#cardList");
const li = document.createElement("li");
const img = document.createElement("img");
const h3 = document.createElement("h3");
li.className = "gameCard";
li.id = `${id}`
img.className = "img"
h3.className = "cardH3"
img.src = thumbnail;
h3.textContent = title;
li.append(img, h3)
ul.append(li)
}
/* ---------------------------- Clicking on Games --------------------------- */
const selectGame = () => {
let card = document.querySelectorAll(".gameCard")
for (let i = 0; i < card.length; i++) {
card[i].addEventListener("click", (e) => {
selectedGame = Number(card[i].id)
gameInfo()
})
}
}
const gameInfo = async () => {
try {
const gameRes = await fetch(gamesUrl, options);
const game = await gameRes.json();
const selected = game.filter(game => game.id === selectedGame)
showGame(selected[0])
} catch (error) {
console.error(error);
}
}
const showGame = (selection) => {
console.log(selection)
const card = document.getElementById("modal");
const info = document.getElementById("gameInfo")
info.innerHTML = ""
const image = document.createElement("img");
const title = document.createElement("h4");
const description = document.createElement("p")
const plat = document.createElement("p")
const pub = document.createElement("p")
const release = document.createElement("p")
image.src = selection.thumbnail
title.textContent = selection.title
description.textContent = `Description: ${selection.short_description}`
plat.textContent = `Platform: ${selection.platform}`
pub.textContent = `Publisher: ${selection.publisher}`
release.textContent = `Release date: ${selection.release_date}`
info.append(image, title, description, plat, pub, release)
card.showModal()
}
/* --------------------------- Clicking on Genres --------------------------- */
const selectGenre = () => {
let cats = Array.from(document.querySelectorAll(".genreButton"))
cats.forEach(genre => {
genre.addEventListener("click", () => {
selectedCategory = genre.textContent
genreInfo(selectedCategory)
})
})
}
const genreInfo = async (choice) => {
try {
const genresRes = await fetch(`https://free-to-play-games-database.p.rapidapi.com/api/games?category=${choice}`, options);
const genres = await genresRes.json();
display.innerHTML = ""
// console.log(genres)
genres.forEach(game => {
createGamesDisplay(game.thumbnail, game.title, game.id)
})
selectGame()
} catch (error) {
console.error(error);
}
}
/* ------------------------------- Search Bar ------------------------------- */
let searchUl
const createSearchStories = (title, articleURL) => {
searchUl = document.querySelector("#search-results");
const li = document.createElement("li");
const a = document.createElement("a");
a.href = articleURL;
a.textContent = title;
li.append(a);
searchUl.append(li);
}
let searchTerm;
document.getElementById("spencers").addEventListener("submit", async (e) => {
e.preventDefault()
searchTerm = e.target[0].value
searchStories(`${searchTerm}`)
display.innerHTML = ""
})
const searchStories = async (search) => {
const res = await fetch(gamesUrl, options)
const searchRes = await res.json()
console.log(search)
searchRes.filter(game => {
if (game.title.toLowerCase().includes(search.toLowerCase())) {
console.log(game)
createGamesDisplay(game.thumbnail, game.title, game.id)
}
})
selectGame()
}
/* -------------------------------------------------------------------------- */
const displayGame = async () => {
try {
const gameRes = await fetch(gamesUrl, options);
const games = await gameRes.json();
games.forEach(game => { createGamesDisplay(game.thumbnail, game.title, game.id) })
selectGenre()
selectGame()
} catch (error) {
console.error(error);
}
}
displayGame()