1-
2-
31import { CoachingSession } from "@/types/coaching-session" ;
42import { Label } from "@/components/ui/label" ;
53import { Calendar } from "@/components/ui/calendar" ;
@@ -15,7 +13,6 @@ import { defaultCoachingSession } from "@/types/coaching-session";
1513
1614export type CoachingSessionFormMode = "create" | "update" ;
1715
18-
1916interface 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