-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (112 loc) · 3.89 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
/** @format */
var searchSuggestion = document.getElementById("searchSuggestion");
var timerId;
async function searchCharacter() {
let query = document.getElementById("serch").value;
if (query.length <= 2) {
document.getElementById("searchSuggestion").style.borderTop = "none";
document.getElementById("container").style.marginTop = "20rem";
return false;
}
let res = await fetch(`https://swapi.dev/api/people/?search=${query}`);
let data = await res.json();
if (data.results.length == 0) {
document.getElementById("noResult").style.display = "block";
} else if (data.results.length !== 0) {
document.getElementById("noResult").style.display = "none";
}
return data.results;
}
function throttleFunction() {
if (timerId) {
return false;
}
timerId = setTimeout(() => {
main();
timerId = undefined;
}, 500);
}
function appendSearchSuggetion(d) {
searchSuggestion.innerHTML = null;
d.forEach(
({
name: names,
gender,
birth_year,
height,
eye_color,
mass,
hair_color,
}) => {
let div = document.createElement("div");
div.setAttribute("class", "searchedItems");
let li = document.createElement("li");
li.innerText = names;
li.setAttribute("class", "suggestionP");
li.addEventListener("click", appendDetails);
//***********Appending in nextPage */
li.addEventListener("click", () => {
document.getElementById("nameDetails").innerText = names;
document.getElementById(
"birthYearP"
).innerText = `Birth Year : ${birth_year}`;
document.getElementById("genderP").innerText = `Gender : ${gender}`;
document.getElementById("heightP").innerText = `Height : ${height}`;
document.getElementById(
"eyeColor"
).innerText = `Eye Color : ${eye_color}`;
document.getElementById("massP").innerText = `Mass : ${mass}`;
document.getElementById(
"hairColorP"
).innerText = `Hair Color : ${hair_color}`;
});
//***********Appending in nextPage */
let span = document.createElement("span");
span.innerText = gender;
span.style.fontSize = "1.3rem";
span.style.color = "rgb(175, 174, 174)";
div.append(li, span);
let divTwo = document.createElement("div");
let span_birth = document.createElement("p");
span_birth.innerText = birth_year;
span_birth.setAttribute("class", "spanYear");
divTwo.append(span_birth);
searchSuggestion.append(div, divTwo);
}
);
}
// **********************************
async function main() {
let character = await searchCharacter();
if (character.length != 0) {
let names = document.getElementById("searchSuggestion");
names.style.display = "block";
appendSearchSuggetion(character);
document.getElementById("container").style.marginTop = "10rem";
document.getElementById("searchSuggestion").style.borderTop =
"1px solid white";
console.log(character);
} else {
searchSuggestion.style.display = "none";
names.style.display = "none";
}
}
// **********************************
// **********************************
// function for showing another container if clicking on any suggestions
function appendDetails() {
document.getElementById("container").style.display = "none";
document.getElementById("detailsContainer").style.display = "block";
}
// **********************************
// **********************************
///Function for back button
function backToHome() {
document.getElementById("container").style.display = "block";
document.getElementById("searchSuggestion").style.borderTop = "none";
document.getElementById("detailsContainer").style.display = "none";
searchSuggestion.innerHTML = null;
document.getElementById("serch").value = null;
document.getElementById("container").style.marginTop = "20rem";
}
// **********************************