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

Math magicians: fetch data from API #4

Merged
merged 5 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
12 changes: 10 additions & 2 deletions src/App.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
* {
margin: auto;
margin: 0;
}

#logo {
margin: 3%;
margin-left: 3%;
margin-top: 3%;
width: 4%;
}

.App {
display: flex;
justify-content: space-between;
align-items: center;
padding: 3%;
}
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import './App.css';
import Calculator from './components/Calculator'; // Import the Calculator component
import JokeComponent from './components/DisplayQuote';

function App() {
return (
<div className="App">
{/* Quotes are Displayed here */}
<JokeComponent />
{/* Add the Calculator component here */}
<Calculator />
</div>
Expand Down
50 changes: 50 additions & 0 deletions src/components/DisplayQuote.js
Copy link

Choose a reason for hiding this comment

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

  • [Optional] When I refresh the browser two quotes display one after the other, it is supposed to be one quote per refresh. Please resolve that.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState, useEffect } from 'react';
import '../styles/display-quote.css';

const JokeComponent = () => {
const [joke, setJoke] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);

useEffect(() => {
const myAPIKey = 'Ixgm2cYnlo7wGSvmiMQCVvfUJ6kVeTvgNZShaXEC';
const apiURL = 'https://api.api-ninjas.com/v1/dadjokes?limit=1';

const options = {
method: 'GET',
headers: { 'X-Api-Key': myAPIKey },
};

const fetchData = async () => {
try {
const response = await fetch(apiURL, options);
const data = await response.json();
setJoke(data[0].joke);
} catch (error) {
setError(true);
}
setLoading(false);
};

fetchData(); // Call the function to fetch data when the component mounts

Choose a reason for hiding this comment

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

  • I suggest that you change a function to Quote not Joke as the requirement says
  • Please change the API from Joke to Quote you can refer to this link

}, []);
return (
<div>
{loading && <p>Loading...</p>}
{error && (
<div>
<p>Error:</p>
{/* <p>{error}</p> */}
</div>
)}
{joke && (
<div id="display-joke">
<h4>Joke of the Day:</h4>
<p>{joke}</p>
</div>
)}
</div>
);
};

export default JokeComponent;
4 changes: 2 additions & 2 deletions src/styles/calculator.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.calculator-wrapper {
margin: 3%;
padding: 10%;
margin-right: 5%;
padding: 1%;
display: flex;
justify-content: center;
}
Expand Down
17 changes: 17 additions & 0 deletions src/styles/display-quote.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#display-joke {
display: flex;
flex-direction: column;
justify-content: space-between;
}

#display-joke h4 {
color: #fff;
font-family: cursive;
}

#display-joke p {
color: #004;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
margin-top: 1.5%;
}