Mosnad-Web01 / oldversion

0 stars 0 forks source link

Add fetchDataFromTMDB function for TMDB API integration #5

Closed ahmedalsanadi closed 1 month ago

ahmedalsanadi commented 1 month ago

This pull request introduces a reusable fetchDataFromTMDB function to handle API requests to the TMDB (The Movie Database) API. The function abstracts the logic for making fetch requests to the TMDB API by dynamically constructing the endpoint URL and including the required API key, which is stored in the .env.local file for security.

Changes:

Usage: The fetchDataFromTMDB function can be reused throughout the project to fetch data from the TMDB API by passing a suitable endpoint. For example, it can be used in a component like this:


import { useEffect, useState } from 'react';
import { fetchDataFromTMDB } from '../utile/fetchDataFromTMDB';

const PopularMovies = () => {
  const [movies, setMovies] = useState([]);

  useEffect(() => {
    const fetchMovies = async () => {
      const data = await fetchDataFromTMDB('/movie/popular');
      if (data) {
        setMovies(data.results);
      }
    };

    fetchMovies();
  }, []);

  return (
    <div>
      <h2>Popular Movies</h2>
      <ul>
        {movies.map(movie => (
          <li key={movie.id}>{movie.title}</li>
        ))}
      </ul>
    </div>
  );
};

export default PopularMovies;