Skip to content

Commit

Permalink
Merge pull request #14 from techswitch-learners/mm102-homepage-backgr…
Browse files Browse the repository at this point in the history
…ound-image

Mm102 homepage background image
  • Loading branch information
claudiaGiancola authored Aug 1, 2024
2 parents 1dd03b1 + e1dc0f3 commit e218254
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ $default-font: "Segoe UI";

a {
text-decoration: none;
}

.home-content {
color: $text-color;
width: 100vw;
height: 100vh;
}
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function App() {
<Header/>
<Routes>
<Route path='/'
element={<Home/>}/>
element={<Home />} />
<Route path='/quiz'
element={<Quiz/>}/>
element={<Quiz />} />
</Routes>
<Footer/>
<Footer />
</Router>
</div>
);
Expand Down
34 changes: 21 additions & 13 deletions src/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
import { useState } from "react";
import React, { useState } from "react";
import DisplayBackgroundImage from '../images/DisplayBackgroundImage';

function Home() {

const [username, setUsername] = useState("");
const [submitStatus, setSubmitStatus] = useState(false);
const [username, setUsername] = useState("");
const [submitStatus, setSubmitStatus] = useState(false);

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setSubmitStatus(true);
}

const homeBackgroundImage = DisplayBackgroundImage();

return (
<>
<form onSubmit={handleSubmit}>
<label>Enter name:
<input type="text"
value={username}
onChange={username => setUsername(username.target.value)}
/>
</label>
<button type="submit">Submit</button>
{submitStatus ? <p>Welcome {username}!</p> : null }
</form>
<main className="home-content" style={{ backgroundImage: `url(${homeBackgroundImage})` }}>

{/* Other components and content */}
<form onSubmit={handleSubmit}>
<label>Enter name:
<input type="text"
value={username}
onChange={username => setUsername(username.target.value)}
/>
</label>
<button type="submit">Submit</button>
{submitStatus ? <p>Welcome {username}!</p> : null}
</form>

</main>
</>
)
}
Expand Down
50 changes: 50 additions & 0 deletions src/images/DisplayBackgroundImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState, useEffect } from 'react';
import { fetchAPI } from '../utils/fetchData';

interface PictureOfDay {
date: string,
explanation: string,
hdurl: string,
title: string,
url: string
}

const DisplayBackgroundImage = () => {
const [myPictureData, setMyPictureData] = useState<PictureOfDay | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);

const FetchPicture = async () => {

try {
setIsLoading(true);

const pictureData = await fetchAPI("https://api.nasa.gov/planetary/apod?api_key=");

setMyPictureData(pictureData);
setIsLoading(false);

} catch (err: unknown) {
if (err instanceof Error) {
setError(err); // Set the actual Error object
} else {
setError(new Error('An unknown error occurred')); // Provide a default Error object
}

setIsLoading(false);
}
}
useEffect(() => {
FetchPicture();
}, [])

if (isLoading) return (<div>Is Loading...</div>);
if (error) return (<div>{error.message}</div>);
if (!myPictureData) return (<div>EMPTY</div>);


return myPictureData.hdurl;

};

export default DisplayBackgroundImage;
1 change: 1 addition & 0 deletions src/images/DisplayPicture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ export const DisplayPictureOfDay = () => {
if (!myPictureData) return (<div>EMPTY</div>);

return myPictureData;

};

0 comments on commit e218254

Please sign in to comment.