@@ -12,9 +12,11 @@ import {
1212} from "@/components/ui/select" ;
1313import { getDateTimeFromString , Id } from "@/types/general" ;
1414import { useCoachingSessions } from "@/lib/api/coaching-sessions" ;
15- import { useEffect } from "react" ;
15+ import { useEffect , useState } from "react" ;
1616import { DateTime } from "ts-luxon" ;
1717import { useCoachingSessionStateStore } from "@/lib/providers/coaching-session-state-store-provider" ;
18+ import { fetchOverarchingGoalsByCoachingSessionId } from "@/lib/api/overarching-goals" ;
19+ import { OverarchingGoal } from "@/types/overarching-goal" ;
1820
1921interface CoachingSessionsSelectorProps extends PopoverProps {
2022 /// The CoachingRelationship Id for which to get a list of associated CoachingSessions
@@ -30,25 +32,47 @@ function CoachingSessionsSelectItems({
3032} : {
3133 relationshipId : Id ;
3234} ) {
33- const { coachingSessions, isLoading, isError } =
34- useCoachingSessions ( relationshipId ) ;
35+ const {
36+ coachingSessions,
37+ isLoading : isLoadingSessions ,
38+ isError : isErrorSessions ,
39+ } = useCoachingSessions ( relationshipId ) ;
40+
3541 const { setCurrentCoachingSessions } = useCoachingSessionStateStore (
3642 ( state ) => state
3743 ) ;
44+ const [ goals , setGoals ] = useState < ( OverarchingGoal [ ] | undefined ) [ ] > ( [ ] ) ;
45+ const [ isLoadingGoals , setIsLoadingGoals ] = useState ( false ) ;
3846
39- console . debug ( `coachingSessions: ${ JSON . stringify ( coachingSessions ) } ` ) ;
40-
41- // Be sure to cache the list of current coaching sessions in the CoachingSessionStateStore
4247 useEffect ( ( ) => {
4348 if ( ! coachingSessions . length ) return ;
44- console . debug (
45- `coachingSessions (useEffect): ${ JSON . stringify ( coachingSessions ) } `
46- ) ;
4749 setCurrentCoachingSessions ( coachingSessions ) ;
4850 } , [ coachingSessions ] ) ;
4951
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 > ;
5276 if ( ! coachingSessions ?. length ) return < div > No coaching sessions found</ div > ;
5377
5478 return (
@@ -62,13 +86,25 @@ function CoachingSessionsSelectItems({
6286 . filter (
6387 ( session ) => getDateTimeFromString ( session . date ) < DateTime . now ( )
6488 )
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+ } ) }
72108 </ SelectGroup >
73109 ) }
74110 { coachingSessions . some (
@@ -80,13 +116,25 @@ function CoachingSessionsSelectItems({
80116 . filter (
81117 ( session ) => getDateTimeFromString ( session . date ) >= DateTime . now ( )
82118 )
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+ } ) }
90138 </ SelectGroup >
91139 ) }
92140 </ >
@@ -105,23 +153,51 @@ export default function CoachingSessionSelector({
105153 getCurrentCoachingSession,
106154 } = useCoachingSessionStateStore ( ( state ) => state ) ;
107155
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+
108183 const handleSetCoachingSession = ( coachingSessionId : Id ) => {
109184 setCurrentCoachingSessionId ( coachingSessionId ) ;
110185 if ( onSelect ) {
111- onSelect ( relationshipId ) ;
186+ onSelect ( coachingSessionId ) ;
112187 }
113188 } ;
114189
115- const currentSession = currentCoachingSessionId
116- ? getCurrentCoachingSession ( currentCoachingSessionId )
117- : null ;
118-
119190 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 >
125201 ) : undefined ;
126202
127203 return (
0 commit comments