Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Post Sorting and Timestamp Display in Your Posts Section #1095

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions app/(app)/my-posts/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ function classNames(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}

const renderDate = (label: string, date: string | Date) => (
<small>
{label} {new Date(date).toLocaleDateString()} at{" "}
{new Date(date).toLocaleTimeString(navigator.language, {
hour: "2-digit",
minute: "2-digit",
hour12: false,
})}
</small>
);

const MyPosts = () => {
const searchParams = useSearchParams();

Expand Down Expand Up @@ -113,7 +124,15 @@ 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;
Expand All @@ -140,31 +159,23 @@ const MyPosts = () => {
<div className="flex items-center">
<div className="flex-grow">
{published && postStatus === PostStatus.SCHEDULED ? (
<small>
Scheduled to publish on{" "}
{new Date(published).toLocaleDateString()} at{" "}
{new Date(published).toLocaleTimeString(
navigator.language,
{
hour: "2-digit",
minute: "2-digit",
hour12: false,
},
)}
</small>
<>
{renderDate("Scheduled to publish on ", published)}
</>
) : published && postStatus === PostStatus.PUBLISHED ? (
<small>
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 ? (
<>{renderDate("Last updated on ", updatedAt)}</>
) : (
<>{renderDate("Published on ", published)}</>
)}
</small>
</>
) : postStatus === PostStatus.DRAFT ? (
<>{renderDate("Last updated on ", updatedAt)}</>
) : null}
</div>

Expand Down
5 changes: 4 additions & 1 deletion server/api/router/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ export const postRouter = createTRPCRouter({
lte(posts.published, new Date().toISOString()),
eq(posts.userId, ctx?.session?.user?.id),
),
orderBy: (posts, { desc }) => [desc(posts.published)],
orderBy: (posts, { desc, sql }) => [
desc(sql`GREATEST(${posts.updatedAt}, ${posts.published})`),
],
});
}),
myScheduled: protectedProcedure.query(async ({ ctx }) => {
Expand All @@ -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
Expand Down