diff --git a/src/App.jsx b/src/App.jsx index 58b51bb..671dbdc 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -7,7 +7,8 @@ import Home from "@/pages/Home"; import NotFound from "@/pages/404"; import Accordion from "@/pages/accordion/accordion"; import RandomColor from "@/pages/RandomColor"; -import StarRating from "@/pages/starRating"; +import StarRating from "@/pages/StarRating"; +import ImageSlider from "@/pages/ImageSlider" function App() { return ( @@ -22,6 +23,8 @@ function App() { } /> {/* Star Rating */} } /> + {/* image Slider */} + } /> {/* Error Page */} } /> diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index 32e3a8f..b47c586 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -8,6 +8,7 @@ function Home() { accordion random Color Generator star rating + image slider ); } diff --git a/src/pages/ImageSlider.jsx b/src/pages/ImageSlider.jsx new file mode 100644 index 0000000..be9dc49 --- /dev/null +++ b/src/pages/ImageSlider.jsx @@ -0,0 +1,98 @@ +import { useEffect, useState } from "react"; +import PropTypes from "prop-types"; +import { BsArrowLeftCircleFill, BsArrowRightCircleFill } from "react-icons/bs"; + +function ImageSlider({ + url = "https://meme-api.com/gimme", + limit = 1, + subReddit = "programmerHumor", +}) { + const [images, setImages] = useState([]); + const [currentSlide, setCurrentSlide] = useState(0); + const [errorMsg, setErrorMsg] = useState(null); + const [loading, setLoading] = useState(false); + + async function fetchImages(getUrl) { + try { + setLoading(true); + + const response = await fetch(`${getUrl}/${subReddit}/${limit}`); + const data = await response.json(); + + if (data) { + setImages(data.memes); + setLoading(false); + } + } catch (e) { + setErrorMsg(e.message); + setLoading(false); + } + } + + useEffect(() => { + if (url !== "") fetchImages(url); + }, [url]); + + if (loading) { + return
Loading data ! please wait
; + } + if (errorMsg !== null) { + return
Error occured ! {errorMsg}
; + } + + function handlePrevious() { + setCurrentSlide(currentSlide === 0 ? images.length - 1 : currentSlide - 1); + } + + function handleNext() { + setCurrentSlide(currentSlide === images.length - 1 ? 0 : currentSlide + 1); + } + + return ( +
+ + {images && images.length + ? images.map((imageItem, index) => ( + {imageItem.title} + )) + : null} + + + {images && images.length + ? images.map((_, index) => ( + + )) + : null} + +
+ ); +} + +ImageSlider.propTypes = { + url: PropTypes.string, + limit: PropTypes.number, + subReddit: PropTypes.string, +}; + +export default ImageSlider; diff --git a/src/pages/starRating.jsx b/src/pages/StarRating.jsx similarity index 95% rename from src/pages/starRating.jsx rename to src/pages/StarRating.jsx index 57d8902..bd0386e 100644 --- a/src/pages/starRating.jsx +++ b/src/pages/StarRating.jsx @@ -1,6 +1,6 @@ import PropTypes from "prop-types"; import { useState } from "react"; -import { FaStar } from "react-icons/fa"; // Correct import for FaStar +import { FaStar } from "react-icons/fa"; import { Input } from "@/components/ui/input"; function StarRating({ numOfStars = 5 }) {