-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
222 lines (193 loc) · 5.7 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// lancer db_connection avant !! il faut creer l'url
// node db_co... dans console
// fetch voir : https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
const form = document.querySelector('#form')
const search = document.querySelector('#search')
const main = document.querySelector('#main')
let notFound = true
let genres = []
const url2 = 'http://localhost:8000/api/genres'
function getGenre() {
fetch(url2)
.then((res) => res.json())
.then((data) => {
//console.log(data)
data.forEach((movie) => {
let genre = movie.name
genres.push(genre)
})
})
//console.log(genres)
}
getGenre()
let producers = []
const url3 = 'http://localhost:8000/api/producers'
function getProd() {
fetch(url3)
.then((res) => res.json())
.then((data) => {
// console.log(data)
data.forEach((movie) => {
let prod = movie.name
producers.push(prod)
})
})
//console.log(producers);
}
getProd()
const url = 'http://localhost:8000/api/movies'
function getMovies() {
fetch(url)
.then((res) => res.json())
.then((data) => {
//console.log(data)
showMovies(data)
})
}
getMovies()
function showMovies(movies) {
main.innerHTML = ''
movies.forEach((movie) => {
let prod_id = movie.producer_id
let prod = producers[prod_id]
prod = prod != null ? prod : ' unknown'
let genre_id = movie.genre_id
let genre = genres[genre_id]
let title = movie.title
let summary = movie.summary
let release_date = movie.release_date
release_date = release_date ? movie.release_date.slice(0, 4) : ' unknown'
let prod_year = movie.prod_year
prod_year = prod_year != 0 ? prod_year : ' unknown'
let min_duration = movie.min_duration
min_duration = min_duration != null ? min_duration : ' unknown'
const movieEl = document.createElement('div')
movieEl.classList.add('movie')
movieEl.innerHTML = `
<div class="movie-info">
<h3>${title}</h3>
<h6>${genre}</h6>
</div>
<div class="summary">
<h3>Summary</h3>
<div class="pitch">
${summary}
</div>
</div>
<button class="more">
More infos
</button>
<div class="overview">
<h4>More infos</h4>
<p>Producer : ${prod}</p>
<p>Release date : ${release_date}</p>
<p>Production year : ${prod_year}</p>
<p>Duration : ${min_duration} min</p>
</div>`
main.appendChild(movieEl)
})
const boxes = document.querySelectorAll('.movie')
//console.log(boxes)
boxes.forEach((box) => {
box.classList.add('show')
})
window.addEventListener('scroll', () => {
const triggerBottom = (window.innerHeight / 5) * 3.5
boxes.forEach((box) => {
const boxTop = box.getBoundingClientRect().top
if (boxTop < triggerBottom) {
box.classList.add('show')
} else {
box.classList.remove('show')
}
})
})
}
const url4 = 'http://localhost:8000/api/allmovies'
function getSearch(searchTerm) {
notFound = true
fetch(url4)
.then((res) => res.json())
.then((data) => {
data.forEach((movie) => {
if (movie.title.includes(searchTerm)) {
//console.log(movie.title)
notFound = false
let prod_id = movie.producer_id
let prod = producers[prod_id]
prod = prod != null ? prod : ' unknown'
let genre_id = movie.genre_id
let genre = genres[genre_id]
let title = movie.title
let summary = movie.summary
let release_date = movie.release_date
release_date = release_date
? movie.release_date.slice(0, 4)
: ' unknown'
let prod_year = movie.prod_year
prod_year = prod_year != 0 ? prod_year : ' unknown'
let min_duration = movie.min_duration
min_duration = min_duration != null ? min_duration : ' unknown'
const movieEl = document.createElement('div')
movieEl.classList.add('movie')
movieEl.innerHTML = `
<div class="movie-info">
<h3>${title}</h3>
<h6>${genre}</h6>
</div>
<div class="summary">
<h3>Summary</h3>
<div class="pitch">
${summary}
</div>
</div>
<button class="more">
More infos
</button>
<div class="overview">
<h4>More infos</h4>
<p>Producer : ${prod}</p>
<p>Release date : ${release_date}</p>
<p>Production year : ${prod_year}</p>
<p>Duration : ${min_duration} min</p>
</div>`
main.appendChild(movieEl)
}
})
if (notFound) {
const notFound = document.createElement('p')
notFound.classList.add('notFound')
notFound.innerText = 'No film found'
main.appendChild(notFound)
}
const boxes = document.querySelectorAll('.movie')
//console.log(boxes)
boxes.forEach((box) => {
box.classList.add('show')
})
window.addEventListener('scroll', () => {
const triggerBottom = (window.innerHeight / 5) * 3.5
boxes.forEach((box) => {
const boxTop = box.getBoundingClientRect().top
if (boxTop < triggerBottom) {
box.classList.add('show')
} else {
box.classList.remove('show')
}
})
})
})
}
form.addEventListener('submit', (e) => {
console.log(e)
e.preventDefault()
const searchTerm = search.value
if (searchTerm && searchTerm != '') {
main.innerHTML = ''
getSearch(searchTerm)
// console.log(searchTerm)
search.value = ''
} else {
window.location.reload() // recharge la page
}
})