From cb1070e024ed3da65f6434c495456cc636da9960 Mon Sep 17 00:00:00 2001 From: Harshit Tripathi Date: Tue, 8 Oct 2024 22:53:34 +0530 Subject: [PATCH 1/2] [fix] Post Sorting and Timestamp Display in Your Posts Section --- app/(app)/my-posts/_client.tsx | 48 ++++++++++++++++++++++++++-------- server/api/router/post.ts | 5 +++- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/app/(app)/my-posts/_client.tsx b/app/(app)/my-posts/_client.tsx index 49389e0d..0de8abb1 100644 --- a/app/(app)/my-posts/_client.tsx +++ b/app/(app)/my-posts/_client.tsx @@ -113,7 +113,7 @@ const MyPosts = () => { {selectedTabData.status === "success" && selectedTabData.data?.map( - ({ id, title, excerpt, readTimeMins, slug, published }) => { + ({ id, title, excerpt, readTimeMins, slug, published, updatedAt }) => { const postStatus = published ? getPostStatus(new Date(published)) : PostStatus.DRAFT; @@ -154,18 +154,44 @@ const MyPosts = () => { ) : published && postStatus === PostStatus.PUBLISHED ? ( - Published on{" "} - {new Date(published).toLocaleDateString()} at{" "} - {new Date(published).toLocaleTimeString( - navigator.language, - { - hour: "2-digit", - minute: "2-digit", - hour12: false, - }, + {/*If updatedAt is greater than published by more than on minutes show updated at else show published + as on updating published updatedAt is automatically updated and is greater than published*/} + {(new Date(updatedAt).getTime() - new Date(published).getTime()) >= 60000 ? ( + <> + {"Last updated on "} + {new Date(updatedAt).toLocaleDateString()} at{" "} + {new Date(updatedAt).toLocaleTimeString(navigator.language, { + hour: "2-digit", + minute: "2-digit", + hour12: false, + })} + + ) : ( + <> + {"Published on "} + {new Date(published).toLocaleDateString()} at{" "} + {new Date(published).toLocaleTimeString(navigator.language, { + hour: "2-digit", + minute: "2-digit", + hour12: false, + })} + )} - ) : null} + ): postStatus === PostStatus.DRAFT ? ( + + Last Updated on {" "} + {new Date(updatedAt).toLocaleDateString()} at{" "} + {new Date(updatedAt).toLocaleTimeString( + navigator.language, + { + hour: "2-digit", + minute: "2-digit", + hour12: false, + }, + )} + + ): null} [desc(posts.published)], + orderBy: (posts, { desc, sql }) => [ + desc(sql`GREATEST(${posts.updatedAt}, ${posts.published})`), + ], }); }), myScheduled: protectedProcedure.query(async ({ ctx }) => { @@ -413,6 +415,7 @@ export const postRouter = createTRPCRouter({ return ctx.db.query.post.findMany({ where: (posts, { eq }) => and(eq(posts.userId, ctx.session.user.id), isNull(posts.published)), + orderBy: (posts, { desc }) => [desc(posts.updatedAt)], }); }), editDraft: protectedProcedure From ca2b84ddc59b20def45ca5894a33add478ac348a Mon Sep 17 00:00:00 2001 From: Harshit Tripathi Date: Wed, 9 Oct 2024 01:10:20 +0530 Subject: [PATCH 2/2] [fix] Post Sorting and Timestamp Display in Your Posts Section --- app/(app)/my-posts/_client.tsx | 81 ++++++++++++++-------------------- 1 file changed, 33 insertions(+), 48 deletions(-) diff --git a/app/(app)/my-posts/_client.tsx b/app/(app)/my-posts/_client.tsx index 0de8abb1..0f50593d 100644 --- a/app/(app)/my-posts/_client.tsx +++ b/app/(app)/my-posts/_client.tsx @@ -24,6 +24,17 @@ function classNames(...classes: string[]) { return classes.filter(Boolean).join(" "); } +const renderDate = (label: string, date: string | Date) => ( + + {label} {new Date(date).toLocaleDateString()} at{" "} + {new Date(date).toLocaleTimeString(navigator.language, { + hour: "2-digit", + minute: "2-digit", + hour12: false, + })} + +); + const MyPosts = () => { const searchParams = useSearchParams(); @@ -113,7 +124,15 @@ const MyPosts = () => { {selectedTabData.status === "success" && selectedTabData.data?.map( - ({ id, title, excerpt, readTimeMins, slug, published, updatedAt }) => { + ({ + id, + title, + excerpt, + readTimeMins, + slug, + published, + updatedAt, + }) => { const postStatus = published ? getPostStatus(new Date(published)) : PostStatus.DRAFT; @@ -140,58 +159,24 @@ const MyPosts = () => {
{published && postStatus === PostStatus.SCHEDULED ? ( - - Scheduled to publish on{" "} - {new Date(published).toLocaleDateString()} at{" "} - {new Date(published).toLocaleTimeString( - navigator.language, - { - hour: "2-digit", - minute: "2-digit", - hour12: false, - }, - )} - + <> + {renderDate("Scheduled to publish on ", published)} + ) : published && postStatus === PostStatus.PUBLISHED ? ( - + <> {/*If updatedAt is greater than published by more than on minutes show updated at else show published as on updating published updatedAt is automatically updated and is greater than published*/} - {(new Date(updatedAt).getTime() - new Date(published).getTime()) >= 60000 ? ( - <> - {"Last updated on "} - {new Date(updatedAt).toLocaleDateString()} at{" "} - {new Date(updatedAt).toLocaleTimeString(navigator.language, { - hour: "2-digit", - minute: "2-digit", - hour12: false, - })} - + {new Date(updatedAt).getTime() - + new Date(published).getTime() >= + 60000 ? ( + <>{renderDate("Last updated on ", updatedAt)} ) : ( - <> - {"Published on "} - {new Date(published).toLocaleDateString()} at{" "} - {new Date(published).toLocaleTimeString(navigator.language, { - hour: "2-digit", - minute: "2-digit", - hour12: false, - })} - + <>{renderDate("Published on ", published)} )} - - ): postStatus === PostStatus.DRAFT ? ( - - Last Updated on {" "} - {new Date(updatedAt).toLocaleDateString()} at{" "} - {new Date(updatedAt).toLocaleTimeString( - navigator.language, - { - hour: "2-digit", - minute: "2-digit", - hour12: false, - }, - )} - - ): null} + + ) : postStatus === PostStatus.DRAFT ? ( + <>{renderDate("Last updated on ", updatedAt)} + ) : null}