File size: 1,330 Bytes
f2b1f40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Movie data API - using The Movie Database API
const API_KEY = 'api_key=1cf50e6248dc270629e802686245c2c8';
const BASE_URL = 'https://api.themoviedb.org/3';
const API_URL = BASE_URL + '/discover/movie?sort_by=popularity.desc&' + API_KEY;
const IMG_URL = 'https://image.tmdb.org/t/p/w500';

// Fetch movies for rows
document.addEventListener('DOMContentLoaded', async () => {
  const rows = document.querySelectorAll('custom-row');
  
  rows.forEach(async (row) => {
    const category = row.getAttribute('category');
    let url;
    
    switch(category) {
      case 'popular':
        url = BASE_URL + '/movie/popular?' + API_KEY;
        break;
      case 'trending':
        url = BASE_URL + '/trending/movie/week?' + API_KEY;
        break;
      case 'top_rated':
        url = BASE_URL + '/movie/top_rated?' + API_KEY;
        break;
      default:
        url = API_URL;
    }
    
    try {
      const res = await fetch(url);
      const data = await res.json();
      row.setMovies(data.results);
    } catch (error) {
      console.error('Error fetching movies:', error);
    }
  });
});

// Search functionality
function searchMovies() {
  const searchInput = document.getElementById('search-input').value;
  if(searchInput) {
    window.location.href = `search.html?query=${encodeURIComponent(searchInput)}`;
  }
}