@@ -36,66 +36,80 @@ import {
36
36
CoachingNotes ,
37
37
EditorRef ,
38
38
} from "@/components/ui/coaching-sessions/coaching-notes" ;
39
+ import { CoachingSessionSelector } from "@/components/ui/coaching-session-selector" ;
40
+ import { CoachingSession } from "@/types/coaching-session" ;
41
+ import { useRouter } from "next/navigation" ;
42
+ import { fetchCoachingSessions } from "@/lib/api/coaching-sessions" ;
39
43
40
44
// export const metadata: Metadata = {
41
45
// title: "Coaching Session",
42
46
// description: "Coaching session main page, where the good stuff happens.",
43
47
// };
44
48
45
49
export default function CoachingSessionsPage ( ) {
50
+ const router = useRouter ( ) ;
46
51
const [ note , setNote ] = useState < Note > ( defaultNote ( ) ) ;
47
52
const [ syncStatus , setSyncStatus ] = useState < string > ( "" ) ;
48
53
const { userId } = useAuthStore ( ( state ) => ( { userId : state . userId } ) ) ;
49
54
const { coachingSession } = useAppStateStore ( ( state ) => state ) ;
55
+ const { coachingSessionId, setCoachingSessionId } = useAppStateStore (
56
+ ( state ) => state
57
+ ) ;
58
+ const [ coachingSessions , setCoachingSessions ] = React . useState <
59
+ CoachingSession [ ]
60
+ > ( [ ] ) ;
61
+ const { relationshipId } = useAppStateStore ( ( state ) => state ) ;
50
62
const [ isLoading , setIsLoading ] = useState ( false ) ;
51
63
const editorRef = useRef < EditorRef > ( null ) ;
52
64
53
- async function fetchNote ( ) {
54
- if ( ! coachingSession . id ) {
55
- console . error (
56
- "Failed to fetch Note since coachingSession.id is not set."
57
- ) ;
58
- return ;
59
- }
60
- if ( isLoading ) {
61
- console . debug (
62
- "Not issuing a new Note fetch because a previous fetch is still in progress."
63
- ) ;
64
- }
65
+ const fetchNoteData = async ( ) => {
66
+ if ( isLoading ) return ;
65
67
66
68
setIsLoading ( true ) ;
69
+ try {
70
+ const notes = await fetchNotesByCoachingSessionId ( coachingSessionId ) ;
71
+ if ( notes . length > 0 ) {
72
+ setEditorContent ( notes [ 0 ] . body ) ;
73
+ setNote ( notes [ 0 ] ) ;
74
+ setSyncStatus ( "Notes refreshed" ) ;
75
+ setEditorFocussed ( ) ;
76
+ }
77
+ } catch ( err ) {
78
+ console . error ( `Failed to fetch Note: ${ err } ` ) ;
79
+ } finally {
80
+ setIsLoading ( false ) ;
81
+ }
82
+ } ;
67
83
68
- await fetchNotesByCoachingSessionId ( coachingSession . id )
69
- . then ( ( notes ) => {
70
- const note = notes [ 0 ] ;
71
- if ( notes . length > 0 ) {
72
- console . trace ( "Fetched note: " + noteToString ( note ) ) ;
73
- setEditorContent ( note . body ) ;
74
- setNote ( note ) ;
75
- setSyncStatus ( "Notes refreshed" ) ;
76
- setEditorFocussed ( ) ;
77
- } else {
78
- console . trace (
79
- "No Notes associated with this coachingSession.id: " +
80
- coachingSession . id
81
- ) ;
82
- }
83
- } )
84
- . catch ( ( err ) => {
85
- console . error (
86
- "Failed to fetch Note for current coaching session id: " +
87
- coachingSession . id +
88
- ". Error: " +
89
- err
90
- ) ;
91
- } ) ;
84
+ useEffect ( ( ) => {
85
+ if ( ! coachingSessionId ) return ;
92
86
93
- setIsLoading ( false ) ;
94
- }
87
+ fetchNoteData ( ) ;
88
+ } , [ coachingSessionId ] ) ; // Remove isLoading from dependencies
95
89
96
90
useEffect ( ( ) => {
97
- fetchNote ( ) ;
98
- } , [ coachingSession . id , isLoading ] ) ;
91
+ if ( ! relationshipId ) return ;
92
+
93
+ const loadCoachingSessions = async ( ) => {
94
+ if ( isLoading ) return ;
95
+
96
+ setIsLoading ( true ) ;
97
+
98
+ try {
99
+ const [ coachingSessions ] = await fetchCoachingSessions ( relationshipId ) ;
100
+ console . debug (
101
+ "setCoachingSessions: " + JSON . stringify ( coachingSessions )
102
+ ) ;
103
+ setCoachingSessions ( coachingSessions ) ;
104
+ } catch ( err ) {
105
+ console . error ( "Failed to fetch coaching sessions: " + err ) ;
106
+ } finally {
107
+ setIsLoading ( false ) ;
108
+ }
109
+ } ;
110
+
111
+ loadCoachingSessions ( ) ;
112
+ } , [ relationshipId ] ) ;
99
113
100
114
const setEditorContent = ( content : string ) => {
101
115
editorRef . current ?. setContent ( `${ content } ` ) ;
@@ -108,14 +122,14 @@ export default function CoachingSessionsPage() {
108
122
const handleOnChange = ( value : string ) => {
109
123
console . debug ( "isLoading (before update/create): " + isLoading ) ;
110
124
console . debug (
111
- "coachingSession.id (before update/create): " + coachingSession . id
125
+ "coachingSessionId (before update/create): " + coachingSessionId
112
126
) ;
113
127
console . debug ( "userId (before update/create): " + userId ) ;
114
128
console . debug ( "value (before update/create): " + value ) ;
115
129
console . debug ( "--------------------------------" ) ;
116
130
117
- if ( ! isLoading && note . id && coachingSession . id && userId ) {
118
- updateNote ( note . id , coachingSession . id , userId , value )
131
+ if ( ! isLoading && note . id && coachingSessionId && userId ) {
132
+ updateNote ( note . id , coachingSessionId , userId , value )
119
133
. then ( ( updatedNote ) => {
120
134
setNote ( updatedNote ) ;
121
135
console . trace ( "Updated Note: " + noteToString ( updatedNote ) ) ;
@@ -126,7 +140,7 @@ export default function CoachingSessionsPage() {
126
140
console . error ( "Failed to update Note: " + err ) ;
127
141
} ) ;
128
142
} else if ( ! isLoading && ! note . id && coachingSession . id && userId ) {
129
- createNote ( coachingSession . id , userId , value )
143
+ createNote ( coachingSessionId , userId , value )
130
144
. then ( ( createdNote ) => {
131
145
setNote ( createdNote ) ;
132
146
console . trace ( "Newly created Note: " + noteToString ( createdNote ) ) ;
@@ -151,6 +165,12 @@ export default function CoachingSessionsPage() {
151
165
document . title = sessionTitle ;
152
166
} ;
153
167
168
+ const handleCoachingSessionSelect = ( coachingSessionId : string ) => {
169
+ setCoachingSessionId ( coachingSessionId ) ;
170
+ console . debug ( "coachingSessionId selected: " + coachingSessionId ) ;
171
+ router . push ( `/coaching-sessions/${ coachingSessionId } ` ) ;
172
+ } ;
173
+
154
174
return (
155
175
< div className = "max-w-screen-2xl" >
156
176
< div className = "flex-col h-full md:flex " >
@@ -160,12 +180,12 @@ export default function CoachingSessionsPage() {
160
180
style = { siteConfig . titleStyle }
161
181
onRender = { handleTitleRender }
162
182
> </ CoachingSessionTitle >
163
- < div className = "ml-auto flex w-full space-x-2 sm:justify-end" >
164
- < PresetSelector current = { current } future = { future } past = { past } />
165
- { /* Hidden for MVP */ }
166
- < div className = "hidden" >
167
- < PresetActions />
168
- </ div >
183
+ < div className = "ml-auto flex w-[28rem] space-x-2 sm:justify-end" >
184
+ < CoachingSessionSelector
185
+ sessions = { coachingSessions }
186
+ placeholder = "Select a coaching session"
187
+ onSelect = { handleCoachingSessionSelect }
188
+ > </ CoachingSessionSelector >
169
189
</ div >
170
190
</ div >
171
191
</ div >
@@ -224,7 +244,11 @@ export default function CoachingSessionsPage() {
224
244
< div className = "flex items-center justify-between" >
225
245
< Label htmlFor = "refresh" > Notes Actions</ Label >
226
246
</ div >
227
- < Button id = "refresh" variant = "outline" onClick = { fetchNote } >
247
+ < Button
248
+ id = "refresh"
249
+ variant = "outline"
250
+ onClick = { fetchNoteData }
251
+ >
228
252
< SymbolIcon className = "mr-2 h-4 w-4" /> Refresh Notes
229
253
</ Button >
230
254
</ div >
0 commit comments