From 36dbe6ceb54e3add1c96f169efc0e3b5136bfb70 Mon Sep 17 00:00:00 2001 From: Vinayrk69 <141519053+Vinayrk69@users.noreply.github.com> Date: Fri, 8 Aug 2025 23:50:37 +0530 Subject: [PATCH] Update script.js --- movie-app/script.js | 181 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 152 insertions(+), 29 deletions(-) diff --git a/movie-app/script.js b/movie-app/script.js index 469f141..9800a5c 100644 --- a/movie-app/script.js +++ b/movie-app/script.js @@ -6,38 +6,59 @@ const main = document.getElementById('main') const form = document.getElementById('form') const search = document.getElementById('search') -// Get initial movies -getMovies(API_URL) + + async function getMovies(url) { - const res = await fetch(url) - const data = await res.json() + try { + 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 = '

No movies found

Try searching for a different movie or check your spelling.

'; + } else { + showMovies(data.results); + } + } catch (error) { + console.error('Fetch error:', error); + main.innerHTML = `

Oops! Something went wrong

Please check your internet connection and try again.

`; + } } -function showMovies(movies) { - main.innerHTML = '' - movies.forEach((movie) => { - const { title, poster_path, vote_average, overview } = movie - - const movieEl = document.createElement('div') - movieEl.classList.add('movie') +function showMovies(movies) { + main.innerHTML = ''; + 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 = ` - ${title} + ${title}
-

${title}

- ${vote_average} +

${title}

+ ${vote_average.toFixed(1)}
-

Overview

- ${overview} -
- ` - main.appendChild(movieEl) - }) +

${title} (${year})

+

${overview ? overview.substring(0, 150) + '...' : 'No overview available.'}

+
+ + + +
+ + `; + + main.appendChild(movieEl); + }); } function getClassByRate(vote) { @@ -50,16 +71,118 @@ function getClassByRate(vote) { } } -form.addEventListener('submit', (e) => { - e.preventDefault() +function watchMovie(movieId) { + 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 !== '') { - getMovies(SEARCH_API + searchTerm) +async function showMovieDetails(movieId) { + 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 = ` + + `; + + 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 { - 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); } -}) \ No newline at end of file +}); + +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); + + + +})