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 1 commit
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
48 changes: 37 additions & 11 deletions app/(app)/my-posts/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,18 +154,44 @@ const MyPosts = () => {
</small>
) : 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 ? (
<>
{"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,
})}
</>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor date rendering logic to improve maintainability

The rendering logic for displaying dates is duplicated and can be consolidated to reduce repetition. Consider abstracting this logic into a helper function or component.

Suggested refactor:

Define a helper function to render date and time:

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,
    })}
  </>
);

Update the rendering code:

{(new Date(updatedAt).getTime() - new Date(published).getTime()) >= ONE_MINUTE_IN_MS ? (
  <>
-   {"Last updated on "}
-   {new Date(updatedAt).toLocaleDateString()} at{" "}
-   {new Date(updatedAt).toLocaleTimeString(navigator.language, {
-     hour: "2-digit",
-     minute: "2-digit",
-     hour12: false,
-   })}
+   {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)}
  </>
)}

)}
</small>
) : null}
): postStatus === PostStatus.DRAFT ? (
<small>
Last Updated on {" "}
{new Date(updatedAt).toLocaleDateString()} at{" "}
{new Date(updatedAt).toLocaleTimeString(
navigator.language,
{
hour: "2-digit",
minute: "2-digit",
hour12: false,
},
)}
</small>
): null}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor date rendering logic in draft posts

Similar to the previous suggestion, the code for rendering the "Last Updated on" date in draft posts can also utilize the helper function to avoid duplication.

Update your code as follows:

<small>
-  Last Updated on {" "}
-  {new Date(updatedAt).toLocaleDateString()} at{" "}
-  {new Date(updatedAt).toLocaleTimeString(
-    navigator.language,
-    {
-      hour: "2-digit",
-      minute: "2-digit",
-      hour12: false,
-    },
-  )}
+  {renderDate("Last Updated on", updatedAt)}
</small>

Committable suggestion was skipped due to low confidence.

</div>

<Menu
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
Loading