From 90ed15a06620342ece9d0dde4ff875b518c39fc2 Mon Sep 17 00:00:00 2001 From: iqbalpa Date: Thu, 11 Jul 2024 08:02:17 +0700 Subject: [PATCH] refactor(detail): create smaller components --- src/components/backButton/backButton.tsx | 22 ++++++++ src/components/genres/genres.tsx | 23 +++++++++ src/components/rating/rating.tsx | 18 +++++++ src/components/yearRuntime/yearRuntime.tsx | 20 ++++++++ src/constant/detailMovie.ts | 2 +- src/modules/detailMovie/detailMovie.tsx | 58 ++++++---------------- 6 files changed, 98 insertions(+), 45 deletions(-) create mode 100644 src/components/backButton/backButton.tsx create mode 100644 src/components/genres/genres.tsx create mode 100644 src/components/rating/rating.tsx create mode 100644 src/components/yearRuntime/yearRuntime.tsx diff --git a/src/components/backButton/backButton.tsx b/src/components/backButton/backButton.tsx new file mode 100644 index 0000000..5f33ddf --- /dev/null +++ b/src/components/backButton/backButton.tsx @@ -0,0 +1,22 @@ +'use client'; + +import { CircleArrowLeft } from 'lucide-react'; +import { useRouter } from 'next/navigation'; +import React from 'react'; + +const BackButton: React.FC = () => { + const router = useRouter(); + const handleBack = () => router.push('/'); + + return ( + + ); +}; + +export default BackButton; diff --git a/src/components/genres/genres.tsx b/src/components/genres/genres.tsx new file mode 100644 index 0000000..2d86bc7 --- /dev/null +++ b/src/components/genres/genres.tsx @@ -0,0 +1,23 @@ +import React from 'react'; +import { Genre } from '@/constant/detailMovie'; + +interface IGenres { + genres: Genre[]; +} + +const Genres: React.FC = ({ genres }) => { + return ( +
+ {genres.map((genre) => ( +
+

{genre.name}

+
+ ))} +
+ ); +}; + +export default Genres; diff --git a/src/components/rating/rating.tsx b/src/components/rating/rating.tsx new file mode 100644 index 0000000..869b250 --- /dev/null +++ b/src/components/rating/rating.tsx @@ -0,0 +1,18 @@ +import { Star } from 'lucide-react'; +import React from 'react'; + +const Rating: React.FC<{ rating: number }> = ({ rating }) => { + return ( +
+ +

+ + {rating.toFixed(1)} + + /10 +

+
+ ); +}; + +export default Rating; diff --git a/src/components/yearRuntime/yearRuntime.tsx b/src/components/yearRuntime/yearRuntime.tsx new file mode 100644 index 0000000..e667bd4 --- /dev/null +++ b/src/components/yearRuntime/yearRuntime.tsx @@ -0,0 +1,20 @@ +import { convertRuntime } from '@/utils/convertRuntime'; +import { Dot } from 'lucide-react'; +import React from 'react'; + +interface IYearRuntime { + release_date: string; + runtime: number; +} + +const YearRuntime: React.FC = ({ release_date, runtime }) => { + return ( +
+

{release_date.split('-')[0]}

+ +

{convertRuntime(runtime)}

+
+ ); +}; + +export default YearRuntime; diff --git a/src/constant/detailMovie.ts b/src/constant/detailMovie.ts index 2468a03..8ec543c 100644 --- a/src/constant/detailMovie.ts +++ b/src/constant/detailMovie.ts @@ -5,7 +5,7 @@ interface Collection { backdrop_path: string; } -interface Genre { +export interface Genre { id: number; name: string; } diff --git a/src/modules/detailMovie/detailMovie.tsx b/src/modules/detailMovie/detailMovie.tsx index b8c09fd..dc09c3d 100644 --- a/src/modules/detailMovie/detailMovie.tsx +++ b/src/modules/detailMovie/detailMovie.tsx @@ -6,15 +6,17 @@ import React, { useEffect, useState } from 'react'; import Image from 'next/image'; import { Skeleton } from '@/components/ui/skeleton'; import { CircleArrowLeft, Dot, Star } from 'lucide-react'; -import { useRouter } from 'next/navigation'; import { convertRuntime } from '@/utils/convertRuntime'; +import BackButton from '@/components/backButton/backButton'; +import Rating from '@/components/rating/rating'; +import Genres from '@/components/genres/genres'; +import YearRuntime from '@/components/yearRuntime/yearRuntime'; interface IDetailMovieModule { id: string; } const DetailMovieModule: React.FC = ({ id }) => { - const router = useRouter(); const [movie, setMovie] = useState(); const [isLoading, setIsLoading] = useState(true); @@ -31,24 +33,16 @@ const DetailMovieModule: React.FC = ({ id }) => { fetchMovie(); }, [id]); - const handleBack = () => router.push('/'); - return (
- + + + {/* backdrop */}
{isLoading && ( )} - - {/* backdrop */} backdrop-image = ({ id }) => { className={`z-10 transition-opacity duration-500 ${isLoading ? 'opacity-0' : 'opacity-100'}`} onLoad={() => setIsLoading(false)} /> + {movie && (
{/* poster */} @@ -69,39 +64,14 @@ const DetailMovieModule: React.FC = ({ id }) => { />
- {/* year and runtime */} -
-

- {movie.release_date.split('-')[0]} -

- -

{convertRuntime(movie.runtime)}

-
- - {/* title, overview, genres */} +

{movie.title}

{movie.overview}

-
- {movie.genres.map((genre) => ( -
-

{genre.name}

-
- ))} -
- - {/* stars */} -
- -

- - {movie.vote_average.toFixed(1)} - - /10 -

-
+ +
)}