2024-05-02 08:17:56 -05:00
|
|
|
import DeleteUserModal from "@/components/ModalContent/DeleteUserModal";
|
|
|
|
import useUserStore from "@/store/admin/users";
|
2024-04-22 17:00:59 -05:00
|
|
|
import { User as U } from "@prisma/client";
|
|
|
|
import Link from "next/link";
|
2024-05-02 08:17:56 -05:00
|
|
|
import { Fragment, useEffect, useState } from "react";
|
2024-04-22 17:00:59 -05:00
|
|
|
|
|
|
|
interface User extends U {
|
|
|
|
subscriptions: {
|
|
|
|
active: boolean;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-05-02 08:17:56 -05:00
|
|
|
type UserModal = {
|
|
|
|
isOpen: boolean;
|
|
|
|
userId: number | null;
|
|
|
|
};
|
|
|
|
|
2024-04-22 17:00:59 -05:00
|
|
|
export default function Admin() {
|
2024-05-02 08:17:56 -05:00
|
|
|
const { users, setUsers } = useUserStore();
|
2024-04-22 17:00:59 -05:00
|
|
|
|
2024-04-24 08:16:34 -05:00
|
|
|
const [searchQuery, setSearchQuery] = useState("");
|
|
|
|
const [filteredUsers, setFilteredUsers] = useState<User[]>();
|
|
|
|
|
2024-05-02 08:17:56 -05:00
|
|
|
const [deleteUserModal, setDeleteUserModal] = useState<UserModal>({
|
|
|
|
isOpen: false,
|
|
|
|
userId: null,
|
|
|
|
});
|
|
|
|
|
2024-04-22 17:00:59 -05:00
|
|
|
useEffect(() => {
|
|
|
|
// fetch users
|
|
|
|
fetch("/api/v1/users")
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((data) => setUsers(data.response));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2024-04-24 08:16:34 -05:00
|
|
|
<div className="max-w-6xl mx-auto p-5">
|
|
|
|
<div className="flex sm:flex-row flex-col justify-between gap-2">
|
|
|
|
<div className="gap-2 inline-flex items-center">
|
|
|
|
<Link
|
|
|
|
href="/dashboard"
|
|
|
|
className="text-neutral btn btn-square btn-sm btn-ghost"
|
|
|
|
>
|
|
|
|
<i className="bi-chevron-left text-xl"></i>
|
|
|
|
</Link>
|
2024-05-02 08:17:56 -05:00
|
|
|
<p className="capitalize text-3xl font-thin inline">
|
2024-04-24 08:16:34 -05:00
|
|
|
User Administration
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex items-center relative justify-between gap-2">
|
|
|
|
<div>
|
|
|
|
<label
|
|
|
|
htmlFor="search-box"
|
|
|
|
className="inline-flex items-center w-fit absolute left-1 pointer-events-none rounded-md p-1 text-primary"
|
|
|
|
>
|
|
|
|
<i className="bi-search"></i>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
id="search-box"
|
|
|
|
type="text"
|
|
|
|
placeholder={"Search for Users"}
|
|
|
|
value={searchQuery}
|
|
|
|
onChange={(e) => {
|
|
|
|
setSearchQuery(e.target.value);
|
|
|
|
|
|
|
|
if (users) {
|
|
|
|
setFilteredUsers(
|
|
|
|
users.filter((user) =>
|
|
|
|
JSON.stringify(user)
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(e.target.value.toLowerCase())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
className="border border-neutral-content bg-base-200 focus:border-primary py-1 rounded-md pl-9 pr-2 w-full max-w-[15rem] md:w-[15rem] md:max-w-full duration-200 outline-none"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="flex items-center btn btn-accent dark:border-violet-400 text-white btn-sm px-2 aspect-square relative">
|
|
|
|
<i className="bi-plus text-3xl absolute"></i>
|
|
|
|
</div>
|
|
|
|
</div>
|
2024-04-22 17:00:59 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="divider my-3"></div>
|
|
|
|
|
2024-04-24 08:16:34 -05:00
|
|
|
{filteredUsers && filteredUsers.length > 0 && searchQuery !== "" ? (
|
2024-05-02 08:17:56 -05:00
|
|
|
UserListing(filteredUsers, deleteUserModal, setDeleteUserModal)
|
2024-04-24 08:16:34 -05:00
|
|
|
) : searchQuery !== "" ? (
|
|
|
|
<p>No users found with the given search query.</p>
|
|
|
|
) : users && users.length > 0 ? (
|
2024-05-02 08:17:56 -05:00
|
|
|
UserListing(users, deleteUserModal, setDeleteUserModal)
|
2024-04-22 17:00:59 -05:00
|
|
|
) : (
|
|
|
|
<p>No users found.</p>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2024-04-24 08:16:34 -05:00
|
|
|
|
2024-05-02 08:17:56 -05:00
|
|
|
const UserListing = (
|
|
|
|
users: User[],
|
|
|
|
deleteUserModal: UserModal,
|
|
|
|
setDeleteUserModal: Function
|
|
|
|
) => {
|
2024-04-24 08:16:34 -05:00
|
|
|
return (
|
|
|
|
<div className="overflow-x-auto whitespace-nowrap w-full">
|
|
|
|
<table className="table table-zebra w-full">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th></th>
|
|
|
|
<th>Username</th>
|
|
|
|
{process.env.NEXT_PUBLIC_EMAIL_PROVIDER === "true" && (
|
|
|
|
<th>Email</th>
|
|
|
|
)}
|
|
|
|
{process.env.NEXT_PUBLIC_STRIPE === "true" && <th>Subscribed</th>}
|
|
|
|
<th>Created At</th>
|
|
|
|
<th></th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{users.map((user, index) => (
|
2024-05-02 08:17:56 -05:00
|
|
|
<tr key={index}>
|
2024-04-24 08:16:34 -05:00
|
|
|
<td className="rounded-tl">{index + 1}</td>
|
|
|
|
<td>{user.username}</td>
|
|
|
|
{process.env.NEXT_PUBLIC_EMAIL_PROVIDER === "true" && (
|
|
|
|
<td>{user.email}</td>
|
|
|
|
)}
|
|
|
|
{process.env.NEXT_PUBLIC_STRIPE === "true" && (
|
|
|
|
<td>{JSON.stringify(user.subscriptions.active)}</td>
|
|
|
|
)}
|
|
|
|
<td>{new Date(user.createdAt).toLocaleString()}</td>
|
|
|
|
<td>
|
2024-05-02 08:17:56 -05:00
|
|
|
<button
|
|
|
|
className="btn btn-sm btn-ghost"
|
|
|
|
onClick={() =>
|
|
|
|
setDeleteUserModal({ isOpen: true, userId: user.id })
|
|
|
|
}
|
|
|
|
>
|
2024-04-24 08:16:34 -05:00
|
|
|
<i className="bi bi-trash"></i>
|
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2024-05-02 08:17:56 -05:00
|
|
|
|
|
|
|
{deleteUserModal.isOpen && deleteUserModal.userId ? (
|
|
|
|
<DeleteUserModal
|
|
|
|
onClose={() => setDeleteUserModal({ isOpen: false, userId: null })}
|
|
|
|
userId={deleteUserModal.userId}
|
|
|
|
/>
|
|
|
|
) : null}
|
2024-04-24 08:16:34 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|