11"use client" ;
22
33import React from "react" ;
4- import { useState } from "react" ;
54import { Button } from "@/components/ui/button" ;
65import {
76 Dialog ,
@@ -10,108 +9,41 @@ import {
109 DialogTitle ,
1110 DialogTrigger ,
1211} from "@/components/ui/dialog" ;
13- import { Label } from "@/components/ui/label" ;
14- import { Calendar } from "@/components/ui/calendar" ;
15- import { useCoachingRelationshipStateStore } from "@/lib/providers/coaching-relationship-state-store-provider" ;
16- import { getDateTimeFromString } from "@/types/general" ;
17- import {
18- CoachingSession ,
19- defaultCoachingSession ,
20- } from "@/types/coaching-session" ;
21- import {
22- useCoachingSessionList ,
23- useCoachingSessionMutation ,
24- } from "@/lib/api/coaching-sessions" ;
25- import { DateTime } from "ts-luxon" ;
12+ import { CoachingSessionFormMode } from "./coaching-session-form" ;
2613import { useAuthStore } from "@/lib/providers/auth-store-provider" ;
27- import { cn } from "@/components/lib/utils" ;
28- import { format } from "date-fns" ;
14+ import { useCoachingRelationshipStateStore } from "@/lib/providers/coaching-relationship-state-store-provider" ;
2915
3016interface CoachingSessionDialogProps {
3117 open : boolean ;
3218 onOpenChange : ( open : boolean ) => void ;
33- onCoachingSessionUpdated : ( ) => void ;
34- dialogTrigger ?: React . ReactElement < React . HTMLAttributes < HTMLButtonElement > > ;
35- existingSession ?: CoachingSession ;
36- mode : "create" | "update" ;
19+ mode : CoachingSessionFormMode ;
20+ children : React . ReactNode ;
3721}
3822
3923export function CoachingSessionDialog ( {
4024 open,
4125 onOpenChange,
42- onCoachingSessionUpdated,
43- dialogTrigger,
44- existingSession,
4526 mode,
27+ children,
4628} : CoachingSessionDialogProps ) {
29+
30+ const { isCoach } = useAuthStore ( ( state ) => state ) ;
4731 const { currentCoachingRelationshipId } = useCoachingRelationshipStateStore (
4832 ( state ) => state
4933 ) ;
50- const fromDate = DateTime . now ( ) . minus ( { month : 1 } ) ;
51- const toDate = DateTime . now ( ) . plus ( { month : 1 } ) ;
52- const { refresh } = useCoachingSessionList (
53- currentCoachingRelationshipId ,
54- fromDate ,
55- toDate
56- ) ;
57- const { create : createCoachingSession , update } =
58- useCoachingSessionMutation ( ) ;
59- const [ sessionDate , setSessionDate ] = useState < Date | undefined > (
60- existingSession ? new Date ( existingSession . date ) : undefined
61- ) ;
62- const [ sessionTime , setSessionTime ] = useState < string > (
63- existingSession ? format ( new Date ( existingSession . date ) , "HH:mm" ) : ""
64- ) ;
65- const { isCoach } = useAuthStore ( ( state ) => state ) ;
66-
67- const handleSubmit = async ( e : React . FormEvent ) => {
68- e . preventDefault ( ) ;
69- if ( ! sessionDate || ! sessionTime ) return ;
70-
71- const [ hours , minutes ] = sessionTime . split ( ":" ) . map ( Number ) ;
72- const dateTime = getDateTimeFromString ( sessionDate . toISOString ( ) )
73- . set ( { hour : hours , minute : minutes } )
74- . toFormat ( "yyyy-MM-dd'T'HH:mm:ss" ) ;
75-
76- try {
77- if ( mode === "create" ) {
78- const newCoachingSession : CoachingSession = {
79- ...defaultCoachingSession ( ) ,
80- coaching_relationship_id : currentCoachingRelationshipId ,
81- date : dateTime ,
82- } ;
83- await createCoachingSession ( newCoachingSession ) ;
84- } else if ( existingSession ) {
85- await update ( existingSession . id , {
86- ...existingSession ,
87- date : dateTime ,
88- updated_at : DateTime . now ( ) ,
89- } ) ;
90- }
91- refresh ( ) ;
92- onCoachingSessionUpdated ( ) ;
93- setSessionDate ( undefined ) ;
94- setSessionTime ( "" ) ;
95- onOpenChange ( false ) ;
96- } catch ( error ) {
97- console . error ( `Failed to ${ mode } coaching session:` , error ) ;
98- }
99- } ;
10034
10135 return (
10236 < Dialog open = { open } onOpenChange = { onOpenChange } >
103- { dialogTrigger && (
104- < DialogTrigger asChild >
105- { React . cloneElement ( dialogTrigger , {
106- ...( dialogTrigger . props as React . HTMLAttributes < HTMLButtonElement > ) ,
107- className : cn (
108- ( dialogTrigger . props as React . HTMLAttributes < HTMLButtonElement > )
109- . className
110- ) ,
111- } ) }
112- </ DialogTrigger >
113- ) }
114-
37+ < DialogTrigger asChild >
38+ < Button
39+ variant = "outline"
40+ size = "sm"
41+ className = "w-full sm:w-auto"
42+ disabled = { ! isCoach || ! currentCoachingRelationshipId }
43+ >
44+ Create New Coaching Session
45+ </ Button >
46+ </ DialogTrigger >
11547 < DialogContent >
11648 < DialogHeader >
11749 < DialogTitle >
@@ -120,30 +52,7 @@ export function CoachingSessionDialog({
12052 : "Update Coaching Session" }
12153 </ DialogTitle >
12254 </ DialogHeader >
123- < form onSubmit = { handleSubmit } className = "space-y-4" >
124- < div className = "space-y-2" >
125- < Label htmlFor = "session-date" > Session Date</ Label >
126- < Calendar
127- mode = "single"
128- selected = { sessionDate }
129- onSelect = { ( date ) => setSessionDate ( date ) }
130- />
131- </ div >
132- < div className = "space-y-2" >
133- < Label htmlFor = "session-time" > Session Time</ Label >
134- < input
135- type = "time"
136- id = "session-time"
137- value = { sessionTime }
138- onChange = { ( e ) => setSessionTime ( e . target . value ) }
139- className = "w-full border rounded p-2"
140- required
141- />
142- </ div >
143- < Button type = "submit" disabled = { ! sessionDate || ! sessionTime } >
144- { mode === "create" ? "Create Session" : "Update Session" }
145- </ Button >
146- </ form >
55+ { children }
14756 </ DialogContent >
14857 </ Dialog >
14958 ) ;
0 commit comments