forked from thinkswell/javascript-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (143 loc) · 5.01 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
var res,child,remove;
MovieSearch = async (TitleSearch) =>
{
const getData = await axios.get('https://www.omdbapi.com/', {
params :
{
apiKey : '1d56cf35',
s : TitleSearch
}
})
if(getData.data.Response === "False")
{
res = 'Movie not Found!'
return 'False'
}
// console.log(getData.data.Search)
else{res = getData.data.Search
return getData.data.Search}
}
let timeOutId;
const inputSearch = document.querySelector('#inputSearch')
inputSearch.addEventListener('input', (event) =>{
if (timeOutId){clearInterval(timeOutId)}
timeOutId = setTimeout(async () =>{
var results = await MovieSearch(event.target.value)
.catch((error)=>(console.log("error is : ", error)))
if (results === 'False'){
if(document.querySelector('#appendChild').children)
{
const list = document.getElementById("appendChild");
while (list.hasChildNodes()) {
list.removeChild(list.firstChild);
}
}
// console.log('if ran')
const createElement = document.createElement('div')
createElement.innerHTML = `
<br>
<p>No results for the search Term<p>
`
document.querySelector('#appendChild').appendChild(createElement)
}
else
{
// console.log("else block ran")
if(document.querySelector('#appendChild').children)
{
// remove = document.querySelector('#appendChild')
// child = document.querySelector('#appendChild').children
// for(let k = 0; k<=child.length; k++)
// {
// const ele = document.getElementsByTagName('div')
// document.querySelector('#appendChild').removeChild(ele)
// }
const list = document.getElementById("appendChild");
while (list.hasChildNodes()) {
list.removeChild(list.firstChild);
}
}
// const br = document.createElement('br')
// document.querySelector('#appendChild').appendChild(br)
const createElement = document.createElement('div')
createElement.innerHTML = `
<div class = "dropdown is-active" id='to-be-removed'>
<div class = "dropdown-menu">
<div class = "dropdown-content results">
</div>
</div>
</div>
`
document.querySelector('#appendChild').appendChild(createElement)
// var value = -1;
for (let result of results)
{
// value++;
if(result.Poster === "N/A")
{result.Poster=""}
const createAnchor = document.createElement('a')
createAnchor.classList.add('dropdown-item');
createAnchor.innerHTML = `
<img src = "${result.Poster}"></img>
${result.Title}
`
createAnchor.addEventListener('click', (event) =>
{
// console.log(event.path[0])
const remove = document.querySelector("#to-be-removed")
remove.classList.remove('is-active')
inputSearch.value = event.target.innerText.trim()
// console.log(result)
getAdditional(result)
})
document.querySelector('.dropdown-content').appendChild(createAnchor)
}
// child = document.querySelector('#appendChild').children
// console.log(`The Child of div are ${document.querySelector('#appendChild').children}`)
}
}, 1000)
})
document.addEventListener('click', (event) =>
{
// console.log(event.target.classList.value)
if(event.target.innerHTML !== `<input id="inputSearch" type="text">` && event.target.classList.value !== `dropdown-item`)
{
const removeElements = document.getElementById("appendChild");
while (removeElements.hasChildNodes()) {
removeElements.removeChild(removeElements.firstChild);
}
}
})
const getAdditional = async function (extractInfo)
{
// console.log(extractInfo)
var ImdbIDSearch = await axios.get('https://www.omdbapi.com', {
params :
{
apiKey : '1d56cf35',
i : extractInfo.imdbID
}
})
const searchResult = document.createElement('div')
searchResult.innerHTML = ( (renderTemplate(ImdbIDSearch)))
document.querySelector('#appendChild').appendChild(searchResult)
}
function renderTemplate(ImdbIDSearch)
{
return `
<article class="media">
<figure class="media-left">
<p class="image">
<img src="${ImdbIDSearch.data.Poster}" />
</p>
</figure>
<div class="media-content">
<div class="content">
<h1>${ImdbIDSearch.data.Title}</h1>
<h4>${ImdbIDSearch.data.Genre}</h4>
<p>${ImdbIDSearch.data.Plot}</p>
</div>
</div>
</article>
`;
}