Skip to content

Commit

Permalink
Adding API for home page
Browse files Browse the repository at this point in the history
  • Loading branch information
IsabellaSulisufi committed Oct 21, 2024
1 parent 0a6cf81 commit 534264c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
Binary file added main/public/assets/defaultCat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 32 additions & 15 deletions main/src/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import React from "react";
import React, { useEffect, useState } from "react";
import "./styling/Home.css";
import Navbar from "./components/Navbar.jsx";
import HomeCards from "./components/HomeCards.jsx";
import Footer from "./components/Footer.jsx";
import Axios from "axios";

export default function Home() {
const [catData, setCatData] = useState([]);

// Move these constants outside of the useEffect
const baseURL = "https://api.thecatapi.com/v1/images/search"; // Base URL
const apikey = "live_YUQtu7qoUFuzzmPawdXulxfge0eTVlkGdxaLgaI5m0vcROtnGDxSNkSrs3kVvuy3"; // Your API key
const limit = 10; // Limit for the number of images

// Constructed API URL with query parameters
const apiUrl = `${baseURL}?limit=${limit}&api_key=${apikey}`;

useEffect(() => {
Axios.get(apiUrl)
.then((res) => setCatData(res.data)) // Store the array of cat images
.catch((error) => console.log(error));
}, [apiUrl]); // Adding apiUrl as a dependency

// Extract URLs for the first two images, if available
const catImages = catData.slice(0, 2).map(cat => cat.url);

return (
<React.Fragment>
<Navbar bgColor="#F5F5F5" borderColor="#6BA8A9"/>
<Navbar bgColor="#F5F5F5" borderColor="#6BA8A9" />

<div className="homeContainer">
<div className="leftContainer">
Expand All @@ -23,7 +43,7 @@ export default function Home() {
<img
className="searchIcon"
src="./main/public/assets/searchIcon.png"
alt=""
alt="Search Icon"
/>
</a>
</div>
Expand All @@ -32,35 +52,32 @@ export default function Home() {
<div className="globeIconContainer">
<img
className="globeIcon"
src="./main/public/assets/globeIcon.png" /* Add the correct path to your globe icon */
src="./main/public/assets/globeIcon.png"
alt="Globe Icon"
/>
</div>

<div className="homeCardSection">
<div className="topCard">
<HomeCards
imgSrc="./main/public/assets/apples.jpg"
title="Apples"
text="Apples are a popular and nutritious fruit from the apple tree known for their crisp texture and sweet or tart flavor."
imgSrc={catImages[0] || "./main/public/assets/defaultCat.jpg"}
title="Cat 1" // Soon will be replaced with the actual cat name and lead to cat's Wikipedia page
text="A cute cat image fetched from the Cat API."
link="#"
/>{" "}
/>
</div>

<div className="bottomCard">
<HomeCards
imgSrc="./main/public/assets/apples.jpg"
title="Apples"
text="Apples are a popular and nutritious fruit from the apple tree known for their crisp texture and sweet or tart flavor."
imgSrc={catImages[1] || "./main/public/assets/defaultCat.jpg"}
title="Cat 2" // Soon will be replaced with the actual cat name and lead to cat's Wikipedia page
text="Another cute cat image fetched from the Cat API."
link="#"
/>{" "}
/>
</div>
</div>


</div>
<Footer bgColor="#4F8787" />

</React.Fragment>
);
}
84 changes: 84 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"author": "Isabella Sulisufi <i.sulisufi@gmail.com>",
"license": "ISC",
"dependencies": {
"axios": "1.7.7",
"body-parser": "1.20.3",
"dotenv": "16.4.5",
"ejs": "3.1.10",
"express": "4.21.0",
"pg": "8.12.0",
Expand Down

0 comments on commit 534264c

Please sign in to comment.