Skip to content

Commit

Permalink
revise type defs for additions / EnketoUserInputEntry
Browse files Browse the repository at this point in the history
- 'key' could be kept in 'data' or 'metadata' depending on if it is a processed or unprocessed entry (this is an inconsistency we may want to revisit later)
- handle possible null/ undefined parameters in functions: check for truthy inputs before executing
  • Loading branch information
JGreenlee committed Jan 9, 2024
1 parent 01f33d3 commit f1bd447
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
19 changes: 11 additions & 8 deletions www/js/survey/enketo/AddedNotesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ const AddedNotesList = ({ timelineEntry, additionEntries }: Props) => {
return dt;
}

function deleteEntry(entry) {
function deleteEntry(entry?: EnketoUserInputEntry) {
if (!entry) return;

const dataKey = entry.data.key || entry.metadata.key;
const data = entry.data;
const index = additionEntries.indexOf(entry);
Expand All @@ -74,34 +76,35 @@ const AddedNotesList = ({ timelineEntry, additionEntries }: Props) => {
return window['cordova'].plugins.BEMUserCache.putMessage(dataKey, data).then(() => {
additionEntries.splice(index, 1);
setConfirmDeleteModalVisible(false);
setEditingEntry(null);
setEditingEntry(undefined);
});
}

function confirmDeleteEntry(entry) {
function confirmDeleteEntry(entry: EnketoUserInputEntry) {
setEditingEntry(entry);
setConfirmDeleteModalVisible(true);
}

function dismissConfirmDelete() {
setEditingEntry(null);
setEditingEntry(undefined);
setConfirmDeleteModalVisible(false);
}

function editEntry(entry) {
setEditingEntry(entry);
console.debug('Editing entry is now ', entry);
setSurveyModalVisible(true);
}

async function onEditedResponse(response) {
async function onEditedResponse(response: EnketoUserInputEntry) {
if (!response) return;
await deleteEntry(editingEntry);
setEditingEntry(null);
setEditingEntry(undefined);
addUserInputToEntry(timelineEntry._id.$oid, response, 'note');
}

function onModalDismiss() {
setEditingEntry(null);
setEditingEntry(undefined);
setSurveyModalVisible(false);
}

Expand Down Expand Up @@ -144,7 +147,7 @@ const AddedNotesList = ({ timelineEntry, additionEntries }: Props) => {
opts={{
timelineEntry,
prefilledSurveyResponse: editingEntry?.data.xmlResponse,
dataKey: editingEntry?.key || editingEntry?.metadata?.key,
dataKey: editingEntry?.data?.key || editingEntry?.metadata?.key,
}}
/>
)}
Expand Down
1 change: 1 addition & 0 deletions www/js/survey/enketo/enketoHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type EnketoResponse = {
};

export type EnketoUserInputData = UserInputData & {
key?: string;
version: number;
xmlResponse: string;
jsonDocResponse: { [k: string]: any };
Expand Down

0 comments on commit f1bd447

Please sign in to comment.