Skip to content

Commit

Permalink
Add support for initialising calculated expression values into response
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Jul 31, 2023
1 parent a5d8db0 commit 1000f44
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { useMemo } from 'react';
import { Stack } from '@mui/material';
import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { getQrItemsIndex, mapQItemsIndex } from '../../utils';
import { updateLinkedItem } from '../../utils/qrItem.ts';
import { updateQrGroup } from '../../utils/qrItem.ts';
import type { PropsWithQrItemChangeHandler } from '../../types/renderProps.interface.ts';
import useQuestionnaireStore from '../../../../stores/useQuestionnaireStore.ts';
import FormBodySingleCollapsibleWrapper from './Collapsible/FormBodySingleCollapsibleWrapper.tsx';
Expand All @@ -46,7 +46,7 @@ function FormBodyCollapsibleWrapper(props: FormBodyCollapsibleProps) {
const qrItems = topLevelQRItem.item;

function handleQrGroupChange(qrItem: QuestionnaireResponseItem) {
updateLinkedItem(qrItem, null, topLevelQRItem, indexMap);
updateQrGroup(qrItem, null, topLevelQRItem, indexMap);
onQrItemChange(topLevelQRItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { TabContext, TabPanel } from '@mui/lab';
import { getQrItemsIndex, mapQItemsIndex } from '../../utils';
import GroupItem from './QFormComponents/GroupItem/GroupItem.tsx';
import { updateLinkedItem } from '../../utils/qrItem.ts';
import { updateQrGroup } from '../../utils/qrItem.ts';
import FormBodyTabList from './Tabs/FormBodyTabList.tsx';
import type { PropsWithQrItemChangeHandler } from '../../types/renderProps.interface.ts';
import useQuestionnaireStore from '../../../../stores/useQuestionnaireStore.ts';
Expand All @@ -46,7 +46,7 @@ function FormBodyTabbed(props: FormBodyTabbedProps) {
const qrItems = topLevelQRItem.item;

function handleQrGroupChange(qrItem: QuestionnaireResponseItem) {
updateLinkedItem(qrItem, null, topLevelQRItem, indexMap);
updateQrGroup(qrItem, null, topLevelQRItem, indexMap);
onQrItemChange(topLevelQRItem);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import type { PropsWithQrItemChangeHandler } from '../../../../types/renderProps.interface.ts';
import { createQrGroup, updateLinkedItem } from '../../../../utils/qrItem.ts';
import { createQrGroup, updateQrGroup } from '../../../../utils/qrItem.ts';
import useHidden from '../../../../hooks/useHidden.ts';
import { QGroupContainerBox } from '../../../../../../components/Box/Box.styles.tsx';
import { Divider, Paper, TableContainer, Typography } from '@mui/material';
Expand Down Expand Up @@ -63,7 +63,7 @@ function GridGroup(props: GridGroupProps) {
// Event Handlers
function handleRowChange(newQrItem: QuestionnaireResponseItem) {
const updatedQrGroup: QuestionnaireResponseItem = { ...qrGroup };
updateLinkedItem(newQrItem, null, updatedQrGroup, qItemsIndexMap);
updateQrGroup(newQrItem, null, updatedQrGroup, qItemsIndexMap);
onQrItemChange(updatedQrGroup);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import type { PropsWithQrItemChangeHandler } from '../../../../types/renderProps.interface.ts';
import { createQrGroup, updateLinkedItem } from '../../../../utils/qrItem.ts';
import { createQrGroup, updateQrGroup } from '../../../../utils/qrItem.ts';
import { GridAnswerTableCell, GridTextTableCell } from '../Tables/Table.styles.tsx';
import SingleItem from '../SingleItem/SingleItem.tsx';
import { getQrItemsIndex, mapQItemsIndex } from '../../../../utils';
Expand Down Expand Up @@ -46,7 +46,7 @@ function GridRow(props: GridRowProps) {

function handleQrRowItemChange(newQrRowItem: QuestionnaireResponseItem) {
const qrRow: QuestionnaireResponseItem = { ...row };
updateLinkedItem(newQrRowItem, null, qrRow, qItemsIndexMap);
updateQrGroup(newQrRowItem, null, qrRow, qItemsIndexMap);
onQrItemChange(qrRow);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { useMemo } from 'react';
import { getQrItemsIndex, mapQItemsIndex } from '../../../../utils';
import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { createQrGroup, updateLinkedItem } from '../../../../utils/qrItem.ts';
import { createQrGroup, updateQrGroup } from '../../../../utils/qrItem.ts';
import { QGroupContainerBox } from '../../../../../../components/Box/Box.styles.tsx';
import type {
PropsWithIsRepeatedAttribute,
Expand Down Expand Up @@ -70,13 +70,13 @@ function GroupItem(props: GroupItemProps) {
// Event Handlers
function handleQrItemChange(newQrItem: QuestionnaireResponseItem) {
const updatedQrGroup: QuestionnaireResponseItem = { ...qrGroup };
updateLinkedItem(newQrItem, null, updatedQrGroup, qItemsIndexMap);
updateQrGroup(newQrItem, null, updatedQrGroup, qItemsIndexMap);
onQrItemChange(updatedQrGroup);
}

function handleQrRepeatGroupChange(qrRepeatGroup: QrRepeatGroup) {
const updatedQrGroup: QuestionnaireResponseItem = { ...qrGroup };
updateLinkedItem(null, qrRepeatGroup, updatedQrGroup, qItemsIndexMap);
updateQrGroup(null, qrRepeatGroup, updatedQrGroup, qItemsIndexMap);
onQrItemChange(updatedQrGroup);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { createQrGroup, updateLinkedItem } from '../../../../utils/qrItem.ts';
import { createQrGroup, updateQrGroup } from '../../../../utils/qrItem.ts';
import SingleItem from '../SingleItem/SingleItem.tsx';
import { getQrItemsIndex } from '../../../../utils';
import { StandardTableCell } from './Table.styles.tsx';
Expand All @@ -41,7 +41,7 @@ function QItemGroupTableRow(props: Props) {

function handleQrRowItemChange(newQrRowItem: QuestionnaireResponseItem) {
const qrRow: QuestionnaireResponseItem = { ...row };
updateLinkedItem(newQrRowItem, null, qrRow, qItemsIndexMap);
updateQrGroup(newQrRowItem, null, qrRow, qItemsIndexMap);
onQrItemChange(qrRow);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function RendererLayout() {
patient &&
user &&
spinner.isLoading &&
(sourceQuestionnaire.contained || sourceResponse.extension) &&
(sourceQuestionnaire.contained || sourceQuestionnaire.extension) &&
hasNotBeenPopulated &&
!sourceResponse.id
) {
Expand All @@ -105,8 +105,8 @@ function RendererLayout() {
user,
encounter,
(populated: QuestionnaireResponse, hasWarnings: boolean) => {
populateResponse(populated);
updatePopulatedProperties(populated);
const updatedResponse = updatePopulatedProperties(populated);
populateResponse(updatedResponse);
setSpinner({ ...spinner, isLoading: false });
if (hasWarnings) {
enqueueSnackbar(
Expand Down
3 changes: 1 addition & 2 deletions apps/smart-forms-app/src/features/renderer/utils/qrItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {

import type { QrRepeatGroup } from '../types/repeatGroup.interface.ts';

// @ts-ignore
/**
* Create a questionnaireResponse from a given questionnaire form item
* A questionnaire form item is the first item of a questionnaire
Expand Down Expand Up @@ -135,7 +134,7 @@ export function createEmptyQrItemWithUnit(
*
* @author Sean Fong
*/
export function updateLinkedItem(
export function updateQrGroup(
newQrItem: QuestionnaireResponseItem | null,
newQrRepeatGroup: QrRepeatGroup | null,
qrGroup: QuestionnaireResponseItem,
Expand Down
25 changes: 21 additions & 4 deletions apps/smart-forms-app/src/stores/useQuestionnaireStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import type { Tabs } from '../features/renderer/types/tab.interface.ts';
import { updateItemAnswer } from '../utils/enableWhen.ts';
import { initialiseFormFromResponse } from '../utils/initaliseForm.ts';
import { evaluateUpdatedExpressions } from '../utils/fhirpath.ts';
import {
evaluateInitialCalculatedExpressions,
initialiseCalculatedExpressionValues
} from '../utils/calculatedExpressions.ts';

const emptyQuestionnaire: Questionnaire = {
resourceType: 'Questionnaire',
Expand Down Expand Up @@ -52,7 +56,7 @@ export interface QuestionnaireState {
toggleEnableWhenActivation: (isActivated: boolean) => void;
updateExpressions: (updatedResponse: QuestionnaireResponse) => void;
addCodingToCache: (valueSetUrl: string, codings: Coding[]) => void;
updatePopulatedProperties: (populatedResponse: QuestionnaireResponse) => void;
updatePopulatedProperties: (populatedResponse: QuestionnaireResponse) => QuestionnaireResponse;
}

const useQuestionnaireStore = create<QuestionnaireState>()((set, get) => ({
Expand Down Expand Up @@ -173,17 +177,28 @@ const useQuestionnaireStore = create<QuestionnaireState>()((set, get) => ({
}
})),
updatePopulatedProperties: (populatedResponse: QuestionnaireResponse) => {
const initialCalculatedExpressions = evaluateInitialCalculatedExpressions({
initialResponse: populatedResponse,
calculatedExpressions: get().calculatedExpressions,
variablesFhirPath: get().variables.fhirPathVariables
});

const updatedResponse = initialiseCalculatedExpressionValues(
get().sourceQuestionnaire,
populatedResponse,
initialCalculatedExpressions
);

const {
initialEnableWhenItems,
initialEnableWhenLinkedQuestions,
initialEnableWhenExpressions,
initialCalculatedExpressions,
firstVisibleTab
} = initialiseFormFromResponse({
questionnaireResponse: populatedResponse,
questionnaireResponse: updatedResponse,
enableWhenItems: get().enableWhenItems,
enableWhenExpressions: get().enableWhenExpressions,
calculatedExpressions: get().calculatedExpressions,
calculatedExpressions: initialCalculatedExpressions,
variablesFhirPath: get().variables.fhirPathVariables,
tabs: get().tabs
});
Expand All @@ -195,6 +210,8 @@ const useQuestionnaireStore = create<QuestionnaireState>()((set, get) => ({
calculatedExpressions: initialCalculatedExpressions,
currentTabIndex: firstVisibleTab
}));

return updatedResponse;
}
}));

Expand Down
Loading

0 comments on commit 1000f44

Please sign in to comment.