Skip to content

Commit

Permalink
Merge pull request #26 from Jim-Hodapp-Coaching/fix_fetching_of_empty…
Browse files Browse the repository at this point in the history
…_notes
  • Loading branch information
jhodapp authored Aug 15, 2024
2 parents e117c16 + c3abefa commit 41f5f01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
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

0 comments on commit 41f5f01

Please sign in to comment.