Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions src/app/coaching-sessions/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,55 +64,61 @@ 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);

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");
})
.catch((err) => {
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."
);
}
};

Expand Down
18 changes: 8 additions & 10 deletions src/lib/api/notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down