From 8ae1166c49e5a1a96d81f7ba5d6215ca8b93546b Mon Sep 17 00:00:00 2001 From: Pico2x Date: Wed, 12 Oct 2022 16:42:28 +0100 Subject: [PATCH] Posts now have denormalized creator username and name --- common/post.ts | 4 ++++ functions/src/create-post.ts | 2 ++ web/components/post-card.tsx | 12 +++--------- web/pages/date-docs/create.tsx | 8 +++++++- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/common/post.ts b/common/post.ts index 77130a2c..ff90d614 100644 --- a/common/post.ts +++ b/common/post.ts @@ -8,6 +8,10 @@ export type Post = { creatorId: string // User id createdTime: number slug: string + + // denormalized user fields + creatorName: string + creatorUsername: string } export type DateDoc = Post & { diff --git a/functions/src/create-post.ts b/functions/src/create-post.ts index d1864ac2..83adeb73 100644 --- a/functions/src/create-post.ts +++ b/functions/src/create-post.ts @@ -100,6 +100,8 @@ export const createpost = newEndpoint({}, async (req, auth) => { createdTime: Date.now(), content: content, contractSlug, + creatorName: creator.name, + creatorUsername: creator.username, }) await postRef.create(post) diff --git a/web/components/post-card.tsx b/web/components/post-card.tsx index 19dce45a..50b378e6 100644 --- a/web/components/post-card.tsx +++ b/web/components/post-card.tsx @@ -3,7 +3,6 @@ import { DocumentIcon } from '@heroicons/react/solid' import clsx from 'clsx' import { Post } from 'common/post' import Link from 'next/link' -import { useUserById } from 'web/hooks/use-user' import { postPath } from 'web/lib/firebase/posts' import { fromNow } from 'web/lib/util/time' import { Avatar } from './avatar' @@ -17,13 +16,8 @@ export function PostCard(props: { highlightOptions?: CardHighlightOptions }) { const { post, onPostClick, highlightOptions } = props - const creatorId = post.creatorId - - const user = useUserById(creatorId) const { itemIds: itemIds, highlightClassName } = highlightOptions || {} - if (!user) return <> - return (
- +
{fromNow(post.createdTime)} diff --git a/web/pages/date-docs/create.tsx b/web/pages/date-docs/create.tsx index 639ed51c..6886ea2d 100644 --- a/web/pages/date-docs/create.tsx +++ b/web/pages/date-docs/create.tsx @@ -52,7 +52,13 @@ export default function CreateDateDocPage() { const newPost: Omit< DateDoc, - 'id' | 'creatorId' | 'createdTime' | 'slug' | 'contractSlug' + | 'id' + | 'creatorId' + | 'createdTime' + | 'slug' + | 'contractSlug' + | 'creatorUsername' + | 'creatorName' > & { question?: string } = removeUndefinedProps({ title, subtitle,