2022-04-20 02:36:56 -04:00
|
|
|
import SettingsGroup from "@components/settings-group"
|
|
|
|
import { Fieldset, useToasts } from "@geist-ui/core"
|
|
|
|
import byteToMB from "@lib/byte-to-mb"
|
2022-04-13 00:14:10 -04:00
|
|
|
import { Post } from "@lib/types"
|
2022-04-20 02:36:56 -04:00
|
|
|
import Table from "rc-table"
|
2022-04-13 00:14:10 -04:00
|
|
|
import { useEffect, useMemo, useState } from "react"
|
|
|
|
import { adminFetcher } from "."
|
2022-04-20 02:36:56 -04:00
|
|
|
import ActionDropdown from "./action-dropdown"
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
const PostTable = () => {
|
|
|
|
const [posts, setPosts] = useState<Post[]>()
|
2022-04-20 04:52:07 -04:00
|
|
|
const { setToast } = useToasts()
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchPosts = async () => {
|
|
|
|
const res = await adminFetcher("/posts")
|
|
|
|
const data = await res.json()
|
|
|
|
setPosts(data)
|
|
|
|
}
|
|
|
|
fetchPosts()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const tablePosts = useMemo(
|
|
|
|
() =>
|
|
|
|
posts?.map((post) => {
|
|
|
|
return {
|
|
|
|
id: post.id,
|
|
|
|
title: post.title,
|
|
|
|
files: post.files?.length || 0,
|
|
|
|
createdAt: `${new Date(
|
|
|
|
post.createdAt
|
|
|
|
).toLocaleDateString()} ${new Date(
|
|
|
|
post.createdAt
|
|
|
|
).toLocaleTimeString()}`,
|
|
|
|
visibility: post.visibility,
|
|
|
|
size: post.files
|
|
|
|
? byteToMB(
|
2022-04-22 01:01:59 -04:00
|
|
|
post.files.reduce((acc, file) => acc + file.html.length, 0)
|
|
|
|
)
|
2022-04-13 00:14:10 -04:00
|
|
|
: 0,
|
|
|
|
actions: ""
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
[posts]
|
|
|
|
)
|
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
const deletePost = async (/* id: string */) => {
|
|
|
|
return alert("Not implemented")
|
|
|
|
|
|
|
|
// const confirm = window.confirm("Are you sure you want to delete this post?")
|
|
|
|
// if (!confirm) return
|
|
|
|
// const res = await adminFetcher(`/posts/${id}`, {
|
|
|
|
// method: "DELETE",
|
|
|
|
// })
|
|
|
|
|
|
|
|
// const json = await res.json()
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
// if (res.status === 200) {
|
|
|
|
// setToast({
|
|
|
|
// text: "Post deleted",
|
|
|
|
// type: "success"
|
|
|
|
// })
|
2022-04-13 00:14:10 -04:00
|
|
|
|
2022-04-20 02:36:56 -04:00
|
|
|
// setPosts((posts) => {
|
|
|
|
// const newPosts = posts?.filter((post) => post.id !== id)
|
|
|
|
// return newPosts
|
|
|
|
// })
|
|
|
|
// } else {
|
|
|
|
// setToast({
|
|
|
|
// text: json.error || "Something went wrong",
|
|
|
|
// type: "error"
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
}
|
2022-04-13 00:14:10 -04:00
|
|
|
|
|
|
|
const tableColumns = [
|
|
|
|
{
|
|
|
|
title: "Title",
|
|
|
|
dataIndex: "title",
|
|
|
|
key: "title",
|
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Files",
|
|
|
|
dataIndex: "files",
|
|
|
|
key: "files",
|
|
|
|
width: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Created",
|
|
|
|
dataIndex: "createdAt",
|
|
|
|
key: "createdAt",
|
|
|
|
width: 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Visibility",
|
|
|
|
dataIndex: "visibility",
|
|
|
|
key: "visibility",
|
|
|
|
width: 50
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Size (MB)",
|
|
|
|
dataIndex: "size",
|
|
|
|
key: "size",
|
|
|
|
width: 10
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Actions",
|
|
|
|
dataIndex: "",
|
|
|
|
key: "actions",
|
|
|
|
width: 50,
|
2022-04-22 01:01:59 -04:00
|
|
|
render() {
|
2022-04-13 00:14:10 -04:00
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<ActionDropdown
|
|
|
|
title="Actions"
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: "Delete",
|
2022-04-22 01:01:59 -04:00
|
|
|
onClick: () => deletePost()
|
2022-04-20 02:36:56 -04:00
|
|
|
}
|
|
|
|
]}
|
|
|
|
/>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
return (
|
2022-04-20 02:36:56 -04:00
|
|
|
<SettingsGroup title="Posts">
|
2022-04-13 00:14:10 -04:00
|
|
|
{!posts && <Fieldset.Subtitle>Loading...</Fieldset.Subtitle>}
|
2022-04-22 01:01:59 -04:00
|
|
|
{posts && (
|
|
|
|
<Fieldset.Subtitle>
|
|
|
|
<h5>{posts.length} posts</h5>
|
|
|
|
</Fieldset.Subtitle>
|
|
|
|
)}
|
2022-04-13 00:14:10 -04:00
|
|
|
{posts && <Table columns={tableColumns} data={tablePosts} />}
|
2022-04-20 02:36:56 -04:00
|
|
|
</SettingsGroup>
|
2022-04-13 00:14:10 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PostTable
|