From 9b74f43559f7e10af637c21f5c452decb735c4c9 Mon Sep 17 00:00:00 2001 From: iqbalpa Date: Thu, 11 Jul 2024 16:44:32 +0700 Subject: [PATCH] fix: error in api --- src/api/api.tsx | 12 +++--------- src/components/header/header.tsx | 2 +- src/modules/detailMovie/detailMovie.tsx | 4 ++-- src/modules/home/home.tsx | 7 +++---- 4 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/api/api.tsx b/src/api/api.tsx index d062f8f..caf9966 100644 --- a/src/api/api.tsx +++ b/src/api/api.tsx @@ -6,7 +6,7 @@ const BASE_URL = 'https://api.themoviedb.org/3/'; const API_KEY = 'eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI1MzM0Njg0NjI2OTg1ODI3OTE1NzMyNWY5OTAxZmU4ZCIsIm5iZiI6MTcyMDU4OTg3MC4wMzYzNDIsInN1YiI6IjY2OGUxY2JlMzA0OTRhNmE2OTMyYzE3MCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.jkIXGMw1WWmtEO9XDryk4R5gP35WQIZHQ8uGIH-QgnM'; -const getAllMovies = async (currentPage: number): Promise => { +export const getAllMovies = async (currentPage: number): Promise => { const res = await axios.get( `${BASE_URL}discover/movie?include_adult=true&include_video=false&language=en-US&page=${currentPage}&sort_by=popularity.desc`, { @@ -20,7 +20,7 @@ const getAllMovies = async (currentPage: number): Promise => { return movies; }; -const getMoviesWithQuery = async ( +export const getMoviesWithQuery = async ( query: string, currentPage: number, ): Promise => { @@ -38,7 +38,7 @@ const getMoviesWithQuery = async ( return movies; }; -const getMovieById = async (id: number): Promise => { +export const getMovieById = async (id: number): Promise => { const res = await axios.get( `${BASE_URL}movie/${id}?append_to_response=credits`, { @@ -56,9 +56,3 @@ const getMovieById = async (id: number): Promise => { const parseQuery = (query: string): string => { return query.replace(' ', '+'); }; - -export default { - getAllMovies, - getMoviesWithQuery, - getMovieById, -}; diff --git a/src/components/header/header.tsx b/src/components/header/header.tsx index b47dfb7..2cfe469 100644 --- a/src/components/header/header.tsx +++ b/src/components/header/header.tsx @@ -25,7 +25,7 @@ const Header: React.FC = () => { isScrolled ? 'bg-opacity-100' : 'bg-opacity-10' } bg-slate-800`} > -
+
= ({ id }) => { useEffect(() => { const fetchMovie = async () => { try { - const res = await api.getMovieById(parseInt(id)); + const res = await getMovieById(parseInt(id)); setMovie(res); document.title = `${res.title} (${res.release_date.split('-')[0]})`; } catch (e) { diff --git a/src/modules/home/home.tsx b/src/modules/home/home.tsx index 4cb9793..44c6f55 100644 --- a/src/modules/home/home.tsx +++ b/src/modules/home/home.tsx @@ -1,9 +1,8 @@ 'use client'; import React, { ChangeEvent, useEffect, useState } from 'react'; -import api from '@/api/api'; +import { getAllMovies, getMoviesWithQuery } from '@/api/api'; import { Movie } from '@/constant/movie'; -import Header from '@/components/header/header'; import Backdrop from '@/components/backdrop/backdrop'; import ListMovies from '@/components/listMovies/listMovies'; import MyPagination from '@/components/myPagination/myPagination'; @@ -21,7 +20,7 @@ const HomeModule: React.FC = () => { } const fetchMovies = async () => { try { - const res: Movie[] = await api.getAllMovies(currentPage); + const res: Movie[] = await getAllMovies(currentPage); setMovies(res); setBackdrop( `https://image.tmdb.org/t/p/original${res[2].backdrop_path}`, @@ -39,7 +38,7 @@ const HomeModule: React.FC = () => { } const fetchMovies = async () => { try { - const res: Movie[] = await api.getMoviesWithQuery(query, currentPage); + const res: Movie[] = await getMoviesWithQuery(query, currentPage); setMovies(res); } catch (e) { console.log('failed to fetch the movies');