@@ -12,9 +12,11 @@ import {
12
12
} from "@/components/ui/select" ;
13
13
import { getDateTimeFromString , Id } from "@/types/general" ;
14
14
import { useCoachingSessions } from "@/lib/api/coaching-sessions" ;
15
- import { useEffect } from "react" ;
15
+ import { useEffect , useState } from "react" ;
16
16
import { DateTime } from "ts-luxon" ;
17
17
import { useCoachingSessionStateStore } from "@/lib/providers/coaching-session-state-store-provider" ;
18
+ import { fetchOverarchingGoalsByCoachingSessionId } from "@/lib/api/overarching-goals" ;
19
+ import { OverarchingGoal } from "@/types/overarching-goal" ;
18
20
19
21
interface CoachingSessionsSelectorProps extends PopoverProps {
20
22
/// The CoachingRelationship Id for which to get a list of associated CoachingSessions
@@ -30,25 +32,47 @@ function CoachingSessionsSelectItems({
30
32
} : {
31
33
relationshipId : Id ;
32
34
} ) {
33
- const { coachingSessions, isLoading, isError } =
34
- useCoachingSessions ( relationshipId ) ;
35
+ const {
36
+ coachingSessions,
37
+ isLoading : isLoadingSessions ,
38
+ isError : isErrorSessions ,
39
+ } = useCoachingSessions ( relationshipId ) ;
40
+
35
41
const { setCurrentCoachingSessions } = useCoachingSessionStateStore (
36
42
( state ) => state
37
43
) ;
44
+ const [ goals , setGoals ] = useState < ( OverarchingGoal [ ] | undefined ) [ ] > ( [ ] ) ;
45
+ const [ isLoadingGoals , setIsLoadingGoals ] = useState ( false ) ;
38
46
39
- console . debug ( `coachingSessions: ${ JSON . stringify ( coachingSessions ) } ` ) ;
40
-
41
- // Be sure to cache the list of current coaching sessions in the CoachingSessionStateStore
42
47
useEffect ( ( ) => {
43
48
if ( ! coachingSessions . length ) return ;
44
- console . debug (
45
- `coachingSessions (useEffect): ${ JSON . stringify ( coachingSessions ) } `
46
- ) ;
47
49
setCurrentCoachingSessions ( coachingSessions ) ;
48
50
} , [ coachingSessions ] ) ;
49
51
50
- if ( isLoading ) return < div > Loading...</ div > ;
51
- if ( isError ) return < div > Error loading coaching sessions</ div > ;
52
+ useEffect ( ( ) => {
53
+ const fetchGoals = async ( ) => {
54
+ setIsLoadingGoals ( true ) ;
55
+ try {
56
+ const sessionIds = coachingSessions ?. map ( ( session ) => session . id ) || [ ] ;
57
+ const goalsPromises = sessionIds . map ( ( id ) =>
58
+ fetchOverarchingGoalsByCoachingSessionId ( id )
59
+ ) ;
60
+ const fetchedGoals = await Promise . all ( goalsPromises ) ;
61
+ setGoals ( fetchedGoals ) ;
62
+ } catch ( error ) {
63
+ console . error ( "Error fetching goals:" , error ) ;
64
+ } finally {
65
+ setIsLoadingGoals ( false ) ;
66
+ }
67
+ } ;
68
+
69
+ if ( coachingSessions ?. length ) {
70
+ fetchGoals ( ) ;
71
+ }
72
+ } , [ coachingSessions ] ) ;
73
+
74
+ if ( isLoadingSessions || isLoadingGoals ) return < div > Loading...</ div > ;
75
+ if ( isErrorSessions ) return < div > Error loading coaching sessions</ div > ;
52
76
if ( ! coachingSessions ?. length ) return < div > No coaching sessions found</ div > ;
53
77
54
78
return (
@@ -62,13 +86,25 @@ function CoachingSessionsSelectItems({
62
86
. filter (
63
87
( session ) => getDateTimeFromString ( session . date ) < DateTime . now ( )
64
88
)
65
- . map ( ( session ) => (
66
- < SelectItem value = { session . id } key = { session . id } >
67
- { getDateTimeFromString ( session . date ) . toLocaleString (
68
- DateTime . DATETIME_FULL
69
- ) }
70
- </ SelectItem >
71
- ) ) }
89
+ . map ( ( session ) => {
90
+ const sessionIndex = coachingSessions . findIndex (
91
+ ( s ) => s . id === session . id
92
+ ) ;
93
+ return (
94
+ < SelectItem value = { session . id } key = { session . id } >
95
+ < div className = "flex flex-col w-full" >
96
+ < span className = "text-left truncate overflow-hidden" >
97
+ { goals [ sessionIndex ] ?. [ 0 ] ?. title || "No goal set" }
98
+ </ span >
99
+ < span className = "text-sm text-gray-400" >
100
+ { getDateTimeFromString ( session . date ) . toLocaleString (
101
+ DateTime . DATETIME_FULL
102
+ ) }
103
+ </ span >
104
+ </ div >
105
+ </ SelectItem >
106
+ ) ;
107
+ } ) }
72
108
</ SelectGroup >
73
109
) }
74
110
{ coachingSessions . some (
@@ -80,13 +116,25 @@ function CoachingSessionsSelectItems({
80
116
. filter (
81
117
( session ) => getDateTimeFromString ( session . date ) >= DateTime . now ( )
82
118
)
83
- . map ( ( session ) => (
84
- < SelectItem value = { session . id } key = { session . id } >
85
- { getDateTimeFromString ( session . date ) . toLocaleString (
86
- DateTime . DATETIME_FULL
87
- ) }
88
- </ SelectItem >
89
- ) ) }
119
+ . map ( ( session ) => {
120
+ const sessionIndex = coachingSessions . findIndex (
121
+ ( s ) => s . id === session . id
122
+ ) ;
123
+ return (
124
+ < SelectItem value = { session . id } key = { session . id } >
125
+ < div className = "flex flex-col w-full" >
126
+ < span className = "text-left truncate overflow-hidden" >
127
+ { goals [ sessionIndex ] ?. [ 0 ] ?. title || "No goal set" }
128
+ </ span >
129
+ < span className = "text-sm text-gray-400" >
130
+ { getDateTimeFromString ( session . date ) . toLocaleString (
131
+ DateTime . DATETIME_FULL
132
+ ) }
133
+ </ span >
134
+ </ div >
135
+ </ SelectItem >
136
+ ) ;
137
+ } ) }
90
138
</ SelectGroup >
91
139
) }
92
140
</ >
@@ -105,23 +153,51 @@ export default function CoachingSessionSelector({
105
153
getCurrentCoachingSession,
106
154
} = useCoachingSessionStateStore ( ( state ) => state ) ;
107
155
156
+ const [ currentGoal , setCurrentGoal ] = useState < OverarchingGoal | undefined > ( ) ;
157
+ const [ isLoadingGoal , setIsLoadingGoal ] = useState ( false ) ;
158
+
159
+ const currentSession = currentCoachingSessionId
160
+ ? getCurrentCoachingSession ( currentCoachingSessionId )
161
+ : null ;
162
+
163
+ useEffect ( ( ) => {
164
+ const fetchGoal = async ( ) => {
165
+ if ( ! currentCoachingSessionId ) return ;
166
+
167
+ setIsLoadingGoal ( true ) ;
168
+ try {
169
+ const goals = await fetchOverarchingGoalsByCoachingSessionId (
170
+ currentCoachingSessionId
171
+ ) ;
172
+ setCurrentGoal ( goals [ 0 ] ) ;
173
+ } catch ( error ) {
174
+ console . error ( "Error fetching goal:" , error ) ;
175
+ } finally {
176
+ setIsLoadingGoal ( false ) ;
177
+ }
178
+ } ;
179
+
180
+ fetchGoal ( ) ;
181
+ } , [ currentCoachingSessionId ] ) ;
182
+
108
183
const handleSetCoachingSession = ( coachingSessionId : Id ) => {
109
184
setCurrentCoachingSessionId ( coachingSessionId ) ;
110
185
if ( onSelect ) {
111
- onSelect ( relationshipId ) ;
186
+ onSelect ( coachingSessionId ) ;
112
187
}
113
188
} ;
114
189
115
- const currentSession = currentCoachingSessionId
116
- ? getCurrentCoachingSession ( currentCoachingSessionId )
117
- : null ;
118
-
119
190
const displayValue = currentSession ? (
120
- < >
121
- { getDateTimeFromString ( currentSession . date ) . toLocaleString (
122
- DateTime . DATETIME_FULL
123
- ) }
124
- </ >
191
+ < div className = "flex flex-col w-[28rem]" >
192
+ < span className = "truncate overflow-hidden text-left" >
193
+ { currentGoal ?. title || "No goal set" }
194
+ </ span >
195
+ < span className = "text-sm text-gray-500 text-left" >
196
+ { getDateTimeFromString ( currentSession . date ) . toLocaleString (
197
+ DateTime . DATETIME_FULL
198
+ ) }
199
+ </ span >
200
+ </ div >
125
201
) : undefined ;
126
202
127
203
return (
0 commit comments