@@ -64,55 +64,61 @@ export default function CoachingSessionsPage() {
64
64
65
65
useEffect ( ( ) => {
66
66
async function fetchNote ( ) {
67
- if ( ! coachingSessionId ) return ;
67
+ if ( ! coachingSessionId ) {
68
+ console . error (
69
+ "Failed to fetch Note since coachingSessionId is not set."
70
+ ) ;
71
+ return ;
72
+ }
68
73
69
74
await fetchNotesByCoachingSessionId ( coachingSessionId )
70
75
. then ( ( notes ) => {
71
- // Apparently it's normal for this to be triggered twice in modern
72
- // React versions in strict + development modes
73
- // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar
74
76
const note = notes [ 0 ] ;
75
- console . trace ( "note: " + noteToString ( note ) ) ;
76
- setNoteId ( note . id ) ;
77
- setNote ( note . body ) ;
77
+ if ( notes . length > 0 ) {
78
+ console . trace ( "note: " + noteToString ( note ) ) ;
79
+ setNoteId ( note . id ) ;
80
+ setNote ( note . body ) ;
81
+ } else {
82
+ console . trace ( "No Notes associated with this coachingSessionId" ) ;
83
+ }
78
84
} )
79
85
. catch ( ( err ) => {
80
86
console . error (
81
87
"Failed to fetch Note for current coaching session: " + err
82
88
) ;
83
-
84
- createNote ( coachingSessionId , userId , "" )
85
- . then ( ( note ) => {
86
- // Apparently it's normal for this to be triggered twice in modern
87
- // React versions in strict + development modes
88
- // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar
89
- console . trace ( "New empty note: " + noteToString ( note ) ) ;
90
- setNoteId ( note . id ) ;
91
- } )
92
- . catch ( ( err ) => {
93
- console . error ( "Failed to create new empty Note: " + err ) ;
94
- } ) ;
95
89
} ) ;
96
90
}
97
91
fetchNote ( ) ;
98
- } , [ coachingSessionId , ! note ] ) ;
92
+ } , [ coachingSessionId , noteId ] ) ;
99
93
100
94
const handleInputChange = ( value : string ) => {
101
95
setNote ( value ) ;
102
96
103
97
if ( noteId && coachingSessionId && userId ) {
104
98
updateNote ( noteId , coachingSessionId , userId , value )
105
99
. then ( ( note ) => {
106
- // Apparently it's normal for this to be triggered twice in modern
107
- // React versions in strict + development modes
108
- // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar
109
100
console . trace ( "Updated Note: " + noteToString ( note ) ) ;
110
101
setSyncStatus ( "All changes saved" ) ;
111
102
} )
112
103
. catch ( ( err ) => {
113
104
setSyncStatus ( "Failed to save changes" ) ;
114
105
console . error ( "Failed to update Note: " + err ) ;
115
106
} ) ;
107
+ } else if ( ! noteId && coachingSessionId && userId ) {
108
+ createNote ( coachingSessionId , userId , value )
109
+ . then ( ( note ) => {
110
+ console . trace ( "Newly created Note: " + noteToString ( note ) ) ;
111
+ setNoteId ( note . id ) ;
112
+ setSyncStatus ( "All changes saved" ) ;
113
+ } )
114
+ . catch ( ( err ) => {
115
+ setSyncStatus ( "Failed to save changes" ) ;
116
+ console . error ( "Failed to create new Note: " + err ) ;
117
+ } ) ;
118
+ } else {
119
+ console . error (
120
+ "Could not update or create a Note since coachingSessionId or userId are not set."
121
+ ) ;
116
122
}
117
123
} ;
118
124
0 commit comments