|
1 | 1 | import { useEffect, useState } from "react"; |
2 | 2 | import axios from "axios"; |
3 | | -import "../../styles/pages/activities/RandomQuote.css"; |
| 3 | +import "../../styles/pages/activities/RandomQuote.css"; // Reuse your styles |
4 | 4 |
|
5 | 5 | export const RandomQuote = () => { |
6 | | - const [quote, setQuote] = useState(null); |
| 6 | + const [joke, setJoke] = useState(null); |
7 | 7 | const [error, setError] = useState(null); |
8 | 8 |
|
9 | | - const generateQuote = () => { |
10 | | - setQuote(null); |
11 | | - axios({ |
12 | | - method: "GET", |
13 | | - url: "https://api.qutable.io/random", |
14 | | - }) |
15 | | - .then((res) => setQuote(res.data)) |
16 | | - .catch((error) => setError(error)); |
| 9 | + const generateJoke = () => { |
| 10 | + setJoke(null); |
| 11 | + setError(null); |
| 12 | + |
| 13 | + axios |
| 14 | + .get("https://official-joke-api.appspot.com/random_joke") |
| 15 | + .then((res) => setJoke(res.data)) |
| 16 | + .catch((err) => setError(err)); |
17 | 17 | }; |
18 | 18 |
|
19 | 19 | useEffect(() => { |
20 | | - generateQuote(); |
| 20 | + generateJoke(); |
21 | 21 | }, []); |
22 | 22 |
|
23 | 23 | return ( |
24 | 24 | <div className="rquote-root"> |
25 | | - <h1 class="header">Random Quote Generator</h1> |
26 | | - <div class="description"> |
27 | | - Generate any random quote to get some inspiration! |
| 25 | + <h1 className="header">Random Joke Generator</h1> |
| 26 | + <div className="description"> |
| 27 | + Get a random joke to brighten your day 😄 |
28 | 28 | </div> |
29 | | - {quote && ( |
| 29 | + |
| 30 | + {joke && ( |
30 | 31 | <div className="rquote-content"> |
31 | | - <div className="rquote-quote">{quote.content}</div> |
32 | | - <div className="rquote-author">- {quote.author}</div> |
| 32 | + <div className="rquote-quote">{joke.setup}</div> |
| 33 | + <div className="rquote-author">{joke.punchline}</div> |
33 | 34 | </div> |
34 | 35 | )} |
| 36 | + |
35 | 37 | {error && ( |
36 | | - <div className="rquote-content error"> |
37 | | - {error.message} |
38 | | - </div> |
| 38 | + <div className="rquote-content error">{error.message}</div> |
39 | 39 | )} |
40 | | - {!quote && !error && ( |
| 40 | + |
| 41 | + {!joke && !error && ( |
41 | 42 | <div className="spinner-wrapper"> |
42 | 43 | <div className="spinner"></div> |
43 | 44 | </div> |
44 | 45 | )} |
45 | | - <button className="rquote-button" onClick={generateQuote}> |
46 | | - Generate Quote |
| 46 | + |
| 47 | + <button className="rquote-button" onClick={generateJoke}> |
| 48 | + Generate Joke |
47 | 49 | </button> |
48 | 50 | </div> |
49 | 51 | ); |
|
0 commit comments