-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from techswitch-learners/mm102-homepage-backgr…
…ound-image Mm102 homepage background image
- Loading branch information
Showing
5 changed files
with
81 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters