2022-11-14 04:28:40 -05:00
|
|
|
"use client"
|
2022-11-28 21:33:06 -05:00
|
|
|
import { Fieldset } from "@geist-ui/core/dist"
|
2022-04-13 00:14:10 -04:00
|
|
|
import Table from "rc-table"
|
2022-04-20 02:36:56 -04:00
|
|
|
import ActionDropdown from "./action-dropdown"
|
2022-11-12 04:28:06 -05:00
|
|
|
import SettingsGroup from "@components/settings-group"
|
2022-11-14 04:28:40 -05:00
|
|
|
import type { User, UserWithPosts } from "@lib/server/prisma"
|
|
|
|
import { useState } from "react"
|
2022-11-28 21:33:06 -05:00
|
|
|
import { useToasts } from "@components/toasts"
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const UserTable = ({ users: initial }: { users: UserWithPosts[] }) => {
|
|
|
|
const [users, setUsers] = useState(initial)
|
2022-04-13 00:14:10 -04:00
|
|
|
const { setToast } = useToasts()
|
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
const toggleRole = async (id: string, role: "admin" | "user") => {
|
2022-11-14 04:28:40 -05:00
|
|
|
const res = await fetch("/api/admin?action=toggle-role", {
|
2022-04-13 00:14:10 -04:00
|
|
|
method: "POST",
|
2022-11-14 04:28:40 -05:00
|
|
|
body: JSON.stringify({
|
|
|
|
userId: id,
|
|
|
|
role
|
|
|
|
})
|
2022-04-13 00:14:10 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
setToast({
|
2022-11-28 21:33:06 -05:00
|
|
|
message: "Role updated",
|
2022-04-13 00:14:10 -04:00
|
|
|
type: "success"
|
|
|
|
})
|
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
setUsers((users) => {
|
|
|
|
const newUsers = users?.map((user) => {
|
|
|
|
if (user.id === id) {
|
|
|
|
return {
|
|
|
|
...user,
|
|
|
|
role
|
|
|
|
}
|
2022-04-13 00:14:10 -04:00
|
|
|
}
|
2022-04-20 02:36:56 -04:00
|
|
|
return user
|
|
|
|
})
|
|
|
|
return newUsers
|
2022-04-13 00:14:10 -04:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setToast({
|
2022-11-28 21:33:06 -05:00
|
|
|
message: "Something went wrong",
|
2022-04-13 00:14:10 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
const deleteUser = async (id: string) => {
|
2022-04-13 00:14:10 -04:00
|
|
|
const confirm = window.confirm("Are you sure you want to delete this user?")
|
|
|
|
if (!confirm) return
|
2022-11-14 04:28:40 -05:00
|
|
|
// const res = await adminFetcher(`/users/${id}`, {
|
|
|
|
// method: "DELETE"
|
|
|
|
// })
|
|
|
|
const res = await fetch("/api/admin?action=delete-user", {
|
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({
|
|
|
|
userId: id
|
|
|
|
})
|
2022-04-13 00:14:10 -04:00
|
|
|
})
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
setUsers((users) => {
|
|
|
|
const newUsers = users?.filter((user) => user.id !== id)
|
|
|
|
return newUsers
|
|
|
|
})
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
if (res.status === 200) {
|
|
|
|
setToast({
|
|
|
|
text: "User deleted",
|
|
|
|
type: "success"
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setToast({
|
2022-11-12 02:59:33 -05:00
|
|
|
text: "Something went wrong",
|
2022-04-13 00:14:10 -04:00
|
|
|
type: "error"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const tableUsers = users?.map((user) => {
|
|
|
|
return {
|
|
|
|
id: user.id,
|
|
|
|
displayName: user.displayName,
|
|
|
|
posts: user.posts?.length || 0,
|
2022-11-14 20:32:32 -05:00
|
|
|
createdAt: `${new Date(user.createdAt).toLocaleDateString()} ${new Date(
|
2022-11-14 04:28:40 -05:00
|
|
|
user.createdAt
|
|
|
|
).toLocaleTimeString()}`,
|
|
|
|
role: user.role,
|
|
|
|
actions: ""
|
|
|
|
}
|
|
|
|
})
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
const usernameColumns = [
|
|
|
|
{
|
2022-11-14 04:28:40 -05:00
|
|
|
title: "Name",
|
|
|
|
dataIndex: "displayName",
|
|
|
|
key: "displayName",
|
2022-04-13 00:14:10 -04:00
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Posts",
|
|
|
|
dataIndex: "posts",
|
|
|
|
key: "posts",
|
|
|
|
width: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Created",
|
|
|
|
dataIndex: "createdAt",
|
|
|
|
key: "createdAt",
|
|
|
|
width: 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Role",
|
|
|
|
dataIndex: "role",
|
|
|
|
key: "role",
|
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Actions",
|
|
|
|
dataIndex: "",
|
|
|
|
key: "actions",
|
|
|
|
width: 50,
|
2022-04-20 02:36:56 -04:00
|
|
|
render(user: User) {
|
2022-04-13 00:14:10 -04:00
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<ActionDropdown
|
|
|
|
title="Actions"
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: user.role === "admin" ? "Change role" : "Make admin",
|
2022-04-22 01:01:59 -04:00
|
|
|
onClick: () =>
|
|
|
|
toggleRole(user.id, user.role === "admin" ? "user" : "admin")
|
2022-04-20 02:36:56 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Delete",
|
|
|
|
onClick: () => deleteUser(user.id)
|
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<SettingsGroup title="Users">
|
2022-04-13 00:14:10 -04:00
|
|
|
{!users && <Fieldset.Subtitle>Loading...</Fieldset.Subtitle>}
|
2022-04-22 01:01:59 -04:00
|
|
|
{users && (
|
|
|
|
<Fieldset.Subtitle>
|
|
|
|
<h5>{users.length} users</h5>
|
|
|
|
</Fieldset.Subtitle>
|
|
|
|
)}
|
2022-04-13 00:14:10 -04:00
|
|
|
{users && <Table columns={usernameColumns} data={tableUsers} />}
|
2022-04-20 02:36:56 -04:00
|
|
|
</SettingsGroup>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default UserTable
|