Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mm102 homepage background image #14

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 />} />
claudiaGiancola marked this conversation as resolved.
Show resolved Hide resolved
<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;

};
Loading