-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update faunctional component in DispplayQuote.js
- Loading branch information
1 parent
8a9d21d
commit e1cb67a
Showing
2 changed files
with
36 additions
and
51 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 |
---|---|---|
@@ -1,65 +1,50 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import '../styles/display-quote.css'; | ||
|
||
const uniqueID = 'Ixgm2cYnlo7wGSvmiMQCVvfUJ6kVeTvgNZShaXEC'; | ||
|
||
function Quote() { | ||
const [quoteText, setQuoteText] = useState(''); | ||
const [authorText, setAuthor] = useState(''); | ||
const [categoryText, setCategory] = useState(''); | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [hasError, setHasError] = useState(false); | ||
const JokeComponent = () => { | ||
const [joke, setJoke] = useState(null); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(false); | ||
|
||
useEffect(() => { | ||
const fetchQuote = async () => { | ||
setIsLoading(true); | ||
const myAPIKey = 'Ixgm2cYnlo7wGSvmiMQCVvfUJ6kVeTvgNZShaXEC'; | ||
const apiURL = 'https://api.api-ninjas.com/v1/jokes? limit=1'; | ||
|
||
const options = { | ||
method: 'GET', | ||
headers: { 'X-Api-Key': myAPIKey }, | ||
}; | ||
|
||
const fetchData = async () => { | ||
try { | ||
const response = await fetch('https://api.api-ninjas.com/v1/quotes', { | ||
headers: { | ||
'X-Api-Key': uniqueID, | ||
}, | ||
}); | ||
if (!response.ok) { | ||
throw new Error( | ||
`API request failed with status code ${response.status}.`, | ||
); | ||
} | ||
const response = await fetch(apiURL, options); | ||
const data = await response.json(); | ||
setQuoteText(data[0].quote); | ||
setAuthor(data[0].author); | ||
setCategory(data[0].category); | ||
setJoke(data[0].joke); | ||
} catch (error) { | ||
setHasError(true); | ||
setError(true); | ||
} | ||
setIsLoading(false); | ||
setLoading(false); | ||
}; | ||
fetchQuote(); | ||
}, []); // Empty dependency array to run only once on initial component mount | ||
|
||
fetchData(); // Call the function to fetch data when the component mounts | ||
}, []); | ||
return ( | ||
<div id="display-quote"> | ||
<h4>Quotes of the Day:</h4> | ||
{isLoading && <p className="loading">Loading Quote...</p>} | ||
{hasError && <p className="error">Something wrong !</p>} | ||
|
||
{quoteText && ( | ||
<p className="quote"> | ||
" | ||
{quoteText} | ||
" | ||
</p> | ||
<div> | ||
{loading && <p>Loading...</p>} | ||
{error && ( | ||
<div> | ||
<p>Error:</p> | ||
{/* <p>{error}</p> */} | ||
</div> | ||
)} | ||
<hr /> | ||
<br /> | ||
{authorText && ( | ||
<p className="author-category"> | ||
{authorText} | ||
, | ||
{categoryText} | ||
</p> | ||
{joke && ( | ||
<div id="display-joke"> | ||
<h4>Joke of the Day:</h4> | ||
<p>{joke}</p> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export default Quote; | ||
export default JokeComponent; |