|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { DotsThree, MagnifyingGlass, Plus } from "@phosphor-icons/react"; |
| 5 | +import { CgSpinner } from "react-icons/cg"; |
| 6 | +import { User, UserType } from "@prisma/client"; |
| 7 | + |
| 8 | +enum UserFilterKey { |
| 9 | + ALL = "All", |
| 10 | + STAFF = "Hope for Haiti Staff", |
| 11 | + PARTNERS = "Partners", |
| 12 | +} |
| 13 | + |
| 14 | +const filterMap: Record<UserFilterKey, (user: User) => boolean> = { |
| 15 | + [UserFilterKey.ALL]: () => true, |
| 16 | + [UserFilterKey.STAFF]: (user) => |
| 17 | + user.type === UserType.STAFF || |
| 18 | + user.type === UserType.ADMIN || |
| 19 | + user.type === UserType.SUPER_ADMIN, |
| 20 | + [UserFilterKey.PARTNERS]: (user) => user.type === UserType.PARTNER, |
| 21 | +}; |
| 22 | + |
| 23 | +function formatUserType(type: UserType): string { |
| 24 | + return type |
| 25 | + .toLowerCase() |
| 26 | + .split("_") |
| 27 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 28 | + .join(" "); |
| 29 | +} |
| 30 | + |
1 | 31 | export default function AccountManagementScreen() {
|
| 32 | + const [users, setUsers] = useState<User[]>([]); |
| 33 | + const [filteredUsers, setFilteredUsers] = useState<User[]>([]); |
| 34 | + const [activeTab, setActiveTab] = useState<string>("All"); |
| 35 | + const [isLoading, setIsLoading] = useState(true); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + const fetchUsers = async () => { |
| 39 | + try { |
| 40 | + const response = await fetch("/api/users"); |
| 41 | + if (response.ok) { |
| 42 | + const data = await response.json(); |
| 43 | + setUsers(data); |
| 44 | + setFilteredUsers(data); |
| 45 | + } |
| 46 | + } catch (error) { |
| 47 | + console.error("Failed to fetch users:", error); |
| 48 | + } finally { |
| 49 | + setIsLoading(false); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + fetchUsers(); |
| 54 | + }, []); |
| 55 | + |
| 56 | + const filterUsers = (type: UserFilterKey) => { |
| 57 | + setActiveTab(type); |
| 58 | + setFilteredUsers(users.filter(filterMap[type])); |
| 59 | + }; |
| 60 | + |
2 | 61 | return (
|
3 | 62 | <>
|
4 | 63 | <h1 className="text-2xl font-semibold">Account Management</h1>
|
| 64 | + <div className="flex justify-between items-center w-full py-4"> |
| 65 | + <div className="relative w-1/3"> |
| 66 | + <MagnifyingGlass |
| 67 | + className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-500" |
| 68 | + size={18} |
| 69 | + /> |
| 70 | + <input |
| 71 | + type="text" |
| 72 | + placeholder="Search" |
| 73 | + className="pl-10 pr-4 py-2 w-full border border-gray-300 rounded-lg bg-gray-100 focus:outline-none focus:border-gray-400" |
| 74 | + /> |
| 75 | + </div> |
| 76 | + <button className="flex items-center gap-2 bg-red-500 text-white px-4 py-2 rounded-lg font-medium hover:bg-red-600 transition"> |
| 77 | + <Plus size={18} /> Add account |
| 78 | + </button> |
| 79 | + </div> |
| 80 | + <div className="flex space-x-4 mt-4 border-b-2"> |
| 81 | + {Object.keys(filterMap).map((tab) => { |
| 82 | + const key = tab as UserFilterKey; |
| 83 | + |
| 84 | + return ( |
| 85 | + <button |
| 86 | + key={tab} |
| 87 | + data-active={activeTab === tab} |
| 88 | + className="px-2 py-1 text-md font-medium relative -mb-px transition-colors focus:outline-none data-[active=true]:border-b-2 data-[active=true]:border-black data-[active=true]:bottom-[-1px] data-[active=false]:text-gray-500" |
| 89 | + onClick={() => filterUsers(key)} |
| 90 | + > |
| 91 | + <div className="hover:bg-gray-100 px-2 py-1 rounded">{tab}</div> |
| 92 | + </button> |
| 93 | + ); |
| 94 | + })} |
| 95 | + </div> |
| 96 | + |
| 97 | + {isLoading ? ( |
| 98 | + <div className="flex justify-center items-center mt-8"> |
| 99 | + <CgSpinner className="w-16 h-16 animate-spin opacity-50" /> |
| 100 | + </div> |
| 101 | + ) : ( |
| 102 | + <div className="overflow-x-scroll"> |
| 103 | + <table className="mt-4 rounded-t-lg overflow-hidden"> |
| 104 | + <thead> |
| 105 | + <tr className="bg-gray-100 border-b-2"> |
| 106 | + <th className="px-4 py-2 text-left font-normal">Name</th> |
| 107 | + <th className="px-4 py-2 text-left font-normal">Email</th> |
| 108 | + <th className="px-4 py-2 text-left font-normal">Role</th> |
| 109 | + <th className="px-4 py-2 text-left font-normal">Status</th> |
| 110 | + <th className="px-4 py-2 text-left font-normal">Manage</th> |
| 111 | + </tr> |
| 112 | + </thead> |
| 113 | + <tbody> |
| 114 | + {filteredUsers.map((user, index) => ( |
| 115 | + <tr |
| 116 | + key={index} |
| 117 | + data-odd={index % 2 !== 0} |
| 118 | + className="bg-white data-[odd=true]:bg-gray-50" |
| 119 | + > |
| 120 | + <td className="border-b px-4 py-2 w-1/5">{user.name}</td> |
| 121 | + <td className="border-b px-4 py-2 w-1/5">{user.email}</td> |
| 122 | + <td className="border-b px-4 py-2 w-1/5"> |
| 123 | + {formatUserType(user.type)} |
| 124 | + </td> |
| 125 | + <td className="border-b px-4 py-2 w-1/5"> |
| 126 | + <span className="px-2 py-1 rounded bg-green-primary whitespace-nowrap"> |
| 127 | + Account created |
| 128 | + </span> |
| 129 | + </td> |
| 130 | + <td className="border-b px-4 py-2 w-12"> |
| 131 | + <div className="float-right"> |
| 132 | + <DotsThree |
| 133 | + weight="bold" |
| 134 | + className="cursor-pointer" |
| 135 | + onClick={() => {}} |
| 136 | + /> |
| 137 | + </div> |
| 138 | + </td> |
| 139 | + </tr> |
| 140 | + ))} |
| 141 | + </tbody> |
| 142 | + </table> |
| 143 | + </div> |
| 144 | + )} |
5 | 145 | </>
|
6 | 146 | );
|
7 | 147 | }
|
0 commit comments