| 1 | import React, { useEffect, useState } from "react";
|
|---|
| 2 | import { useNavigate } from "react-router-dom";
|
|---|
| 3 |
|
|---|
| 4 | interface User {
|
|---|
| 5 | id: number;
|
|---|
| 6 | username: string;
|
|---|
| 7 | fullName: string;
|
|---|
| 8 | }
|
|---|
| 9 |
|
|---|
| 10 | const AllUsers = () => {
|
|---|
| 11 | const [users, setUsers] = useState<User[]>([]);
|
|---|
| 12 | const [searchedUser, setSearchedUser] = useState<string>("");
|
|---|
| 13 | const [error, setError] = useState<string | null>(null);
|
|---|
| 14 | const navigate = useNavigate();
|
|---|
| 15 |
|
|---|
| 16 | useEffect(() => {
|
|---|
| 17 | const fetchUsers = async () => {
|
|---|
| 18 | const response = await fetch("http://localhost:8080/users/all");
|
|---|
| 19 | const data = await response.json();
|
|---|
| 20 | setUsers(data);
|
|---|
| 21 | };
|
|---|
| 22 | fetchUsers();
|
|---|
| 23 | }, []);
|
|---|
| 24 |
|
|---|
| 25 | const handleUserClick = (userId: number) => {
|
|---|
| 26 | navigate(`/users/${userId}`);
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | const handleSearch = async (e: React.FormEvent) => {
|
|---|
| 30 | e.preventDefault();
|
|---|
| 31 | try {
|
|---|
| 32 | const response = await fetch(
|
|---|
| 33 | `http://localhost:8080/users/search?name=${searchedUser}`,
|
|---|
| 34 | );
|
|---|
| 35 | if (!response.ok) throw new Error("Search failed");
|
|---|
| 36 |
|
|---|
| 37 | const data = await response.json();
|
|---|
| 38 | setUsers(data);
|
|---|
| 39 | } catch (err: any) {
|
|---|
| 40 | setError(err.message);
|
|---|
| 41 | }
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | if (users.length == 0) return <div className="p-6">Loading...</div>;
|
|---|
| 45 |
|
|---|
| 46 | return (
|
|---|
| 47 | <div className="container mx-auto p-6">
|
|---|
| 48 | <h1 className="text-3xl font-bold mb-6">All Users</h1>
|
|---|
| 49 | <form onSubmit={handleSearch} className="flex gap-2 mb-10">
|
|---|
| 50 | <input
|
|---|
| 51 | type="text"
|
|---|
| 52 | value={searchedUser}
|
|---|
| 53 | onChange={(e) => setSearchedUser(e.target.value)}
|
|---|
| 54 | className="border p-2 rounded w-full"
|
|---|
| 55 | placeholder="Search for a user..."
|
|---|
| 56 | />
|
|---|
| 57 | <button
|
|---|
| 58 | type="submit"
|
|---|
| 59 | className="bg-blue-600 text-white px-6 py-2 rounded hover:bg-blue-700 transition-colors"
|
|---|
| 60 | >
|
|---|
| 61 | Search
|
|---|
| 62 | </button>
|
|---|
| 63 | </form>
|
|---|
| 64 |
|
|---|
| 65 | <div className="grid gap-4">
|
|---|
| 66 | {users.map((u) => (
|
|---|
| 67 | <div
|
|---|
| 68 | key={u.id}
|
|---|
| 69 | onClick={() => handleUserClick(u.id)}
|
|---|
| 70 | className="bg-white shadow-md rounded-lg p-4 hover:shadow-lg hover:bg-gray-50 cursor-pointer transition-all"
|
|---|
| 71 | >
|
|---|
| 72 | <h2 className="text-xl font-semibold">{u.fullName}</h2>
|
|---|
| 73 | <p className="text-gray-600">@{u.username}</p>
|
|---|
| 74 | <p className="text-gray-600">{u.id}</p>
|
|---|
| 75 | </div>
|
|---|
| 76 | ))}
|
|---|
| 77 | </div>
|
|---|
| 78 | </div>
|
|---|
| 79 | );
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | export default AllUsers;
|
|---|