diff --git a/src/app/coaching-sessions/[id]/page.tsx b/src/app/coaching-sessions/[id]/page.tsx index 2f84b83..2bc2ddb 100644 --- a/src/app/coaching-sessions/[id]/page.tsx +++ b/src/app/coaching-sessions/[id]/page.tsx @@ -64,38 +64,32 @@ export default function CoachingSessionsPage() { useEffect(() => { async function fetchNote() { - if (!coachingSessionId) return; + if (!coachingSessionId) { + console.error( + "Failed to fetch Note since coachingSessionId is not set." + ); + return; + } await fetchNotesByCoachingSessionId(coachingSessionId) .then((notes) => { - // Apparently it's normal for this to be triggered twice in modern - // React versions in strict + development modes - // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar const note = notes[0]; - console.trace("note: " + noteToString(note)); - setNoteId(note.id); - setNote(note.body); + if (notes.length > 0) { + console.trace("note: " + noteToString(note)); + setNoteId(note.id); + setNote(note.body); + } else { + console.trace("No Notes associated with this coachingSessionId"); + } }) .catch((err) => { console.error( "Failed to fetch Note for current coaching session: " + err ); - - createNote(coachingSessionId, userId, "") - .then((note) => { - // Apparently it's normal for this to be triggered twice in modern - // React versions in strict + development modes - // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar - console.trace("New empty note: " + noteToString(note)); - setNoteId(note.id); - }) - .catch((err) => { - console.error("Failed to create new empty Note: " + err); - }); }); } fetchNote(); - }, [coachingSessionId, !note]); + }, [coachingSessionId, noteId]); const handleInputChange = (value: string) => { setNote(value); @@ -103,9 +97,6 @@ export default function CoachingSessionsPage() { if (noteId && coachingSessionId && userId) { updateNote(noteId, coachingSessionId, userId, value) .then((note) => { - // Apparently it's normal for this to be triggered twice in modern - // React versions in strict + development modes - // https://stackoverflow.com/questions/60618844/react-hooks-useeffect-is-called-twice-even-if-an-empty-array-is-used-as-an-ar console.trace("Updated Note: " + noteToString(note)); setSyncStatus("All changes saved"); }) @@ -113,6 +104,21 @@ export default function CoachingSessionsPage() { setSyncStatus("Failed to save changes"); console.error("Failed to update Note: " + err); }); + } else if (!noteId && coachingSessionId && userId) { + createNote(coachingSessionId, userId, value) + .then((note) => { + console.trace("Newly created Note: " + noteToString(note)); + setNoteId(note.id); + setSyncStatus("All changes saved"); + }) + .catch((err) => { + setSyncStatus("Failed to save changes"); + console.error("Failed to create new Note: " + err); + }); + } else { + console.error( + "Could not update or create a Note since coachingSessionId or userId are not set." + ); } }; diff --git a/src/lib/api/notes.ts b/src/lib/api/notes.ts index 0858671..2254d19 100644 --- a/src/lib/api/notes.ts +++ b/src/lib/api/notes.ts @@ -25,16 +25,11 @@ export const fetchNotesByCoachingSessionId = async ( }) .then(function (response: AxiosResponse) { // handle success - if (response?.status == 204) { - console.error("Retrieval of Note failed: no content."); - err = "Retrieval of Note failed: no content."; - } else { - var notes_data = response.data.data; - if (isNoteArray(notes_data)) { - notes_data.forEach((note_data: any) => { - notes.push(parseNote(note_data)) - }); - } + var notes_data = response.data.data; + if (isNoteArray(notes_data)) { + notes_data.forEach((note_data: any) => { + notes.push(parseNote(note_data)) + }); } }) .catch(function (error: AxiosError) { @@ -43,6 +38,9 @@ export const fetchNotesByCoachingSessionId = async ( if (error.response?.status == 401) { console.error("Retrieval of Note failed: unauthorized."); err = "Retrieval of Note failed: unauthorized."; + } else if (error.response?.status == 404) { + console.error("Retrieval of Note failed: Note by coaching session Id (" + coachingSessionId + ") not found."); + err = "Retrieval of Note failed: Note by coaching session Id (" + coachingSessionId + ") not found."; } else { console.log(error); console.error(