Skip to content

Commit

Permalink
Add key linkId for QuestionItem and fix evaluate method
Browse files Browse the repository at this point in the history
  • Loading branch information
vesnushka committed Nov 18, 2024
1 parent 699b788 commit e77648b
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions sdc-qrf/src/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,16 @@ export function QuestionItems(props: QuestionItemsProps) {

return (
<React.Fragment>
{getEnabledQuestions(questionItems, parentPath, formValues, context).map(
(item, index) => {
return (
<QuestionItem
key={index}
questionItem={item}
context={context}
parentPath={parentPath}
/>
);
},
)}
{getEnabledQuestions(questionItems, parentPath, formValues, context).map((item) => {
return (
<QuestionItem
key={item.linkId}
questionItem={item}
context={context}
parentPath={parentPath}
/>
);
})}
</React.Fragment>
);
}
Expand Down Expand Up @@ -103,10 +101,7 @@ export function QuestionItem(props: QuestionItemProps) {
_.get(formValues, fieldPath),
);

const itemContext = useMemo(
() => (isGroupItem(questionItem, context) ? context[0] : context),
[questionItem, context],
);
const itemContext = isGroupItem(questionItem, context) ? context[0] : context;

useEffect(() => {
// TODO: think about use cases for group context
Expand Down Expand Up @@ -160,7 +155,7 @@ export function QuestionItem(props: QuestionItemProps) {
'_text.cqfExpression',
itemContext,
cqfExpression,
) ?? initialQuestionItem.text;
)[0] ?? initialQuestionItem.text;

if (prevQuestionItem?.text !== calculatedValue) {
setQuestionItem((qi) => ({
Expand All @@ -179,7 +174,7 @@ export function QuestionItem(props: QuestionItemProps) {
'_readOnly.cqfExpression',
itemContext,
cqfExpression,
) ?? initialQuestionItem.readOnly;
)[0] ?? initialQuestionItem.readOnly;

if (prevQuestionItem?.readOnly !== calculatedValue) {
setQuestionItem((qi) => ({
Expand All @@ -198,7 +193,7 @@ export function QuestionItem(props: QuestionItemProps) {
'_required.cqfExpression',
itemContext,
cqfExpression,
) ?? initialQuestionItem.required;
)[0] ?? initialQuestionItem.required;

if (prevQuestionItem?.required !== calculatedValue) {
setQuestionItem((qi) => ({
Expand Down Expand Up @@ -325,16 +320,16 @@ export function evaluateQuestionItemExpression(
expression?: Expression,
) {
if (!expression) {
return;
return [];
}

if (expression.language !== 'text/fhirpath') {
console.error('Only fhirpath expression is supported');
return;
return [];
}

try {
return fhirpath.evaluate(context.context ?? {}, expression.expression!, context)[0];
return fhirpath.evaluate(context.context ?? {}, expression.expression!, context);
} catch (err: unknown) {
throw Error(`FHIRPath expression evaluation failure for ${linkId}.${path}: ${err}`);
}
Expand Down

0 comments on commit e77648b

Please sign in to comment.