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 all 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
7 changes: 4 additions & 3 deletions .env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# A template for the file that would store secrets (e.g. environment variables)
# copy this to your .env file and add your own API key etc
# copy this file and rename it as .env
# this needs to be in youre root directory
# replace <YOUR API KEY> with your api key

NASA-API-KEY=add-your-key-here
REACT_APP_NASA_API_KEY="<YOUR API KEY>"
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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';
import Header from './header';
Expand Down
46 changes: 46 additions & 0 deletions src/images/DisplayPicture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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=");

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;
};
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