From ea1f95b355ff4a41f1b4825f840bbf617ba8d416 Mon Sep 17 00:00:00 2001 From: Pico2x Date: Tue, 30 Aug 2022 13:24:42 +0100 Subject: [PATCH] Fixed James nits --- web/components/groups/group-about-post.tsx | 8 +++----- web/hooks/use-post.ts | 13 +++++++++++++ web/lib/firebase/posts.ts | 9 ++++++++- web/pages/group/[...slugs]/index.tsx | 3 ++- 4 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 web/hooks/use-post.ts diff --git a/web/components/groups/group-about-post.tsx b/web/components/groups/group-about-post.tsx index 64336fde..1b42c04d 100644 --- a/web/components/groups/group-about-post.tsx +++ b/web/components/groups/group-about-post.tsx @@ -11,15 +11,16 @@ import { DocumentRemoveIcon } from '@heroicons/react/solid' import { createPost } from 'web/lib/firebase/api' import { Post } from 'common/post' import { deletePost, updatePost } from 'web/lib/firebase/posts' -import { useRouter } from 'next/router' import { useState } from 'react' +import { usePost } from 'web/hooks/use-post' export function GroupAboutPost(props: { group: Group isCreator: boolean post: Post }) { - const { group, isCreator, post } = props + const { group, isCreator } = props + const post = usePost(group.aboutPostId) ?? props.post const isAdmin = useAdmin() if (group.aboutPostId == null && !isCreator) { @@ -41,7 +42,6 @@ function RichEditGroupAboutPost(props: { group: Group; post: Post }) { const { group, post } = props const [editing, setEditing] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false) - const router = useRouter() const { editor, upload } = useTextEditor({ defaultValue: post.content, @@ -68,13 +68,11 @@ function RichEditGroupAboutPost(props: { group: Group; post: Post }) { content: newPost.content, }) } - await router.replace(router.asPath) } async function deleteGroupAboutPost() { await deletePost(post) await deleteFieldFromGroup(group, 'aboutPostId') - await router.replace(router.asPath) } return editing ? ( diff --git a/web/hooks/use-post.ts b/web/hooks/use-post.ts new file mode 100644 index 00000000..9daf2d22 --- /dev/null +++ b/web/hooks/use-post.ts @@ -0,0 +1,13 @@ +import { useEffect, useState } from 'react' +import { Post } from 'common/post' +import { listenForPost } from 'web/lib/firebase/posts' + +export const usePost = (postId: string | undefined) => { + const [post, setPost] = useState() + + useEffect(() => { + if (postId) return listenForPost(postId, setPost) + }, [postId]) + + return post +} diff --git a/web/lib/firebase/posts.ts b/web/lib/firebase/posts.ts index 10bea499..162933af 100644 --- a/web/lib/firebase/posts.ts +++ b/web/lib/firebase/posts.ts @@ -7,7 +7,7 @@ import { where, } from 'firebase/firestore' import { Post } from 'common/post' -import { coll, getValue } from './utils' +import { coll, getValue, listenForValue } from './utils' export const posts = coll('posts') @@ -32,3 +32,10 @@ export async function getPostBySlug(slug: string) { const docs = (await getDocs(q)).docs return docs.length === 0 ? null : docs[0].data() } + +export function listenForPost( + postId: string, + setPost: (post: Post | null) => void +) { + return listenForValue(doc(posts, postId), setPost) +} diff --git a/web/pages/group/[...slugs]/index.tsx b/web/pages/group/[...slugs]/index.tsx index 3bb319ec..5271a395 100644 --- a/web/pages/group/[...slugs]/index.tsx +++ b/web/pages/group/[...slugs]/index.tsx @@ -49,6 +49,7 @@ import { GroupAboutPost } from 'web/components/groups/group-about-post' import { getPost } from 'web/lib/firebase/posts' import { Post } from 'common/post' import { Spacer } from 'web/components/layout/spacer' +import { usePost } from 'web/hooks/use-post' export const getStaticProps = fromPropz(getStaticPropz) export async function getStaticPropz(props: { params: { slugs: string[] } }) { @@ -147,7 +148,6 @@ export default function GroupPage(props: { topTraders, creatorScores, topCreators, - aboutPost, } = props const router = useRouter() @@ -155,6 +155,7 @@ export default function GroupPage(props: { const page = slugs?.[1] as typeof groupSubpages[number] const group = useGroup(props.group?.id) ?? props.group + const aboutPost = usePost(props.aboutPost?.id) ?? props.aboutPost const user = useUser()