2022-08-30 12:39:10 +00:00
|
|
|
import { Row } from '../layout/row'
|
|
|
|
import { Content } from '../editor'
|
|
|
|
import { TextEditor, useTextEditor } from 'web/components/editor'
|
|
|
|
import { Button } from '../button'
|
|
|
|
import { Spacer } from '../layout/spacer'
|
|
|
|
import { Group } from 'common/group'
|
|
|
|
import { deleteFieldFromGroup, updateGroup } from 'web/lib/firebase/groups'
|
|
|
|
import PencilIcon from '@heroicons/react/solid/PencilIcon'
|
2022-10-05 20:19:10 +00:00
|
|
|
import { TrashIcon } from '@heroicons/react/solid'
|
2022-08-30 12:39:10 +00:00
|
|
|
import { createPost } from 'web/lib/firebase/api'
|
|
|
|
import { Post } from 'common/post'
|
|
|
|
import { deletePost, updatePost } from 'web/lib/firebase/posts'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import { usePost } from 'web/hooks/use-post'
|
2022-10-05 20:19:10 +00:00
|
|
|
import { Col } from '../layout/col'
|
2022-08-30 12:39:10 +00:00
|
|
|
|
2022-10-02 23:02:31 +00:00
|
|
|
export function GroupOverviewPost(props: {
|
2022-08-30 12:39:10 +00:00
|
|
|
group: Group
|
2022-08-31 18:29:49 +00:00
|
|
|
isEditable: boolean
|
2022-09-23 19:11:50 +00:00
|
|
|
post: Post | null
|
2022-08-30 12:39:10 +00:00
|
|
|
}) {
|
2022-08-31 18:29:49 +00:00
|
|
|
const { group, isEditable } = props
|
2022-08-30 12:39:10 +00:00
|
|
|
const post = usePost(group.aboutPostId) ?? props.post
|
|
|
|
|
|
|
|
return (
|
2022-09-08 16:32:42 +00:00
|
|
|
<div className="rounded-md bg-white p-4 ">
|
2022-09-23 19:11:50 +00:00
|
|
|
{isEditable && <RichEditGroupAboutPost group={group} post={post} />}
|
|
|
|
{!isEditable && post && <Content content={post.content} />}
|
2022-08-30 12:39:10 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:11:50 +00:00
|
|
|
function RichEditGroupAboutPost(props: { group: Group; post: Post | null }) {
|
2022-08-30 12:39:10 +00:00
|
|
|
const { group, post } = props
|
|
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
|
|
|
|
const { editor, upload } = useTextEditor({
|
2022-09-23 19:11:50 +00:00
|
|
|
defaultValue: post?.content,
|
2022-08-30 12:39:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
async function savePost() {
|
|
|
|
if (!editor) return
|
|
|
|
const newPost = {
|
|
|
|
title: group.name,
|
2022-10-05 10:37:23 +00:00
|
|
|
subtitle: 'About post for the group',
|
2022-08-30 12:39:10 +00:00
|
|
|
content: editor.getJSON(),
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:11:50 +00:00
|
|
|
if (post == null) {
|
2022-08-30 12:39:10 +00:00
|
|
|
const result = await createPost(newPost).catch((e) => {
|
|
|
|
console.error(e)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
await updateGroup(group, {
|
|
|
|
aboutPostId: result.post.id,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await updatePost(post, {
|
|
|
|
content: newPost.content,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteGroupAboutPost() {
|
2022-09-23 19:11:50 +00:00
|
|
|
if (post == null) return
|
2022-08-30 12:39:10 +00:00
|
|
|
await deletePost(post)
|
|
|
|
await deleteFieldFromGroup(group, 'aboutPostId')
|
|
|
|
}
|
|
|
|
|
|
|
|
return editing ? (
|
|
|
|
<>
|
|
|
|
<TextEditor editor={editor} upload={upload} />
|
|
|
|
<Spacer h={2} />
|
|
|
|
<Row className="gap-2">
|
|
|
|
<Button
|
|
|
|
onClick={async () => {
|
|
|
|
await savePost()
|
|
|
|
setEditing(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Save
|
|
|
|
</Button>
|
|
|
|
<Button color="gray" onClick={() => setEditing(false)}>
|
|
|
|
Cancel
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-09-23 19:11:50 +00:00
|
|
|
{post == null ? (
|
2022-08-30 12:39:10 +00:00
|
|
|
<div className="text-center text-gray-500">
|
|
|
|
<p className="text-sm">
|
|
|
|
No post has been added yet.
|
|
|
|
<Spacer h={2} />
|
|
|
|
<Button onClick={() => setEditing(true)}>Add a post</Button>
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
) : (
|
2022-10-05 20:19:10 +00:00
|
|
|
<Col>
|
|
|
|
<Content content={post.content} />
|
|
|
|
<Row className="place-content-end">
|
2022-08-30 12:39:10 +00:00
|
|
|
<Button
|
2022-10-05 20:19:10 +00:00
|
|
|
color="gray-white"
|
|
|
|
size="2xs"
|
2022-08-30 12:39:10 +00:00
|
|
|
onClick={() => {
|
|
|
|
setEditing(true)
|
|
|
|
editor?.commands.focus('end')
|
|
|
|
}}
|
|
|
|
>
|
2022-10-05 20:19:10 +00:00
|
|
|
<PencilIcon className="inline h-5 w-5" />
|
2022-08-30 12:39:10 +00:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
2022-10-05 20:19:10 +00:00
|
|
|
color="gray-white"
|
|
|
|
size="2xs"
|
2022-08-30 12:39:10 +00:00
|
|
|
onClick={() => {
|
|
|
|
deleteGroupAboutPost()
|
|
|
|
}}
|
|
|
|
>
|
2022-10-05 21:21:03 +00:00
|
|
|
<TrashIcon className="inline h-5 w-5 text-red-500" />
|
2022-08-30 12:39:10 +00:00
|
|
|
</Button>
|
2022-10-05 20:19:10 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
2022-08-30 12:39:10 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|