Skip to content

Commit b78cde4

Browse files
committed
added UserList
1 parent d6e6021 commit b78cde4

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
work correctly both with client-side routing and a non-root public URL.
2525
Learn how to configure a non-root public URL by running `npm run build`.
2626
-->
27-
<title>React App</title>
27+
<title>User List</title>
2828
</head>
2929
<body>
3030
<noscript>You need to enable JavaScript to run this app.</noscript>

src/App.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React from 'react';
1+
import UserList from './UserList';
22

33
function App() {
44
return (
55
<div>
6-
<h1>hola que tal</h1>
6+
<h1>User List</h1>
7+
<UserList/>
78
</div>
89
);
910
}

src/UserList.tsx

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, { useState, useEffect } from "react";
2+
3+
interface User {
4+
id: number;
5+
name: string;
6+
username: string;
7+
email: string;
8+
address: {
9+
city: string;
10+
};
11+
}
12+
13+
const UserList: React.FC = () => {
14+
const [users, setUsers] = useState<User[]>([]);
15+
const [filteredUsers, setFilteredUsers] = useState<User[]>([]);
16+
const [filterBy, setFilterBy] = useState<string>("name");
17+
18+
useEffect(() => {
19+
fetch("https://jsonplaceholder.typicode.com/users")
20+
.then((response) => response.json())
21+
.then((data: User[]) => {
22+
setUsers(data);
23+
setFilteredUsers(data);
24+
})
25+
.catch((error) => console.log(error));
26+
}, []);
27+
28+
const handleFilterChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
29+
setFilterBy(event.target.value);
30+
};
31+
32+
const handleFilterInput = (event: React.ChangeEvent<HTMLInputElement>) => {
33+
const filterValue = event.target.value.toLowerCase();
34+
const filtered = users.filter((user) =>
35+
user[filterBy].toLowerCase().includes(filterValue)
36+
);
37+
setFilteredUsers(filtered);
38+
};
39+
40+
return (
41+
<div>
42+
<label htmlFor="filterBy">Filter by:</label>
43+
<select id="filterBy" value={filterBy} onChange={handleFilterChange}>
44+
<option value="name">Name</option>
45+
<option value="email">Email</option>
46+
<option value="address.city">City</option>
47+
</select>
48+
<input type="text" onChange={handleFilterInput} />
49+
<table>
50+
<thead>
51+
<tr>
52+
<th>ID</th>
53+
<th>Name</th>
54+
<th>Username</th>
55+
<th>Email</th>
56+
<th>City</th>
57+
</tr>
58+
</thead>
59+
<tbody>
60+
{filteredUsers.map((user) => (
61+
<tr key={user.id}>
62+
<td>{user.id}</td>
63+
<td>{user.name}</td>
64+
<td>{user.username}</td>
65+
<td>{user.email}</td>
66+
<td>{user.address.city}</td>
67+
</tr>
68+
))}
69+
</tbody>
70+
</table>
71+
</div>
72+
);
73+
};
74+
75+
export default UserList;

src/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
3-
// import './index.css';
43
import App from './App';
54

65
const root = ReactDOM.createRoot(

0 commit comments

Comments
 (0)