From f2d1ecd0974f4cb8f766465fb8d66c2c0c778cf3 Mon Sep 17 00:00:00 2001 From: Ryan Pandya Date: Tue, 26 Nov 2024 10:26:57 -0800 Subject: [PATCH] Editable Mood Stars for Day --- apps/cli/src/commands/days.ts | 2 +- .../web/components/dashboard/EditableText.tsx | 7 ++ .../web/components/dashboard/days/DayView.tsx | 8 +- .../dashboard/days/EditableDayComment.tsx | 2 +- .../components/dashboard/days/MoodStars.tsx | 76 +++++++++++++++++++ packages/trpc/routers/days.ts | 4 +- 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 apps/web/components/dashboard/days/MoodStars.tsx diff --git a/apps/cli/src/commands/days.ts b/apps/cli/src/commands/days.ts index 32c9797..f9b978d 100644 --- a/apps/cli/src/commands/days.ts +++ b/apps/cli/src/commands/days.ts @@ -40,7 +40,7 @@ export const daysCmd = new Command() } else { const moodStr = moodToStars(day.mood); - const dateStr = format(`${day.date}T00:00:00`, "EEEE, MMMM do"); + const dateStr = format(day.date, "EEEE, MMMM do"); const data: string[][] = [[dateStr, moodStr], [day.comment ?? "No comment", '',]]; console.log(table(data, { diff --git a/apps/web/components/dashboard/EditableText.tsx b/apps/web/components/dashboard/EditableText.tsx index 94b1f26..f81033f 100644 --- a/apps/web/components/dashboard/EditableText.tsx +++ b/apps/web/components/dashboard/EditableText.tsx @@ -32,6 +32,13 @@ function EditMode({ if (ref.current) { ref.current.focus(); ref.current.textContent = originalText; + const range = document.createRange(); + range.selectNodeContents(ref.current); + const selection = window.getSelection(); + if (selection) { + selection.removeAllRanges(); + selection.addRange(range); + } } }, [ref]); diff --git a/apps/web/components/dashboard/days/DayView.tsx b/apps/web/components/dashboard/days/DayView.tsx index 1ac0865..a2c17bb 100644 --- a/apps/web/components/dashboard/days/DayView.tsx +++ b/apps/web/components/dashboard/days/DayView.tsx @@ -4,6 +4,7 @@ import { api } from "@/server/api/client"; import { getServerAuthSession } from "@/server/auth"; import { ZDay } from "@lifetracker/shared/types/days"; import EditableDayComment from "./EditableDayComment"; +import { MoodStars } from "./MoodStars"; import { format } from "date-fns"; export default async function DayView({ @@ -29,13 +30,14 @@ export default async function DayView({
- {format(`${day.date}T00:00:00`, "EEEE, MMMM do")} + {format(day.date, "EEEE, MMMM do")} - +
-
Rating: {day.mood?.toString() ?? "-"}
{ + toast({ + description: "Rating updated!", + }); + router.refresh(); + }, + }); + const setStars = (newStars: number) => { + if (day.mood == newStars) { + // Nothing to do here + return; + } + updateDay( + { + dateQuery: format(day.date, "yyyy-MM-dd"), + mood: newStars, + }, + { + onError: (e) => { + toast({ + description: e.message, + variant: "destructive", + }); + }, + }, + ); + }; + + return ( + +
+ {Array.from({ length: maximum }).map((_, i) => ( + setStars(i + 1)} + > + {i < day.mood + ? + : + } + + ))} +
+ + + Hi + + +
+ ) +} \ No newline at end of file diff --git a/packages/trpc/routers/days.ts b/packages/trpc/routers/days.ts index 65ce969..c61efb3 100644 --- a/packages/trpc/routers/days.ts +++ b/packages/trpc/routers/days.ts @@ -14,7 +14,6 @@ import { TZDate } from "@date-fns/tz"; function dateFromInput(input: { dateQuery: string }) { let t: string; - console.log(input.dateQuery); if (input.dateQuery == "today") { t = TZDate.tz("America/Los_Angeles"); } @@ -22,8 +21,7 @@ function dateFromInput(input: { dateQuery: string }) { t = new TZDate(input.dateQuery, "Etc/UTC"); } - console.log(format(t, "yyyy-MM-dd")); - return format(t, "yyyy-MM-dd"); + return format(t, "yyyy-MM-dd") + "T00:00:00"; } async function createDay(date: string, ctx: Context) {