Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 9aef9f0

Browse files
committed
Created an input bar and a search button to display quotes with specific keywords
1 parent 68b1a9b commit 9aef9f0

5 files changed

Lines changed: 48 additions & 8 deletions

File tree

quotes-react-app/src/App.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import {useState, useEffect} from "react";
22
import './App.css';
33
import Button from "./component/Button";
4+
import Input from "./component/Input";
5+
import Quote from "./component/Quote";
46

57
function App() {
68
const [quote, setQuote] = useState(
79
{
8-
quote: "loading...",
9-
author: "loading..."
10+
quote: "Loading quote...",
11+
author: "Loading quote's author..."
1012
}
1113
);
1214

15+
const [keyword, setKeyword] = useState("");
16+
17+
const [searchQuotes, setSearchQuotes] = useState([]);
18+
19+
20+
21+
1322
useEffect(getRandomQuote, []);
1423

1524
function getRandomQuote() {
@@ -18,13 +27,24 @@ function App() {
1827
.then(data => setQuote(data));
1928
}
2029

30+
const searchQuote = () => {
31+
fetch(`https://afsha1-quote-server.glitch.me/quotes/search?term=${keyword}`)
32+
.then((res) => res.json())
33+
.then((data) => setSearchQuotes(data));
34+
console.log(searchQuotes);
35+
};
36+
2137
return (
2238
<div className="App">
2339
<header className="App-header">
24-
<h1>{quote.quote}</h1>
25-
<h2>{quote.author}</h2>
26-
<Button getRandomQuote={getRandomQuote} />
40+
<Quote quote={quote} />
41+
<Button onClickHandler={getRandomQuote} label="Get Random Quote" />
42+
<Input className="searchInput" type="text" onChangeHandler={setKeyword} placeholder="Search"/>
43+
<Button onClickHandler={searchQuote} label="Search Quote"/>
2744
</header>
45+
<>
46+
{searchQuotes.map((searchQuote, index) => (<Quote key={index} quote={searchQuote} />))}
47+
</>
2848
</div>
2949
);
3050
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22

3-
const Button = ({getRandomQuote}) => {
4-
return <button onClick={getRandomQuote} >Get Random Quote</button>
3+
const Button = ({onClickHandler, label}) => {
4+
return <button onClick={onClickHandler} >{label}</button>
55
}
66

77
export default Button;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
3+
const Input = ({onChangeHandler, placeholder}) => {
4+
return <input onChange={e => onChangeHandler(e.target.value)} placeholder={placeholder} />;
5+
}
6+
7+
export default Input;
8+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
3+
const Quote = ({ quote }) => {
4+
return (
5+
<div>
6+
<h1>" {quote.quote} "</h1>
7+
<h2>{quote.author}</h2>
8+
</div>
9+
);
10+
};
11+
12+
export default Quote;

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ function pickFromArray(myArray) {
5959
}
6060

6161
//Start our server so that it listens for HTTP requests!
62-
const listener = app.listen(3000, function () {
62+
const listener = app.listen(3001, function () {
6363
console.log("Your app is listening on port " + listener.address().port);
6464
});

0 commit comments

Comments
 (0)