Fixed James nits

This commit is contained in:
Pico2x 2022-08-30 13:24:42 +01:00
parent eca2c8f441
commit ea1f95b355
4 changed files with 26 additions and 7 deletions

View File

@ -11,15 +11,16 @@ import { DocumentRemoveIcon } from '@heroicons/react/solid'
import { createPost } from 'web/lib/firebase/api' import { createPost } from 'web/lib/firebase/api'
import { Post } from 'common/post' import { Post } from 'common/post'
import { deletePost, updatePost } from 'web/lib/firebase/posts' import { deletePost, updatePost } from 'web/lib/firebase/posts'
import { useRouter } from 'next/router'
import { useState } from 'react' import { useState } from 'react'
import { usePost } from 'web/hooks/use-post'
export function GroupAboutPost(props: { export function GroupAboutPost(props: {
group: Group group: Group
isCreator: boolean isCreator: boolean
post: Post post: Post
}) { }) {
const { group, isCreator, post } = props const { group, isCreator } = props
const post = usePost(group.aboutPostId) ?? props.post
const isAdmin = useAdmin() const isAdmin = useAdmin()
if (group.aboutPostId == null && !isCreator) { if (group.aboutPostId == null && !isCreator) {
@ -41,7 +42,6 @@ function RichEditGroupAboutPost(props: { group: Group; post: Post }) {
const { group, post } = props const { group, post } = props
const [editing, setEditing] = useState(false) const [editing, setEditing] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)
const router = useRouter()
const { editor, upload } = useTextEditor({ const { editor, upload } = useTextEditor({
defaultValue: post.content, defaultValue: post.content,
@ -68,13 +68,11 @@ function RichEditGroupAboutPost(props: { group: Group; post: Post }) {
content: newPost.content, content: newPost.content,
}) })
} }
await router.replace(router.asPath)
} }
async function deleteGroupAboutPost() { async function deleteGroupAboutPost() {
await deletePost(post) await deletePost(post)
await deleteFieldFromGroup(group, 'aboutPostId') await deleteFieldFromGroup(group, 'aboutPostId')
await router.replace(router.asPath)
} }
return editing ? ( return editing ? (

13
web/hooks/use-post.ts Normal file
View File

@ -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<Post | null | undefined>()
useEffect(() => {
if (postId) return listenForPost(postId, setPost)
}, [postId])
return post
}

View File

@ -7,7 +7,7 @@ import {
where, where,
} from 'firebase/firestore' } from 'firebase/firestore'
import { Post } from 'common/post' import { Post } from 'common/post'
import { coll, getValue } from './utils' import { coll, getValue, listenForValue } from './utils'
export const posts = coll<Post>('posts') export const posts = coll<Post>('posts')
@ -32,3 +32,10 @@ export async function getPostBySlug(slug: string) {
const docs = (await getDocs(q)).docs const docs = (await getDocs(q)).docs
return docs.length === 0 ? null : docs[0].data() return docs.length === 0 ? null : docs[0].data()
} }
export function listenForPost(
postId: string,
setPost: (post: Post | null) => void
) {
return listenForValue(doc(posts, postId), setPost)
}

View File

@ -49,6 +49,7 @@ import { GroupAboutPost } from 'web/components/groups/group-about-post'
import { getPost } from 'web/lib/firebase/posts' import { getPost } from 'web/lib/firebase/posts'
import { Post } from 'common/post' import { Post } from 'common/post'
import { Spacer } from 'web/components/layout/spacer' import { Spacer } from 'web/components/layout/spacer'
import { usePost } from 'web/hooks/use-post'
export const getStaticProps = fromPropz(getStaticPropz) export const getStaticProps = fromPropz(getStaticPropz)
export async function getStaticPropz(props: { params: { slugs: string[] } }) { export async function getStaticPropz(props: { params: { slugs: string[] } }) {
@ -147,7 +148,6 @@ export default function GroupPage(props: {
topTraders, topTraders,
creatorScores, creatorScores,
topCreators, topCreators,
aboutPost,
} = props } = props
const router = useRouter() const router = useRouter()
@ -155,6 +155,7 @@ export default function GroupPage(props: {
const page = slugs?.[1] as typeof groupSubpages[number] const page = slugs?.[1] as typeof groupSubpages[number]
const group = useGroup(props.group?.id) ?? props.group const group = useGroup(props.group?.id) ?? props.group
const aboutPost = usePost(props.aboutPost?.id) ?? props.aboutPost
const user = useUser() const user = useUser()