-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.js
58 lines (53 loc) · 1.57 KB
/
Search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, { Fragment, useState } from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
// import "./App.css";
function Search() {
const [name, setName] = useState("");
const [users, setUsers] = useState([]);
const onSubmitForm = async e => {
e.preventDefault();
try {
const response = await fetch(`http://localhost:5000/users/?name=${name}`);
const parseResponse = await response.json();
setUsers(parseResponse);
} catch (err) {
console.error(err.message);
}
};
return (
<Fragment>
<div className="container text-center">
<h1 className="my-5">Party List</h1>
<form className="d-flex" onSubmit={onSubmitForm}>
<input
type="text"
name="name"
placeholder="Enter user ..."
className="form-control"
value={name}
onChange={e => setName(e.target.value)}
/>
<button className="btn btn-success">Submit</button>
</form>
<table className="table my-5">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
{users.map(user => (
<tr key={user.user_id}>
<td>{user.first_name}</td>
<td>{user.last_name}</td>
</tr>
))}
</tbody>
</table>
{users.length === 0 && <p>No Results Found</p>}
</div>
</Fragment>
);
}
export default Search;