Skip to content

Commit

Permalink
Feat: Add ability to disable and set default values for answers in sp…
Browse files Browse the repository at this point in the history
…ecificQuestions (#84)

(feat) Add ability to disable and set default values for answers in specificQuestions

Co-authored-by: Jose Francisco <[email protected]>
  • Loading branch information
icrc-jofrancisco and Jose Francisco authored Sep 2, 2024
1 parent 79bdd85 commit 6ae1fda
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/group-form-entry-workflow/SessionDetailsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AttendanceTable } from './attendance-table';
import GroupFormWorkflowContext from '../context/GroupFormWorkflowContext';
import useGetPatients from '../hooks/useGetPatients';
import ConfigurableQuestionsSection from './configurable-questions/ConfigurableQuestionsSection';
import useSpecificQuestions from '../hooks/useForm';
import useSpecificQuestions from '../hooks/useSpecificQuestions';

interface ParamTypes {
formUuid?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const ConfigurableQuestionsSection: React.FC<ConfigurableQuestionsSectionProps>
{...register(specificQuestion.question.id, { required: false })}
id={specificQuestion.question.id}
labelText={specificQuestion.question.display}
readOnly={!!specificQuestion.question.disabled}
defaultValue={specificQuestion.question.defaultAnswer}
>
<SelectItem value="" text="" />
{specificQuestion.answers.map((answer) => (
Expand All @@ -30,6 +32,8 @@ const ConfigurableQuestionsSection: React.FC<ConfigurableQuestionsSectionProps>
{...register(specificQuestion.question.id, { required: false })}
type="text"
labelText={specificQuestion.question.display}
readOnly={!!specificQuestion.question.disabled}
defaultValue={specificQuestion.question.defaultAnswer}
/>
)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import useGetAllForms from './useGetAllForms';
import useGetPatient from './useGetPatient';
import useFormState from './useFormState';
import useGetEncounter from './useGetEncounter';
import useForm from './useForm';
import useSpecificQuestions from './useSpecificQuestions';

export { useGetAllForms, useGetPatient, useFormState, useGetEncounter, useForm };
export { useGetAllForms, useGetPatient, useFormState, useGetEncounter, useSpecificQuestions };
export * from './usePostEndpoint';
56 changes: 0 additions & 56 deletions src/hooks/useForm.ts

This file was deleted.

68 changes: 68 additions & 0 deletions src/hooks/useSpecificQuestions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { type FetchResponse, openmrsFetch, restBaseUrl } from '@openmrs/esm-framework';
import useSWR from 'swr';
import { type SpecificQuestion, type SpecificQuestionConfig } from '../types';
import { useMemo } from 'react';

const formUrl = `${restBaseUrl}/o3/forms`;

export const useSpecificQuestions = (formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) => {
const specificQuestionsToLoad = useMemo(
() => getQuestionByFormId(formUuid, specificQuestionConfig),
[formUuid, specificQuestionConfig],
);

const { data, error } = useSWR<FetchResponse, Error>(
specificQuestionsToLoad ? `${formUrl}/${formUuid}` : null,
openmrsFetch,
);

const specificQuestions = getQuestions(specificQuestionsToLoad, data?.data);

return {
questions: specificQuestions || null,
isError: error,
isLoading: !data && !error,
};
};

function getQuestionByFormId(formUuid: string, specificQuestionConfig: Array<SpecificQuestionConfig>) {
return specificQuestionConfig.filter((question) => question.forms.includes(formUuid));
}

function getQuestions(specificQuestions: Array<SpecificQuestionConfig>, formSchema): Array<SpecificQuestion> {
if (!formSchema || specificQuestions.length <= 0) {
return [];
}

const specificQuestionsMap = new Map<string, SpecificQuestionConfig>(
specificQuestions.map((sq) => [sq.questionId, sq]),
);

const questionIds = new Set(specificQuestionsMap.keys());
const conceptLabels = formSchema.conceptReferences;

return formSchema.pages.flatMap((page) =>
page.sections.flatMap((section) =>
section.questions
.filter((question) => questionIds.has(question.id))
.map((question) => {
const specificQuestion = specificQuestionsMap.get(question.id) || {};

return {
question: {
display: question.label ?? conceptLabels[question.questionOptions.concept]?.display,
id: question.id,
disabled: (specificQuestion as SpecificQuestionConfig).disabled,
defaultAnswer: (specificQuestion as SpecificQuestionConfig).defaultAnswer,
},
answers: (question.questionOptions.answers ?? []).map((answer) => ({
value: answer.concept,
display: answer.label ?? conceptLabels[answer.concept]?.display,
})),
};
}),
),
);
}

export default useSpecificQuestions;
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface SpecificQuestion {
question: {
id: string;
display: string;
defaultAnswer?: string;
disabled?: boolean;
};
answers: Array<{
value: string;
Expand All @@ -17,4 +19,6 @@ export interface SpecificQuestion {
export interface SpecificQuestionConfig {
forms: Array<string>;
questionId: string;
defaultAnswer?: string;
disabled?: boolean;
}

0 comments on commit 6ae1fda

Please sign in to comment.