Last active
July 17, 2021 00:30
-
-
Save thusmiley/79632e40a61f3fd863675875d22c5cc9 to your computer and use it in GitHub Desktop.
CodePen Home Build a Random Quote Machine - FCC (Solution)
This file contains hidden or 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
| //https://codepen.io/thudang211/full/ZEKYwPR | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <script | |
| crossorigin | |
| src="https://unpkg.com/react@17/umd/react.development.js" | |
| ></script> | |
| <script | |
| crossorigin | |
| src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" | |
| ></script> | |
| <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script> | |
| <link | |
| href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" | |
| rel="stylesheet" | |
| integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" | |
| crossorigin="anonymous" | |
| /> | |
| <link | |
| rel="stylesheet" | |
| href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" | |
| integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" | |
| crossorigin="anonymous" | |
| /> | |
| <link rel="stylesheet" href="style.css" /> | |
| <title>Random Quote Machine</title> | |
| </head> | |
| <body> | |
| <div id="app"></div> | |
| <script type="text/babel" src="./index.js"></script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| function App() { | |
| const [quotes, setQuotes] = React.useState([]); | |
| const [randomQuote, setRandomQuote] = React.useState(""); | |
| const [color, setColor] = React.useState("#16a085"); | |
| React.useEffect(() => { | |
| async function fetchData() { | |
| const response = await fetch("https://type.fit/api/quotes"); | |
| const data = await response.json(); | |
| setQuotes(data); | |
| let index = Math.floor(Math.random() * data.length); | |
| setRandomQuote(data[index]); | |
| } | |
| fetchData(); | |
| }, []); | |
| const getNewQuote = () => { | |
| const colors = [ | |
| "#16a085", | |
| "#27ae60", | |
| "#2c3e50", | |
| "#f39c12", | |
| "#e74c3c", | |
| "#9b59b6", | |
| "#FB6964", | |
| "#342224", | |
| "#472E32", | |
| "#BDBB99", | |
| "#77B1A9", | |
| "#73A857", | |
| ]; | |
| let index = Math.floor(Math.random() * quotes.length); | |
| let randomColor = Math.floor(Math.random() * colors.length); | |
| setRandomQuote(quotes[index]); | |
| setColor(colors[randomColor]); | |
| }; | |
| return ( | |
| <div style={{ backgroundColor: color }}> | |
| <div className="container container-fluid"> | |
| <div className="box"> | |
| {randomQuote ? ( | |
| <div> | |
| <p className="quote-text" style={{ color: color }}> | |
| <i class="fa fa-quote-left" style={{ color: color }}></i> | |
| <span> </span> | |
| {randomQuote.text} | |
| </p> | |
| <h5 className="author" style={{ color: color }}> | |
| - {randomQuote.author || "No author"} | |
| </h5> | |
| </div> | |
| ) : ( | |
| <h5>loading</h5> | |
| )} | |
| <div className="row"> | |
| <a | |
| className="btn twitter" | |
| style={{ backgroundColor: color }} | |
| href={ | |
| "https://twitter.com/intent/tweet?hashtags=quotes&related=thusmiley&text=" + | |
| encodeURIComponent( | |
| '"' + randomQuote.text + '"' + randomQuote.author | |
| ) | |
| } | |
| target="_blank" | |
| > | |
| Tweet | |
| </a> | |
| <a | |
| className="btn tumblr" | |
| href="#" | |
| style={{ backgroundColor: color }} | |
| > | |
| Share | |
| </a> | |
| <button | |
| onClick={getNewQuote} | |
| className="btn new-quote" | |
| style={{ backgroundColor: color }} | |
| > | |
| New quote | |
| </button> | |
| </div> | |
| </div> | |
| <div className="footer">by thu smiley</div> | |
| </div> | |
| </div> | |
| ); | |
| } | |
| ReactDOM.render(<App />, document.getElementById("app")); |
This file contains hidden or 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
| @import url('https://fonts.googleapis.com/css2?family=Raleway:wght@500&display=swap'); | |
| * | |
| transition: all 1s | |
| margin: 0 | |
| padding: 0 | |
| box-sizing: border-box | |
| .btn | |
| padding: 10px 20px | |
| cursor: pointer | |
| font-size: 0.85em | |
| color: white | |
| .btn:hover | |
| transition: 1s | |
| color: white | |
| opacity: 0.6 | |
| .btn:focus, .btn:active | |
| outline: none | |
| box-shadow: none | |
| .footer | |
| color: #fff | |
| font-size: 0.75em | |
| text-align: center | |
| margin-top: 15px | |
| .container | |
| font-family: 'Raleway', sans-serif | |
| font-weight: bold | |
| display: flex | |
| flex-direction: column | |
| justify-content: center | |
| align-items: center | |
| position: relative | |
| height: 100vh | |
| .box | |
| background-color: #fff | |
| padding: 40px 50px | |
| border-radius: 3px | |
| width: 650px | |
| .quote-text | |
| font-size: 1.75em | |
| text-align: center | |
| .author | |
| text-align: right | |
| font-size: 1em | |
| .row | |
| display: block | |
| margin-top: 20px | |
| .twitter, .tumblr | |
| float: left | |
| width: 80px | |
| margin-right: 5px | |
| .new-quote | |
| float: right | |
| width: 120px | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment