|
| 1 | +// src/components/MovieSwiper.jsx |
| 2 | +import { useEffect, useState } from "react"; |
| 3 | +import { Swiper, SwiperSlide } from "swiper/react"; |
| 4 | +import { Navigation, Pagination, Scrollbar, A11y } from "swiper/modules"; |
| 5 | +import { Link } from "react-router-dom"; |
| 6 | +import "./MovieSwiper.css"; |
| 7 | + |
| 8 | +import "swiper/css"; |
| 9 | +import "swiper/css/navigation"; |
| 10 | +import "swiper/css/pagination"; |
| 11 | +import "swiper/css/scrollbar"; |
| 12 | + |
| 13 | +const API_TOKEN = import.meta.env.VITE_TMDB_API_TOKEN; |
| 14 | + |
| 15 | +const MovieSwiper = () => { |
| 16 | + const [movies, setMovies] = useState([]); |
| 17 | + |
| 18 | + const fetchTopRated = async () => { |
| 19 | + const res = await fetch( |
| 20 | + "https://api.themoviedb.org/3/movie/top_rated?language=ko-KR", |
| 21 | + { |
| 22 | + headers: { |
| 23 | + accept: "application/json", |
| 24 | + Authorization: `Bearer ${API_TOKEN}`, |
| 25 | + }, |
| 26 | + } |
| 27 | + ); |
| 28 | + |
| 29 | + const data = await res.json(); |
| 30 | + setMovies(data.results || []); |
| 31 | + }; |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + fetchTopRated(); |
| 35 | + }, []); |
| 36 | + |
| 37 | + return ( |
| 38 | + <div className="top-rated-section"> |
| 39 | + <h2 className="top-rated-title">Top Rated Movies ⭐</h2> |
| 40 | + |
| 41 | + <Swiper |
| 42 | + modules={[Navigation, Pagination, Scrollbar, A11y]} |
| 43 | + spaceBetween={10} // 기존 20 → 10 감소 |
| 44 | + slidesPerView={6} // 한 줄 더 꽉 채워보임 |
| 45 | + navigation |
| 46 | + pagination={{ clickable: true }} |
| 47 | + scrollbar={{ draggable: true }} |
| 48 | + autoHeight={false} |
| 49 | + style={{ height: "360px" }} |
| 50 | + > |
| 51 | + {movies.map((movie) => ( |
| 52 | + <SwiperSlide key={movie.id}> |
| 53 | + <Link to={`/detail/${movie.id}`}> |
| 54 | + <div className="swiper-movie-card"> |
| 55 | + <img |
| 56 | + src={`https://image.tmdb.org/t/p/w500${movie.poster_path}`} |
| 57 | + alt={movie.title} |
| 58 | + /> |
| 59 | + <p className="s-title">{movie.title}</p> |
| 60 | + <p className="s-rating">⭐ {movie.vote_average.toFixed(2)}</p> |
| 61 | + </div> |
| 62 | + </Link> |
| 63 | + </SwiperSlide> |
| 64 | + ))} |
| 65 | + </Swiper> |
| 66 | + </div> |
| 67 | + ); |
| 68 | +}; |
| 69 | + |
| 70 | +export default MovieSwiper; |
0 commit comments