|
1 | 1 | import React, { useEffect, useState } from "react"; |
2 | 2 | import "./photoOfTheDay.scss"; |
3 | 3 |
|
| 4 | +export default function PhotoOfTheDay() { |
| 5 | + const [photo, setPhoto] = useState<string>(); |
| 6 | + const [title, setTitle] = useState<string>(); |
| 7 | + const [explanation, setExplanation] = useState<string>(); |
4 | 8 |
|
5 | | -export default function PhotoOfTheDay () { |
| 9 | + useEffect(() => { |
| 10 | + const photoAddress = |
| 11 | + "https://api.nasa.gov/planetary/apod?api_key=5s0C1UhCZLh3WVVN2kGzF1kEw76CImpsBBf3AvEy"; |
| 12 | + const fallBackImageDates = [ |
| 13 | + "2025-05-08", |
| 14 | + "2025-05-13", |
| 15 | + "2025-04-24", |
| 16 | + "2025-03-26", |
| 17 | + "2025-04-13", |
| 18 | + "2025-03-23", |
| 19 | + "2025-01-15", |
| 20 | + "2024-11-10", |
| 21 | + "2024-03-22", |
| 22 | + "2023-08-10", |
| 23 | + ]; |
6 | 24 |
|
7 | | - const [photo, setPhoto] = useState<string>() |
8 | | - const [title, setTitle] = useState<string>() |
9 | | - const [explanation,setExplanation] = useState<string>() |
10 | | - |
11 | | - useEffect (() => { |
12 | | - const photoAddress = "https://api.nasa.gov/planetary/apod?api_key=5s0C1UhCZLh3WVVN2kGzF1kEw76CImpsBBf3AvEy" |
13 | | - const fallBackImageDates = ["2025-05-08" , "2025-05-13", "2025-04-24" , "2025-03-26" , "2025-04-13" , "2025-03-23" , "2025-01-15" , "2024-11-10" , "2024-03-22" , "2023-08-10"] |
14 | | - |
15 | | - fetch(photoAddress) |
16 | | - .then(response => response.json()) |
17 | | - .then( data => { |
18 | | - if(data.hdurl) { |
19 | | - setPhoto(data.hdurl) |
20 | | - setTitle(data.title) |
21 | | - setExplanation(data.explanation) |
22 | | - } |
23 | | - else { |
24 | | - const randomFromZeroToNine = Math.floor(Math.random() * 10); |
25 | | - const altPhotoAddress = `https://api.nasa.gov/planetary/apod?date=${fallBackImageDates[randomFromZeroToNine]}&api_key=5s0C1UhCZLh3WVVN2kGzF1kEw76CImpsBBf3AvEy` |
26 | | - |
27 | | - fetch(altPhotoAddress) |
28 | | - .then(response => response.json()) |
29 | | - .then(data => { |
30 | | - setPhoto(data.hdurl) |
31 | | - setTitle(data.title) |
32 | | - setExplanation(data.explanation) |
33 | | - }) |
34 | | - .catch(error => console.error("Error fetching api", error)) |
35 | | - } |
| 25 | + fetch(photoAddress) |
| 26 | + .then((response) => response.json()) |
| 27 | + .then((data) => { |
| 28 | + if (data.hdurl) { |
| 29 | + setPhoto(data.hdurl); |
| 30 | + setTitle(data.title); |
| 31 | + setExplanation(data.explanation); |
| 32 | + } else { |
| 33 | + const randomFromZeroToNine = Math.floor(Math.random() * 10); |
| 34 | + const altPhotoAddress = `https://api.nasa.gov/planetary/apod?date=${fallBackImageDates[randomFromZeroToNine]}&api_key=5s0C1UhCZLh3WVVN2kGzF1kEw76CImpsBBf3AvEy`; |
| 35 | + |
| 36 | + fetch(altPhotoAddress) |
| 37 | + .then((response) => response.json()) |
| 38 | + .then((data) => { |
| 39 | + setPhoto(data.hdurl); |
| 40 | + setTitle(data.title); |
| 41 | + setExplanation(data.explanation); |
36 | 42 | }) |
37 | | - .catch(error => console.error("Error fetching api", error)) |
38 | | - },[]); |
39 | | - |
40 | | -return ( |
41 | | -<div> <link href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,100..900;1,100..900&display=swap" rel="stylesheet"></link> |
42 | | - <div className="photoOfTheDayContainer"> |
43 | | - <img src={photo} className="photo-of-the-day" width="100%" alt={title}/> |
44 | | - |
45 | | - </div> |
46 | | - <div className="photoTitle">Astronomy Photo of the Day</div> |
47 | | - <div className="explanation"> |
48 | | - {explanation} |
49 | | - </div> |
50 | | -</div> |
51 | | -) |
| 43 | + .catch((error) => console.error("Error fetching api", error)); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch((error) => console.error("Error fetching api", error)); |
| 47 | + }, []); |
52 | 48 |
|
| 49 | + return ( |
| 50 | + <div> |
| 51 | + {" "} |
| 52 | + <link |
| 53 | + href="https://fonts.googleapis.com/css2?family=Public+Sans:ital,wght@0,100..900;1,100..900&display=swap" |
| 54 | + rel="stylesheet" |
| 55 | + ></link> |
| 56 | + <div className="photoOfTheDayContainer"> |
| 57 | + <img |
| 58 | + src={photo} |
| 59 | + className="photo-of-the-day" |
| 60 | + width="100%" |
| 61 | + alt={title} |
| 62 | + /> |
| 63 | + </div> |
| 64 | + <div className="photoTitle">Astronomy Photo of the Day</div> |
| 65 | + <div className="explanation">{explanation}</div> |
| 66 | + </div> |
| 67 | + ); |
53 | 68 | } |
0 commit comments