Skip to content

Commit 413981c

Browse files
committed
PR comments
1 parent 3b21b68 commit 413981c

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/components/ui/coaching-session.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,10 @@ import {
1717
import { MoreHorizontal } from "lucide-react";
1818
import { CoachingSessionDialog } from "@/components/ui/dashboard/coaching-session-dialog";
1919
import CoachingSessionForm, { CoachingSessionFormMode } from "@/components/ui/dashboard/coaching-session-form";
20-
import { DateTime } from "ts-luxon";
20+
import { CoachingSession as CoachingSessionType } from "@/types/coaching-session";
2121

2222
interface CoachingSessionProps {
23-
coachingSession: {
24-
id: Id;
25-
date: string;
26-
coaching_relationship_id: Id;
27-
created_at: DateTime;
28-
updated_at: DateTime;
29-
};
23+
coachingSession: CoachingSessionType;
3024
}
3125

3226
const CoachingSession: React.FC<CoachingSessionProps> = ({

src/components/ui/dashboard/coaching-session-form.tsx

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
import { CoachingSession } from "@/types/coaching-session";
42
import { Label } from "@/components/ui/label";
53
import { Calendar } from "@/components/ui/calendar";
@@ -15,7 +13,6 @@ import { defaultCoachingSession } from "@/types/coaching-session";
1513

1614
export type CoachingSessionFormMode = "create" | "update";
1715

18-
1916
interface CoachingSessionFormProps {
2017
existingSession?: CoachingSession;
2118
mode: CoachingSessionFormMode;
@@ -47,6 +44,30 @@ export default function CoachingSessionForm({
4744
existingSession ? format(new Date(existingSession.date), "HH:mm") : ""
4845
);
4946

47+
const resetForm = () => {
48+
setSessionDate(undefined);
49+
setSessionTime("");
50+
onOpenChange(false);
51+
};
52+
53+
const handleCreateSession = async (dateTime: string) => {
54+
const newCoachingSession: CoachingSession = {
55+
...defaultCoachingSession(),
56+
coaching_relationship_id: currentCoachingRelationshipId,
57+
date: dateTime,
58+
};
59+
await createCoachingSession(newCoachingSession);
60+
};
61+
62+
const handleUpdateSession = async (dateTime: string) => {
63+
if (!existingSession) return;
64+
await update(existingSession.id, {
65+
...existingSession,
66+
date: dateTime,
67+
updated_at: DateTime.now(),
68+
});
69+
};
70+
5071
const handleSubmit = async (e: React.FormEvent) => {
5172
e.preventDefault();
5273
if (!sessionDate || !sessionTime) return;
@@ -56,27 +77,16 @@ export default function CoachingSessionForm({
5677
.set({ hour: hours, minute: minutes })
5778
.toFormat("yyyy-MM-dd'T'HH:mm:ss");
5879

80+
const handler = mode === "create" ? handleCreateSession : handleUpdateSession;
81+
5982
try {
60-
if (mode === "create") {
61-
const newCoachingSession: CoachingSession = {
62-
...defaultCoachingSession(),
63-
coaching_relationship_id: currentCoachingRelationshipId,
64-
date: dateTime,
65-
};
66-
await createCoachingSession(newCoachingSession);
67-
} else if (existingSession) {
68-
await update(existingSession.id, {
69-
...existingSession,
70-
date: dateTime,
71-
updated_at: DateTime.now(),
72-
});
73-
}
83+
await handler(dateTime);
7484
refresh();
75-
setSessionDate(undefined);
76-
setSessionTime("");
77-
onOpenChange(false);
7885
} catch (error) {
86+
// TODO: We might want to show a toast here if/when we get that infrastructure in place
7987
console.error(`Failed to ${mode} coaching session:`, error);
88+
} finally {
89+
resetForm();
8090
}
8191
};
8292

0 commit comments

Comments
 (0)