1
1
"use client" ;
2
2
3
- import { useState } from "react" ;
3
+ import { useEffect , useState } from "react" ;
4
4
import { ActionsList } from "@/components/ui/coaching-sessions/actions-list" ;
5
- import { ActionStatus , Id } from "@/types/general" ;
5
+ import { ItemStatus , Id } from "@/types/general" ;
6
6
import { Action } from "@/types/action" ;
7
7
import { AgreementsList } from "@/components/ui/coaching-sessions/agreements-list" ;
8
8
import { Agreement } from "@/types/agreement" ;
@@ -17,12 +17,24 @@ import { DateTime } from "ts-luxon";
17
17
import { siteConfig } from "@/site.config" ;
18
18
import { Tabs , TabsContent , TabsList , TabsTrigger } from "@/components/ui/tabs" ;
19
19
import { useAppStateStore } from "@/lib/providers/app-state-store-provider" ;
20
- import { OverarchingGoal } from "./overarching-goal" ;
20
+ import { OverarchingGoalComponent } from "./overarching-goal" ;
21
+ import {
22
+ createOverarchingGoal ,
23
+ fetchOverarchingGoalsByCoachingSessionId ,
24
+ updateOverarchingGoal ,
25
+ } from "@/lib/api/overarching-goals" ;
26
+ import {
27
+ defaultOverarchingGoal ,
28
+ OverarchingGoal ,
29
+ overarchingGoalToString ,
30
+ } from "@/types/overarching-goal" ;
21
31
22
32
const OverarchingGoalContainer : React . FC < {
23
33
userId : Id ;
24
34
} > = ( { userId } ) => {
25
35
const [ isOpen , setIsOpen ] = useState ( false ) ;
36
+ const [ goal , setGoal ] = useState < OverarchingGoal > ( defaultOverarchingGoal ( ) ) ;
37
+ const [ goalId , setGoalId ] = useState < Id > ( "" ) ;
26
38
const { coachingSession, coachingRelationship } = useAppStateStore (
27
39
( state ) => ( {
28
40
coachingSession : state . coachingSession ,
@@ -66,7 +78,7 @@ const OverarchingGoalContainer: React.FC<{
66
78
67
79
const handleActionAdded = (
68
80
body : string ,
69
- status : ActionStatus ,
81
+ status : ItemStatus ,
70
82
dueBy : DateTime
71
83
) : Promise < Action > => {
72
84
// Calls the backend endpoint that creates and stores a full Action entity
@@ -83,7 +95,7 @@ const OverarchingGoalContainer: React.FC<{
83
95
const handleActionEdited = (
84
96
id : Id ,
85
97
body : string ,
86
- status : ActionStatus ,
98
+ status : ItemStatus ,
87
99
dueBy : DateTime
88
100
) : Promise < Action > => {
89
101
return updateAction ( id , coachingSession . id , body , status , dueBy )
@@ -107,6 +119,84 @@ const OverarchingGoalContainer: React.FC<{
107
119
} ) ;
108
120
} ;
109
121
122
+ useEffect ( ( ) => {
123
+ async function fetchOverarchingGoal ( ) {
124
+ if ( ! coachingSession . id ) {
125
+ console . error (
126
+ "Failed to fetch Overarching Goal since coachingSession.id is not set."
127
+ ) ;
128
+ return ;
129
+ }
130
+
131
+ await fetchOverarchingGoalsByCoachingSessionId ( coachingSession . id )
132
+ . then ( ( goals ) => {
133
+ const goal = goals [ 0 ] ;
134
+ if ( goals . length > 0 ) {
135
+ console . trace ( "Overarching Goal: " + overarchingGoalToString ( goal ) ) ;
136
+ setGoalId ( goal . id ) ;
137
+ setGoal ( goal ) ;
138
+ } else {
139
+ console . trace (
140
+ "No Overarching Goals associated with this coachingSession.id"
141
+ ) ;
142
+ }
143
+ } )
144
+ . catch ( ( err ) => {
145
+ console . error (
146
+ "Failed to fetch Overarching Goal for current coaching session: " +
147
+ err
148
+ ) ;
149
+ } ) ;
150
+ }
151
+ fetchOverarchingGoal ( ) ;
152
+ } , [ coachingSession . id , goalId ] ) ;
153
+
154
+ const handleGoalChange = async ( newGoal : OverarchingGoal ) => {
155
+ console . trace ( "handleGoalChange (goal to set/update): " + newGoal . title ) ;
156
+
157
+ if ( goalId && coachingSession . id ) {
158
+ console . debug ( "Update existing Overarching Goal with id: " + goalId ) ;
159
+ updateOverarchingGoal (
160
+ goalId ,
161
+ coachingSession . id ,
162
+ newGoal . title ,
163
+ newGoal . body ,
164
+ newGoal . status
165
+ )
166
+ . then ( ( responseGoal ) => {
167
+ console . trace (
168
+ "Updated Overarching Goal: " + overarchingGoalToString ( responseGoal )
169
+ ) ;
170
+ setGoal ( responseGoal ) ;
171
+ } )
172
+ . catch ( ( err ) => {
173
+ console . error ( "Failed to update Overarching Goal: " + err ) ;
174
+ } ) ;
175
+ } else if ( ! goalId && coachingSession . id ) {
176
+ createOverarchingGoal (
177
+ coachingSession . id ,
178
+ newGoal . title ,
179
+ newGoal . body ,
180
+ newGoal . status
181
+ )
182
+ . then ( ( responseGoal ) => {
183
+ console . trace (
184
+ "Newly created Overarching Goal: " +
185
+ overarchingGoalToString ( responseGoal )
186
+ ) ;
187
+ setGoal ( responseGoal ) ;
188
+ setGoalId ( responseGoal . id ) ;
189
+ } )
190
+ . catch ( ( err ) => {
191
+ console . error ( "Failed to create new Overarching Goal: " + err ) ;
192
+ } ) ;
193
+ } else {
194
+ console . error (
195
+ "Could not update or create a Overarching Goal since coachingSession.id or userId are not set."
196
+ ) ;
197
+ }
198
+ } ;
199
+
110
200
return (
111
201
< div className = "grid grid-flow-row auto-rows-min gap-4" >
112
202
< div className = "row-span-1 pt-4" >
@@ -115,9 +205,11 @@ const OverarchingGoalContainer: React.FC<{
115
205
onOpenChange = { setIsOpen }
116
206
className = "w-full space-y-2"
117
207
>
118
- < OverarchingGoal
208
+ < OverarchingGoalComponent
209
+ initialValue = { goal }
119
210
onOpenChange = { ( open : boolean ) => setIsOpen ( open ) }
120
- > </ OverarchingGoal >
211
+ onGoalChange = { ( goal : OverarchingGoal ) => handleGoalChange ( goal ) }
212
+ > </ OverarchingGoalComponent >
121
213
< CollapsibleContent className = "px-4" >
122
214
< div className = "flex-col space-y-4 sm:flex" >
123
215
< div className = "grid flex-1 items-start gap-4 sm:py-0 md:gap-8" >
0 commit comments