-
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 #25 from techswitch-learners/mm-104-selection-butt…
…ons--and-display-image-preview Mm 104 selection buttons and display image preview
- Loading branch information
Showing
6 changed files
with
103 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useState } from 'react'; | ||
|
||
function BirthdayForm() { | ||
const [birthday, setBirthday] = useState(''); | ||
|
||
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
getBirthday(birthday); | ||
}; | ||
|
||
const getBirthday = (date: string) => { | ||
console.log("Birthday:", date); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit}> | ||
<label>Birthday</label> | ||
<input | ||
type="date" | ||
id="birthday" | ||
value={birthday} | ||
onChange={(e) => setBirthday(e.target.value)} | ||
/> | ||
<button type="submit">Submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
export default BirthdayForm; |
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,67 @@ | ||
import { useState } from "react"; | ||
import { PictureOfDay } from "./DisplayPicture"; | ||
import { fetchAPI, fetchRandomAPI } from "../utils/fetchData"; | ||
|
||
export function PreviewImage() { | ||
|
||
const [myPreviewImage, setMyPreviewImage] = useState<PictureOfDay | null>(null); | ||
const [birthday, setBirthday] = useState(''); | ||
|
||
const handleRandomButtonClick = async () => { | ||
try { | ||
const randomPicture = await fetchRandomAPI("https://api.nasa.gov/planetary/apod?api_key="); | ||
setMyPreviewImage(randomPicture[0]); | ||
console.log(myPreviewImage); | ||
|
||
} | ||
catch (error) { | ||
console.error("Error fetching today's image:", error); | ||
} | ||
} | ||
|
||
const handleTodayButtonClick = async () => { | ||
try { | ||
const todaysPicture = await fetchAPI("https://api.nasa.gov/planetary/apod?api_key="); | ||
setMyPreviewImage(todaysPicture); | ||
} | ||
catch (error) { | ||
console.error("Error fetching today's image:", error); | ||
} | ||
} | ||
|
||
const handleBirthdayFormSubmit = async (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
try { | ||
const userBirthdayImage = await fetchAPI("https://api.nasa.gov/planetary/apod?api_key=", birthday); | ||
setMyPreviewImage(userBirthdayImage); | ||
} catch (error) { | ||
console.error("Error fetching today's image:", error); | ||
} | ||
} | ||
|
||
return ( | ||
<> | ||
<h4>CHOOSE THE IMAGE YOU WANT TO DISPLAY:</h4> | ||
<button type="button" | ||
id="todays-date-button" | ||
onClick={handleTodayButtonClick}>Today</button> | ||
|
||
<button type="button" | ||
id="random-button" | ||
onClick={handleRandomButtonClick}>Random</button> | ||
|
||
<form onSubmit={handleBirthdayFormSubmit}> | ||
<label>Birthday</label> | ||
<input | ||
type="date" | ||
id="birthday" | ||
value={birthday} | ||
onChange={(e) => setBirthday(e.target.value)} | ||
/> | ||
<button type="submit">Submit</button> | ||
</form> | ||
<img src={myPreviewImage?.url} alt={myPreviewImage?.explanation} className="preview-chosen-image" /> | ||
</> | ||
) | ||
|
||
} |
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