From b1d386ca5ad1c00e2f7eb1bc77f35296caa72e6f Mon Sep 17 00:00:00 2001 From: James Grugett Date: Thu, 6 Oct 2022 18:50:53 -0500 Subject: [PATCH] Listen for date doc changes --- web/hooks/use-post.ts | 14 ++++++++++++-- web/lib/firebase/posts.ts | 13 ++++++++++++- web/pages/date-docs/index.tsx | 4 +++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/web/hooks/use-post.ts b/web/hooks/use-post.ts index ff7bf6b9..1fd69888 100644 --- a/web/hooks/use-post.ts +++ b/web/hooks/use-post.ts @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react' -import { Post } from 'common/post' -import { listenForPost } from 'web/lib/firebase/posts' +import { DateDoc, Post } from 'common/post' +import { listenForDateDocs, listenForPost } from 'web/lib/firebase/posts' export const usePost = (postId: string | undefined) => { const [post, setPost] = useState() @@ -37,3 +37,13 @@ export const usePosts = (postIds: string[]) => { ) .sort((a, b) => b.createdTime - a.createdTime) } + +export const useDateDocs = () => { + const [dateDocs, setDateDocs] = useState() + + useEffect(() => { + return listenForDateDocs(setDateDocs) + }, []) + + return dateDocs +} diff --git a/web/lib/firebase/posts.ts b/web/lib/firebase/posts.ts index 22b9d095..343243cd 100644 --- a/web/lib/firebase/posts.ts +++ b/web/lib/firebase/posts.ts @@ -7,7 +7,13 @@ import { where, } from 'firebase/firestore' import { DateDoc, Post } from 'common/post' -import { coll, getValue, getValues, listenForValue } from './utils' +import { + coll, + getValue, + getValues, + listenForValue, + listenForValues, +} from './utils' import { getUserByUsername } from './users' export const posts = coll('posts') @@ -51,6 +57,11 @@ export async function getDateDocs() { return getValues(q) } +export function listenForDateDocs(setDateDocs: (dateDocs: DateDoc[]) => void) { + const q = query(posts, where('type', '==', 'date-doc')) + return listenForValues(q, setDateDocs) +} + export async function getDateDoc(username: string) { const user = await getUserByUsername(username) if (!user) return null diff --git a/web/pages/date-docs/index.tsx b/web/pages/date-docs/index.tsx index 48e0bb13..f25746ee 100644 --- a/web/pages/date-docs/index.tsx +++ b/web/pages/date-docs/index.tsx @@ -13,6 +13,7 @@ import { SiteLink } from 'web/components/site-link' import { getUser, User } from 'web/lib/firebase/users' import { DateDocPost } from './[username]' import { NoSEO } from 'web/components/NoSEO' +import { useDateDocs } from 'web/hooks/use-post' export async function getStaticProps() { const dateDocs = await getDateDocs() @@ -34,9 +35,10 @@ export default function DatePage(props: { dateDocs: DateDoc[] docCreators: User[] }) { - const { dateDocs, docCreators } = props + const { docCreators } = props const user = useUser() + const dateDocs = useDateDocs() ?? props.dateDocs const hasDoc = dateDocs.some((d) => d.creatorId === user?.id) return (