-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetails.js
More file actions
89 lines (73 loc) · 2.52 KB
/
details.js
File metadata and controls
89 lines (73 loc) · 2.52 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
// ? =============> Global ===============>
const searchParams = location.search;
const modeBtn = document.getElementById("mode");
// ! =============> When Start ===============>
const params = new URLSearchParams(searchParams);
const id = params.get("id");
getDetails(id);
if (localStorage.getItem("theme")) {
const theme = localStorage.getItem("theme");
console.log(theme);
document.documentElement.dataset.theme = localStorage.getItem("theme");
if (theme === "light") {
modeBtn.classList.replace("fa-sun", "fa-moon");
} else {
modeBtn.classList.replace("fa-moon", "fa-sun");
}
}
// ? =============> Event ===============>
modeBtn.addEventListener("click", function (e) {
theme(e.target);
});
// ! =============> Functions ===============>
async function getDetails(id) {
const options = {
method: "GET",
headers: {
"X-RapidAPI-Key": "761b8a3226msh868f0d927cb6ea4p117ef0jsn46d63d281712",
"X-RapidAPI-Host": "free-to-play-games-database.p.rapidapi.com",
Accept: "application/json",
"Content-Type": "application/json",
},
};
const api = await fetch(`https://free-to-play-games-database.p.rapidapi.com/api/game?id=${id}`, options);
const response = await api.json();
displayData(response);
console.log(response);
}
function displayData(data) {
let detailsBox = `
<div class="col-md-4">
<figure>
<img src="${data.thumbnail}" class="w-100" alt="details image" />
</figure>
</div>
<div class="col-md-8">
<div>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item text-reset"><a href="./home.html">Home</a></li>
<li class="breadcrumb-item text-info" aria-current="page">${data.title}</li>
</ol>
</nav>
<h1>${data.title}</h1>
<h3>About ${data.title}</h3>
<p>${data.description}</p>
</div>
</div>
`;
document.getElementById("detailsData").innerHTML = detailsBox;
document.body.style.cssText = `background:url('${data.thumbnail.replace("thumbnail", "background")}') center / cover no-repeat`;
}
function theme(element) {
const rootElement = document.documentElement;
if (element.classList.contains("fa-sun")) {
element.classList.replace("fa-sun", "fa-moon");
rootElement.dataset.theme = "light";
localStorage.setItem("theme", "light");
} else {
element.classList?.replace("fa-moon", "fa-sun");
rootElement.dataset.theme = "dark";
localStorage.setItem("theme", "dark");
}
}