-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (69 loc) · 2.28 KB
/
script.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
const API_URL = 'https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=e15c56dc03f0f326c38d49f23ebe4509&page=1'
const IMG_PATH = 'https://image.tmdb.org/t/p/w1280'
const SEARCH_API = 'https://api.themoviedb.org/3/search/movie?api_key=e15c56dc03f0f326c38d49f23ebe4509&query="'
const form = document.querySelector('.form')
const search = document.querySelector('.search')
const main = document.querySelector('.main')
const descHeader = document.querySelector('.desc-header')
const notFound = document.querySelector('.not-found')
//Get initial movies
getMovies(API_URL)
async function getMovies(url){
const res = await axios.get(url)
const data = await res.data
// console.log(data.results)
showMovies(data.results)
}
function showMovies(movies){
main.innerHTML = ''
movies.forEach(movie => {
const {title, poster_path, vote_average, overview, release_date} = movie
const date = release_date.slice(0,4)
const movieTitle = title.slice(0,35)
const movieOverview = overview.slice(0,400)
const movieElement = document.createElement('div')
movieElement.classList.add('movie')
movieElement.innerHTML =
`
<div class+"img-container>
<img src="${IMG_PATH + poster_path}" alt="${movieTitle}">
<div class="year">${date}</div>
</div>
<div class="movie-info">
<h3>${movieTitle}</h3>
<span class="${getClassByRate(vote_average)}">${vote_average}</span>
</div>
<div class="overview">
<h3>Overview</h3>
<small>${movieOverview}</small>
</div>
`
main.appendChild(movieElement)
});
}
function getClassByRate(vote){
if(vote>=8){
return 'green'
}
if(vote>=7){
return 'yellow'
}
if(vote>=5){
return 'orange'
}
else{
return 'red'
}
}
form.addEventListener('submit', (event) =>{
event.preventDefault()
const searchTerm = search.value
if(searchTerm && searchTerm !== ''){
getMovies(SEARCH_API + searchTerm)
search.value = ''
descHeader.innerText = "Search Results for : " + searchTerm
}
else{
window.location.reload()
}
})