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

Mm 006 creation api functions #10

Merged
merged 19 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
4 changes: 0 additions & 4 deletions .env.template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you accidentally deleted this file - please can you recreate it - as it's needed for the codebase

This file was deleted.

10 changes: 9 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import React from 'react';
import {BrowserRouter as Router, Routes, Route} from 'react-router-dom';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import './App.scss';
import Home from './Home/Home';
// line below is just an example of what to import to use the DisplayPictureOfDay function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove these comments and the the before merging - they might be confusing for some people

// import { DisplayPictureOfDay } from './images/Fetch
import { Quiz } from './Quiz/Quiz';
import Footer from './Footer/Footer';

function App() {
return (
<div className="App">

{/* line below is just an example of what to import to use the DisplayPictureOfDay function */}
{/* <DisplayPictureOfDay /> */}

<Router>
<h1>MarsioKart</h1>
{/* line below is just an example of what to import to use the DisplayPictureOfDay function */}
{/* <DisplayPictureOfDay /> */}
<Routes>
<Route path='/'
element={<Home/>}/>
Expand Down
68 changes: 68 additions & 0 deletions src/images/DisplayPicture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React, { useState, useEffect } from 'react';
import { fetchAPI } from '../utils/fetchData';

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

export const DisplayPictureOfDay = () => {
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=");

// test picture data for when the API requests have reached their limit:
Copy link
Contributor

@Kiran-R-K Kiran-R-K Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this from this PR as it's not for production.

// const pictureData = {
// "date": "2024-07-30",
// "explanation": "To some, it looks like a penguin. But to people who study the universe, it is an interesting example of two big galaxies interacting. Just a few hundred million years ago, the upper NGC 2936 was likely a normal spiral galaxy: spinning, creating stars, and minding its own business. Then it got too close to the massive elliptical galaxy NGC 2937, below, and took a dive. Together known as Arp 142, they are featured in this new Webb infrared image, while a visible light Hubble image appears in comparison. NGC 2936 is not only being deflected, but distorted, by this close gravitational interaction. When massive galaxies pass near each other, gas is typically condensed from which new stars form. A young group of stars appears as the nose of the penguin toward the right of the upper galaxy, while in the center of the spiral, bright stars together appear as an eye. Before a billion years, the two galaxies will likely merge into one larger galaxy. Explore Your Universe: Random APOD Generator",
// "hdurl": "https://apod.nasa.gov/apod/image/2407/Arp142_Webb_1487.jpg",
// "media_type": "image",
// "service_version": "v1",
// "title": "Arp 142: Interacting Galaxies from Webb",
// "url": "https://apod.nasa.gov/apod/image/2407/Arp142_Webb_960.jpg"
// }
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;

// example of how to pick specific properties
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove these comments from this review for clarity

// return (
// <>
// <h3>{myPictureData.title}</h3>
// <p>{myPictureData.date}</p>
// <p>{myPictureData.explanation}</p>
// <img src={myPictureData.url} />
// <img src={myPictureData.hdurl} />
// </>
// )

};
20 changes: 20 additions & 0 deletions src/utils/fetchData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 1. .env has to be created in root
// 2. inside .env, it should be REACT_APP_NASA_API_KEY="<your api key>"

export async function fetchAPI(apiUrl: string) {
const apiKey = process.env.REACT_APP_NASA_API_KEY;
const link = `${apiUrl}${apiKey}`;

try {
const response = await fetch(link);
if (!response.ok) {
throw new Error('Network response was not ok');
}

const pictureData = await response.json();
console.log(pictureData);
return pictureData;
} catch (err) {
throw err;
}
}
Loading