File size: 4,635 Bytes
f5071ca |
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
import React, { useRef } from "react";
import {
BsFillArrowLeftCircleFill,
BsFillArrowRightCircleFill,
} from "react-icons/bs";
import { useNavigate } from "react-router-dom";
import { useSelector } from "react-redux";
import dayjs from "dayjs";
import ContentWrapper from "../contentWrapper/ContentWrapper";
import Img from "../lazyLoadImage/Img";
import PosterFallback from "../../assets/no-poster.png";
import CircleRating from "../circleRating/CircleRating";
import Genres from "../genres/Genres";
import "./style.scss";
const Carousel = ({ data, loading, endpoint, title }) => {
const carouselContainer = useRef();
const { url } = useSelector((state) => state.home);
const navigate = useNavigate();
const navigation = (dir) => {
const container = carouselContainer.current;
const scrollAmount =
dir === "left"
? container.scrollLeft - (container.offsetWidth + 20)
: container.scrollLeft + (container.offsetWidth + 20);
container.scrollTo({
left: scrollAmount,
behavior: "smooth",
});
};
const skItem = () => {
return (
<div className="skeletonItem">
<div className="posterBlock skeleton"></div>
<div className="textBlock">
<div className="title skeleton"></div>
<div className="date skeleton"></div>
</div>
</div>
);
};
return (
<div className="carousel">
<ContentWrapper>
{title && <div className="carouselTitle">{title}</div>}
<BsFillArrowLeftCircleFill
className="carouselLeftNav arrow"
onClick={() => navigation("left")}
/>
<BsFillArrowRightCircleFill
className="carouselRighttNav arrow"
onClick={() => navigation("right")}
/>
{!loading ? (
<div className="carouselItems" ref={carouselContainer}>
{data?.map((item) => {
const posterUrl = item.poster_path
? url.poster + item.poster_path
: PosterFallback;
return (
<div
key={item.id}
className="carouselItem"
onClick={() =>
navigate(
`/${item.media_type || endpoint}/${
item.id
}`
)
}
>
<div className="posterBlock">
<Img src={posterUrl} />
<CircleRating
rating={item.vote_average.toFixed(
1
)}
/>
<Genres
data={item.genre_ids.slice(0, 2)}
/>
</div>
<div className="textBlock">
<span className="title">
{item.title || item.name}
</span>
<span className="date">
{dayjs(item.release_date || item.first_air_date).format(
"MMM D, YYYY"
)}
</span>
</div>
</div>
);
})}
</div>
) : (
<div className="loadingSkeleton">
{skItem()}
{skItem()}
{skItem()}
{skItem()}
{skItem()}
</div>
)}
</ContentWrapper>
</div>
);
};
export default Carousel;
|