@ -6,38 +6,59 @@ const main = document.getElementById('main')
const form = document . getElementById ( 'form' )
const form = document . getElementById ( 'form' )
const search = document . getElementById ( 'search' )
const search = document . getElementById ( 'search' )
// Get initial movies
getMovies ( API _URL )
async function getMovies ( url ) {
async function getMovies ( url ) {
const res = await fetch ( url )
try {
const data = await res . json ( )
showLoading ( ) ;
const res = await fetch ( url ) ;
if ( ! res . ok ) throw new Error ( ` HTTP error! status: ${ res . status } ` ) ;
const data = await res . json ( ) ;
showMovies ( data . results )
if ( data . results . length === 0 ) {
main . innerHTML = '<div class="no-results"><h2>No movies found</h2><p>Try searching for a different movie or check your spelling.</p></div>' ;
} else {
showMovies ( data . results ) ;
}
} catch ( error ) {
console . error ( 'Fetch error:' , error ) ;
main . innerHTML = ` <div class="error-message"><h2>Oops! Something went wrong</h2><p>Please check your internet connection and try again.</p><button onclick="location.reload()" class="retry-btn">Retry</button></div> ` ;
}
}
}
function showMovies ( movies ) {
main . innerHTML = ''
movies . forEach ( ( movie ) => {
function showMovies ( movies ) {
const { title , poster _path , vote _average , overview } = movie
main . innerHTML = '' ;
const movieEl = document . createElement ( 'div' )
movieEl . classList . add ( 'movie' )
movies . forEach ( movie => {
const { title , poster _path , vote _average , overview , release _date , id , genre _ids } = movie ;
const movieEl = document . createElement ( 'div' ) ;
movieEl . classList . add ( 'movie' ) ;
const year = release _date ? new Date ( release _date ) . getFullYear ( ) : 'N/A' ;
const poster = poster _path ? IMG _PATH + poster _path : 'https://via.placeholder.com/300x450/2e3152/ffffff?text=No+Image' ;
movieEl . innerHTML = `
movieEl . innerHTML = `
< img src = "${IMG_PATH + poster_path}" alt = "${title}" >
< img src = "${ poster}" alt = "${title} " loading = "lazy ">
< div class = "movie-info" >
< div class = "movie-info" >
< h3 > $ { title } < / h 3 >
< h3 > $ { title } < / h 3 >
< span class = "${getClassByRate(vote_average)}" > $ { vote _average } < / s p a n >
< span class = "${getClassByRate(vote_average)}" > $ { vote _average . toFixed ( 1 ) } < / s p a n >
< / d i v >
< / d i v >
< div class = "overview" >
< div class = "overview" >
< h3 > Overview < / h 3 >
< h3 > $ { title } ( $ { year } ) < / h 3 >
$ { overview }
< p > $ { overview ? overview . substring ( 0 , 150 ) + '...' : 'No overview available.' } < / p >
< / d i v >
< div class = "movie-actions" >
`
< button class = "btn-watch" onclick = "watchMovie(${id})" > Watch Now < / b u t t o n >
main . appendChild ( movieEl )
< button class = "btn-trailer" onclick = "watchTrailer('${title}')" > Trailer < / b u t t o n >
} )
< button class = "btn-details" onclick = "showMovieDetails(${id})" > Details < / b u t t o n >
< / d i v >
< / d i v >
` ;
main . appendChild ( movieEl ) ;
} ) ;
}
}
function getClassByRate ( vote ) {
function getClassByRate ( vote ) {
@ -50,16 +71,118 @@ function getClassByRate(vote) {
}
}
}
}
form . addEventListener ( 'submit' , ( e ) => {
function watchMovie ( movieId ) {
e . preventDefault ( )
showNotification ( 'Opening movie player...' , 'info' ) ;
// Simulate opening a movie player
setTimeout ( ( ) => {
showNotification ( 'Movie player is ready!' , 'success' ) ;
} , 1000 ) ;
}
const searchTerm = search . value
function watchTrailer ( movieTitle ) {
showNotification ( ` Loading trailer for: ${ movieTitle } ` , 'info' ) ;
// Simulate opening trailer
setTimeout ( ( ) => {
showNotification ( 'Trailer is playing!' , 'success' ) ;
} , 1500 ) ;
}
if ( searchTerm && searchTerm !== '' ) {
async function showMovieDetails ( movieId ) {
getMovies ( SEARCH _API + searchTerm )
try {
const response = await fetch ( ` https://api.themoviedb.org/3/movie/ ${ movieId } ?api_key=3fd2be6f0c70a2a598f084ddfb75487c ` ) ;
const movie = await response . json ( ) ;
const modal = document . createElement ( 'div' ) ;
modal . className = 'modal' ;
modal . innerHTML = `
< div class = "modal-content" >
< span class = "close" > & times ; < / s p a n >
< div class = "modal-body" >
< div class = "modal-poster" >
< img src = "${movie.poster_path ? IMG_PATH + movie.poster_path : 'https://via.placeholder.com/300x450/2e3152/ffffff?text=No+Image'}" alt = "${movie.title}" >
< / d i v >
< div class = "modal-info" >
< h2 > $ { movie . title } < / h 2 >
< div class = "movie-meta" >
< span class = "year" > $ { movie . release _date ? new Date ( movie . release _date ) . getFullYear ( ) : 'N/A' } < / s p a n >
< span class = "runtime" > $ { movie . runtime ? movie . runtime + ' min' : 'N/A' } < / s p a n >
< span class = "rating ${getClassByRate(movie.vote_average)}" > $ { movie . vote _average . toFixed ( 1 ) } < / s p a n >
< / d i v >
< p class = "overview-text" > $ { movie . overview || 'No overview available.' } < / p >
< div class = "genres" >
$ { movie . genres ? movie . genres . map ( genre => ` <span class="genre-tag"> ${ genre . name } </span> ` ) . join ( '' ) : '' }
< / d i v >
< div class = "modal-actions" >
< button class = "btn-watch" onclick = "watchMovie(${movie.id})" > Watch Now < / b u t t o n >
< button class = "btn-trailer" onclick = "watchTrailer('${movie.title}')" > Watch Trailer < / b u t t o n >
< button class = "btn-add-list" onclick = "addToMyList(${movie.id})" > Add to My List < / b u t t o n >
< / d i v >
< / d i v >
< / d i v >
< / d i v >
` ;
document . body . appendChild ( modal ) ;
// Close modal functionality
const closeBtn = modal . querySelector ( '.close' ) ;
closeBtn . onclick = ( ) => modal . remove ( ) ;
modal . onclick = ( e ) => {
if ( e . target === modal ) modal . remove ( ) ;
} ;
} catch ( error ) {
showNotification ( 'Failed to load movie details' , 'error' ) ;
}
}
search . value = ''
function addToMyList ( movieId ) {
showNotification ( 'Added to My List!' , 'success' ) ;
// Here you would typically save to localStorage or send to backend
}
// Search functionality
form . addEventListener ( 'submit' , ( e ) => {
e . preventDefault ( )
const searchTerm = search . value . trim ( )
if ( searchTerm ) {
getMovies ( SEARCH _API + encodeURIComponent ( searchTerm ) ) ;
search . value = '' ;
} else {
} else {
window . location . reload ( )
showNotification ( 'Please enter a search term.' , 'error' ) ;
}
} )
// Real-time search suggestions
let searchTimeout ;
search . addEventListener ( 'input' , ( e ) => {
clearTimeout ( searchTimeout ) ;
const searchTerm = e . target . value . trim ( ) ;
if ( searchTerm . length > 2 ) {
searchTimeout = setTimeout ( ( ) => {
getMovies ( SEARCH _API + encodeURIComponent ( searchTerm ) ) ;
} , 500 ) ;
}
}
} )
} ) ;
function showNotification ( message , type = 'info' ) {
const notification = document . createElement ( 'div' ) ;
notification . className = ` notification ${ type } ` ;
notification . textContent = message ;
document . body . appendChild ( notification ) ;
setTimeout ( ( ) => {
notification . remove ( ) ;
} , 3000 ) ;
}
// Initialize the app
getMovies ( API _URL ) ;
} )