Skip to content

Commit 41f5f01

Browse files
authored
Merge pull request #26 from Jim-Hodapp-Coaching/fix_fetching_of_empty_notes
2 parents e117c16 + c3abefa commit 41f5f01

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

src/app/coaching-sessions/[id]/page.tsx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,61 @@ export default function CoachingSessionsPage() {
6464

6565
useEffect(() => {
6666
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+
}
6873

6974
await fetchNotesByCoachingSessionId(coachingSessionId)
7075
.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
7476
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+
}
7884
})
7985
.catch((err) => {
8086
console.error(
8187
"Failed to fetch Note for current coaching session: " + err
8288
);
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-
});
9589
});
9690
}
9791
fetchNote();
98-
}, [coachingSessionId, !note]);
92+
}, [coachingSessionId, noteId]);
9993

10094
const handleInputChange = (value: string) => {
10195
setNote(value);
10296

10397
if (noteId && coachingSessionId && userId) {
10498
updateNote(noteId, coachingSessionId, userId, value)
10599
.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
109100
console.trace("Updated Note: " + noteToString(note));
110101
setSyncStatus("All changes saved");
111102
})
112103
.catch((err) => {
113104
setSyncStatus("Failed to save changes");
114105
console.error("Failed to update Note: " + err);
115106
});
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+
);
116122
}
117123
};
118124

src/lib/api/notes.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ export const fetchNotesByCoachingSessionId = async (
2525
})
2626
.then(function (response: AxiosResponse) {
2727
// handle success
28-
if (response?.status == 204) {
29-
console.error("Retrieval of Note failed: no content.");
30-
err = "Retrieval of Note failed: no content.";
31-
} else {
32-
var notes_data = response.data.data;
33-
if (isNoteArray(notes_data)) {
34-
notes_data.forEach((note_data: any) => {
35-
notes.push(parseNote(note_data))
36-
});
37-
}
28+
var notes_data = response.data.data;
29+
if (isNoteArray(notes_data)) {
30+
notes_data.forEach((note_data: any) => {
31+
notes.push(parseNote(note_data))
32+
});
3833
}
3934
})
4035
.catch(function (error: AxiosError) {
@@ -43,6 +38,9 @@ export const fetchNotesByCoachingSessionId = async (
4338
if (error.response?.status == 401) {
4439
console.error("Retrieval of Note failed: unauthorized.");
4540
err = "Retrieval of Note failed: unauthorized.";
41+
} else if (error.response?.status == 404) {
42+
console.error("Retrieval of Note failed: Note by coaching session Id (" + coachingSessionId + ") not found.");
43+
err = "Retrieval of Note failed: Note by coaching session Id (" + coachingSessionId + ") not found.";
4644
} else {
4745
console.log(error);
4846
console.error(

0 commit comments

Comments
 (0)