Skip to content

Commit 0c60f8d

Browse files
committed
Cover logic with unit tests, add types and fix errors
1 parent ab6c071 commit 0c60f8d

File tree

4 files changed

+1699
-236
lines changed

4 files changed

+1699
-236
lines changed

src/components.tsx

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Expression } from 'fhir/r4b';
2-
import fhirpath from 'fhirpath';
31
import _ from 'lodash';
42
import isEqual from 'lodash/isEqual';
53
import React, { PropsWithChildren, useEffect, useContext, useMemo, useRef, useState } from 'react';
@@ -9,7 +7,13 @@ import { FCEQuestionnaireItem } from './fce.types';
97
import { useQuestionnaireResponseFormContext } from '.';
108
import { QRFContext } from './context';
119
import { FormAnswerItems, ItemContext, QRFContextData, QuestionItemProps, QuestionItemsProps } from './types';
12-
import { calcContext, getBranchItems, getEnabledQuestions, wrapAnswerValue } from './utils.js';
10+
import {
11+
calcContext,
12+
evaluateQuestionItemExpression,
13+
getBranchItems,
14+
getEnabledQuestions,
15+
wrapAnswerValue,
16+
} from './utils.js';
1317

1418
function usePreviousValue<T = any>(value: T) {
1519
const prevValue = useRef<T | undefined>(value);
@@ -61,7 +65,7 @@ export function QuestionItem(props: QuestionItemProps) {
6165
? branchItems.qrItems.map((curQRItem) =>
6266
calcContext(initialContext, variable, branchItems.qItem, curQRItem),
6367
)
64-
: calcContext(initialContext, variable, branchItems.qItem, branchItems.qrItems[0]!);
68+
: calcContext(initialContext, variable, branchItems.qItem, branchItems.qrItems[0]);
6569
const prevAnswers: FormAnswerItems[] | undefined = usePreviousValue(_.get(formValues, fieldPath));
6670

6771
const itemContext = isGroupItem(questionItem, context) ? context[0] : context;
@@ -218,20 +222,3 @@ function isGroupItem(
218222
): context is ItemContext[] {
219223
return questionItem.type === 'group';
220224
}
221-
222-
function evaluateQuestionItemExpression(linkId: string, path: string, context: ItemContext, expression?: Expression) {
223-
if (!expression) {
224-
return [];
225-
}
226-
227-
if (expression.language !== 'text/fhirpath') {
228-
console.error('Only fhirpath expression is supported');
229-
return [];
230-
}
231-
232-
try {
233-
return fhirpath.evaluate(context.context ?? {}, expression.expression!, context, undefined, { async: false });
234-
} catch (err: unknown) {
235-
throw Error(`FHIRPath expression evaluation failure for ${linkId}.${path}: ${err}`);
236-
}
237-
}

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ interface NotRepeatableFormGroupItems {
8787
export type FormGroupItems = RepeatableFormGroupItems | NotRepeatableFormGroupItems;
8888

8989
export interface FormAnswerItems {
90-
value: AnswerValue | undefined;
90+
value?: { [x: string]: any };
9191
question?: string;
9292
items?: FormItems;
9393
}

0 commit comments

Comments
 (0)