generated from openmrs/openmrs-esm-template-app
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Add support for pre-filled questions (#70)
- Loading branch information
1 parent
25076e5
commit c98ac69
Showing
14 changed files
with
245 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/group-form-entry-workflow/configurable-questions/ConfigurableQuestionsSection.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React from "react"; | ||
import { TextInput, Select, SelectItem } from "@carbon/react"; | ||
import { FieldValues, UseFormRegister } from "react-hook-form"; | ||
import { SpecificQuestion } from "../../types"; | ||
|
||
interface ConfigurableQuestionsSectionProps { | ||
specificQuestions: Array<SpecificQuestion>; | ||
register?: UseFormRegister<FieldValues>; | ||
} | ||
|
||
const ConfigurableQuestionsSection: React.FC< | ||
ConfigurableQuestionsSectionProps | ||
> = ({ register, specificQuestions }) => { | ||
return ( | ||
<> | ||
{specificQuestions?.map((specificQuestion) => ( | ||
<div key={specificQuestion.question.id}> | ||
{specificQuestion?.answers?.length > 0 ? ( | ||
<Select | ||
{...register(specificQuestion.question.id, { required: false })} | ||
id={specificQuestion.question.id} | ||
labelText={specificQuestion.question.display} | ||
> | ||
<SelectItem value="" text="" /> | ||
{specificQuestion.answers.map((answer) => ( | ||
<SelectItem | ||
key={answer.value} | ||
value={answer.value} | ||
text={answer.display} | ||
/> | ||
))} | ||
</Select> | ||
) : ( | ||
<TextInput | ||
id={specificQuestion.question.id} | ||
{...register(specificQuestion.question.id, { required: false })} | ||
type="text" | ||
labelText={specificQuestion.question.display} | ||
/> | ||
)} | ||
</div> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
export default ConfigurableQuestionsSection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { type FetchResponse, openmrsFetch } from "@openmrs/esm-framework"; | ||
import useSWR from "swr"; | ||
import { SpecificQuestion, SpecificQuestionConfig } from "../types"; | ||
import { useMemo } from "react"; | ||
|
||
const formUrl = "/ws/rest/v1/o3/forms"; | ||
|
||
export const useSpecificQuestions = ( | ||
formUuid: string, | ||
specificQuestionConfig: Array<SpecificQuestionConfig> | ||
) => { | ||
const specificQuestionsToLoad = useMemo( | ||
() => getQuestionIdsByFormId(formUuid, specificQuestionConfig), | ||
[formUuid, specificQuestionConfig] | ||
); | ||
|
||
const { data, error } = useSWR<FetchResponse, Error>( | ||
specificQuestionsToLoad ? `${formUrl}/${formUuid}` : null, | ||
openmrsFetch | ||
); | ||
|
||
const specificQuestions = getQuestionsByIds( | ||
specificQuestionsToLoad, | ||
data?.data | ||
); | ||
|
||
return { | ||
questions: specificQuestions || null, | ||
isError: error, | ||
isLoading: !data && !error, | ||
}; | ||
}; | ||
|
||
function getQuestionIdsByFormId( | ||
formUuid: string, | ||
specificQuestionConfig: Array<SpecificQuestionConfig> | ||
) { | ||
const matchingQuestions = specificQuestionConfig.filter((question) => | ||
question.forms.includes(formUuid) | ||
); | ||
return matchingQuestions.map((question) => question.questionId); | ||
} | ||
|
||
function getQuestionsByIds(questionIds, formSchema): Array<SpecificQuestion> { | ||
if (!formSchema || questionIds.lenght <= 0) { | ||
return []; | ||
} | ||
const conceptLabels = formSchema.conceptReferences; | ||
return formSchema.pages.flatMap((page) => | ||
page.sections.flatMap((section) => | ||
section.questions | ||
.filter((question) => questionIds.includes(question.id)) | ||
.map((question) => ({ | ||
question: { | ||
display: | ||
question.label ?? | ||
conceptLabels[question.questionOptions.concept]?.display, | ||
id: question.id, | ||
}, | ||
answers: (question.questionOptions.answers ?? []).map((answer) => ({ | ||
value: answer.concept, | ||
display: answer.label ?? conceptLabels[answer.concept]?.display, | ||
})), | ||
})) | ||
) | ||
); | ||
} | ||
|
||
export default useSpecificQuestions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface Concept { | ||
uuid: string; | ||
display: string; | ||
} | ||
|
||
export interface SpecificQuestion { | ||
question: { | ||
id: string; | ||
display: string; | ||
}; | ||
answers: Array<{ | ||
value: string; | ||
display: string; | ||
}>; | ||
} | ||
|
||
export interface SpecificQuestionConfig { | ||
forms: Array<string>; | ||
questionId: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.