|
| 1 | +import { useCallback, useEffect, useMemo, useState } from "react"; |
| 2 | +import { MRT_ColumnDef, MantineReactTable } from "mantine-react-table"; |
| 3 | +import { Anchor } from "@mantine/core"; |
| 4 | +import { useNavigate } from "react-router-dom"; |
| 5 | +import { IUser } from "../api-types"; |
| 6 | + |
| 7 | +export const UsersPage = () => { |
| 8 | + const navigate = useNavigate(); |
| 9 | + |
| 10 | + const [users, setUsers] = useState<IUser[]>([]); |
| 11 | + const [isLoadingUser, setIsLoadingUser] = useState(false); |
| 12 | + const [isErrorLoadingUser, setIsErrorLoadingUser] = useState(false); |
| 13 | + const [isFetchingUser, setIsFetchingUser] = useState(false); |
| 14 | + |
| 15 | + const fetchUsers = useCallback(async () => { |
| 16 | + if (!users.length) { |
| 17 | + setIsLoadingUser(true); |
| 18 | + } |
| 19 | + setIsFetchingUser(true); |
| 20 | + try { |
| 21 | + const fetchUrl = new URL(`http://localhost:3333/users`); |
| 22 | + const response = await fetch(fetchUrl.href); |
| 23 | + await new Promise((resolve) => setTimeout(resolve, 1000)); // Simulate slow network |
| 24 | + const newUsers = (await response.json()) as IUser[]; |
| 25 | + setUsers(newUsers); |
| 26 | + } catch (error) { |
| 27 | + console.error(error); |
| 28 | + setIsErrorLoadingUser(true); |
| 29 | + } finally { |
| 30 | + setIsLoadingUser(false); |
| 31 | + setIsFetchingUser(false); |
| 32 | + } |
| 33 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 34 | + }, []); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + fetchUsers(); |
| 38 | + }, [fetchUsers]); |
| 39 | + |
| 40 | + const columns = useMemo<MRT_ColumnDef<IUser>[]>( |
| 41 | + () => [ |
| 42 | + { |
| 43 | + accessorKey: "name", |
| 44 | + header: "Name", |
| 45 | + }, |
| 46 | + { |
| 47 | + accessorKey: "email", |
| 48 | + header: "Email", |
| 49 | + }, |
| 50 | + { |
| 51 | + accessorKey: "phone", |
| 52 | + header: "Phone", |
| 53 | + }, |
| 54 | + { |
| 55 | + accessorKey: "website", |
| 56 | + header: "Website", |
| 57 | + Cell: ({ cell, renderedCellValue }) => ( |
| 58 | + <Anchor |
| 59 | + onClick={(e) => e.stopPropagation()} |
| 60 | + href={`https://${cell.getValue<string>()}`} |
| 61 | + target="_blank" |
| 62 | + > |
| 63 | + {renderedCellValue} |
| 64 | + </Anchor> |
| 65 | + ), |
| 66 | + }, |
| 67 | + ], |
| 68 | + [] |
| 69 | + ); |
| 70 | + |
| 71 | + return ( |
| 72 | + <MantineReactTable |
| 73 | + data={users} |
| 74 | + columns={columns} |
| 75 | + state={{ |
| 76 | + isLoading: isLoadingUser, |
| 77 | + showProgressBars: isFetchingUser, |
| 78 | + showAlertBanner: isErrorLoadingUser, |
| 79 | + }} |
| 80 | + mantineToolbarAlertBannerProps={ |
| 81 | + isErrorLoadingUser |
| 82 | + ? { |
| 83 | + color: "red", |
| 84 | + children: "Error loading data", |
| 85 | + } |
| 86 | + : undefined |
| 87 | + } |
| 88 | + mantineTableBodyRowProps={({ row }) => ({ |
| 89 | + onClick: () => navigate(`/users/${row.original.id}`), |
| 90 | + style: { |
| 91 | + cursor: "pointer", |
| 92 | + }, |
| 93 | + })} |
| 94 | + /> |
| 95 | + ); |
| 96 | +}; |
0 commit comments