-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
69 lines (61 loc) · 2.87 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
function showLoading() {
const searchButton = document.getElementById('searchButton');
const loadingSpinner = document.getElementById('loadingSpinner');
searchButton.disabled = true;
searchButton.style.cursor = 'not-allowed';
document.getElementById('searchText').style.display = 'none';
loadingSpinner.style.display = 'inline-block';
}
function hideLoading() {
const searchButton = document.getElementById('searchButton');
const loadingSpinner = document.getElementById('loadingSpinner');
searchButton.disabled = false;
searchButton.style.cursor = 'pointer';
document.getElementById('searchText').style.display = 'inline';
loadingSpinner.style.display = 'none';
}
function searchMovies(query) {
if (!query) {
alert('Please enter a movie name before searching.');
return;
}
showLoading();
fetch(`https://api.yanzbotz.my.id/api/movie/layarkaca21?query=${query}`)
.then(response => response.json())
.then(data => {
const movieResults = document.getElementById('movieResults');
movieResults.innerHTML = '';
data.result.forEach(movie => {
const categories = movie.categories.map(category => `<span class="category-capsule">${category}</span>`).join(', ');
const card = `
<div class="col-12 col-sm-6 col-md-4 col-lg-3">
<div class="card">
<img src="${movie.posterImage}" class="card-img-top" alt="${movie.title}">
<div class="card-body">
<h5 class="card-title">${movie.title}</h5>
<p class="card-text">Director: ${movie.directors[0]}</p>
<p class="card-text">Cast: ${movie.cast.join(', ')}</p>
<p class="card-text">Categories: ${categories}</p>
<a href="${movie.movieLink}" class="btn btn-primary btn-block">Detail</a>
</div>
</div>
</div>
`;
movieResults.innerHTML += card;
});
hideLoading();
})
.catch(error => console.error(error));
}
document.getElementById('searchButton').addEventListener('click', () => {
const searchInput = document.getElementById('searchInput');
const query = searchInput.value;
searchMovies(query);
});
document.getElementById('searchInput').addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
const searchInput = document.getElementById('searchInput');
const query = searchInput.value;
searchMovies(query);
}
});