Skip to content

Commit

Permalink
Fix answerOptions default to valueCoding, move helper functions into …
Browse files Browse the repository at this point in the history
…new helper package
  • Loading branch information
fongsean committed Dec 20, 2023
1 parent dec1e8d commit 8b90f17
Show file tree
Hide file tree
Showing 17 changed files with 200 additions and 54 deletions.
5 changes: 3 additions & 2 deletions apps/smart-forms-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"@aehrc/sdc-assemble": "^1.2.0",
"@aehrc/sdc-populate": "^1.3.0",
"@aehrc/smart-forms-renderer": "^0.10.5",
"@aehrc/sdc-populate": "^1.4.1",
"@aehrc/fhir-questionnaire-helpers": "^0.1.0",
"@aehrc/smart-forms-renderer": "^0.10.6",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.10.8",
"@fontsource/material-icons": "^5.0.7",
Expand Down
9 changes: 7 additions & 2 deletions apps/smart-forms-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import svgr from 'vite-plugin-svgr';
export default defineConfig({
plugins: [react(), svgr()],
optimizeDeps: {
include: ['@aehrc/sdc-assemble', '@aehrc/sdc-populate']
include: ['@aehrc/sdc-assemble', '@aehrc/sdc-populate', '@aehrc/fhir-questionnaire-helpers']
},
build: {
commonjsOptions: {
include: [/node_modules/, '@aehrc/sdc-assemble', '@aehrc/sdc-populate']
include: [
/node_modules/,
'@aehrc/sdc-assemble',
'@aehrc/sdc-populate',
'@aehrc/fhir-questionnaire-helpers'
]
}
},
resolve: { preserveSymlinks: true }
Expand Down
40 changes: 36 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/fhir-questionnaire-helpers/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib
3 changes: 3 additions & 0 deletions packages/fhir-questionnaire-helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# FHIR Questionnaire Helpers

A Typescript library for questionnaire helper functions used in Smart Forms, smart-forms-renderer, sdc-populate and sdc-assemble.
26 changes: 26 additions & 0 deletions packages/fhir-questionnaire-helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@aehrc/fhir-questionnaire-helpers",
"version": "0.1.0",
"main": "lib/index.js",
"scripts": {
"compile": "tsc",
"watch": "tsc -w",
"prepare": "npm run compile"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aehrc/smart-forms.git"
},
"author": "Sean Fong",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/aehrc/smart-forms/issues"
},
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"fhirpath": "^3.7.1"
},
"devDependencies": {
"@types/fhir": "^0.0.38"
}
}
56 changes: 56 additions & 0 deletions packages/fhir-questionnaire-helpers/src/answerOption.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023 Commonwealth Scientific and Industrial Research
* Organisation (CSIRO) ABN 41 687 119 230.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { QuestionnaireItemAnswerOption, QuestionnaireResponseItemAnswer } from 'fhir/r4';

/**
* Find and return corresponding answerOption based on selected answer in form
*
* @author Sean Fong
*/
export function findInAnswerOptions(
options: QuestionnaireItemAnswerOption[],
str: string
): QuestionnaireResponseItemAnswer | undefined {
for (const option of options) {
if (option.valueCoding) {
if (str === option.valueCoding.code) {
return {
valueCoding: option.valueCoding
};
}
}

if (option.valueString) {
if (str === option.valueString) {
return {
valueString: option.valueString
};
}
}

if (option.valueInteger) {
if (str === option.valueInteger.toString()) {
return {
valueInteger: option.valueInteger
};
}
}
}

return;
}
18 changes: 18 additions & 0 deletions packages/fhir-questionnaire-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2023 Commonwealth Scientific and Industrial Research
* Organisation (CSIRO) ABN 41 687 119 230.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export { findInAnswerOptions } from './answerOption';
30 changes: 30 additions & 0 deletions packages/fhir-questionnaire-helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"jsx": "react",
"sourceMap": true,
"allowJs": true,
"outDir": "lib",
"declaration": true,
"checkJs": true,
"resolveJsonModule": true,

"allowUnusedLabels": false,
"allowUnreachableCode": false,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedParameters": true
},
"include": ["src"],
"exclude": ["lib"]
}
8 changes: 5 additions & 3 deletions packages/sdc-populate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aehrc/sdc-populate",
"version": "1.3.0",
"version": "1.4.1",
"description": "Performs the $populate operation from the HL7 FHIR SDC (Structured Data Capture) specification: http://hl7.org/fhir/uv/sdc",
"main": "lib/index.js",
"scripts": {
Expand All @@ -20,10 +20,12 @@
},
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"@aehrc/fhir-questionnaire-helpers": "^0.1.0",
"dayjs": "^1.11.10",
"moment": "^2.29.4",
"fhir-sdc-helpers": "^0.1.0",
"fhirclient": "^2.5.2",
"fhirpath": "^3.7.1"
"fhirpath": "^3.7.1",
"moment": "^2.29.4"
},
"devDependencies": {
"@jest/globals": "^29.3.1",
Expand Down
9 changes: 3 additions & 6 deletions packages/sdc-populate/src/utils/constructResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import moment from 'moment';
import dayjs from 'dayjs';
import fhirpath from 'fhirpath';
import fhirpath_r4_model from 'fhirpath/fhir-context/r4';
import { findInAnswerOptions } from '@aehrc/fhir-questionnaire-helpers';

/**
* Constructs a questionnaireResponse recursively from a specified questionnaire, its subject and its initialExpressions
Expand Down Expand Up @@ -389,14 +390,10 @@ function itemIsHidden(item: QuestionnaireItem): boolean {

function parseValueToAnswer(qItem: QuestionnaireItem, value: any): QuestionnaireResponseItemAnswer {
if (qItem.answerOption) {
const answerOption = qItem.answerOption.find(
(option: QuestionnaireItemAnswerOption) => option.valueCoding?.code === value?.code
);
const answerOption = findInAnswerOptions(qItem.answerOption, value);

if (answerOption) {
return {
valueCoding: answerOption.valueCoding
};
return answerOption;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/smart-forms-renderer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aehrc/smart-forms-renderer",
"version": "0.10.5",
"version": "0.10.6",
"description": "FHIR Structured Data Captured (SDC) rendering engine for Smart Forms",
"main": "lib/index.js",
"scripts": {
Expand All @@ -24,6 +24,7 @@
},
"homepage": "https://github.com/aehrc/smart-forms#readme",
"dependencies": {
"@aehrc/fhir-questionnaire-helpers": "^0.1.0",
"@iconify/react": "^4.1.1",
"@types/fhir": "^0.0.38",
"dayjs": "^1.11.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import React from 'react';
import Grid from '@mui/material/Grid';
import type { ChoiceItemOrientation } from '../../../interfaces/choice.enum';
import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { findInAnswerOptions, getQrChoiceValue } from '../../../utils/choice';
import { getQrChoiceValue } from '../../../utils/choice';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
Expand All @@ -32,6 +32,7 @@ import DisplayInstructions from '../DisplayItem/DisplayInstructions';
import LabelWrapper from '../ItemParts/ItemLabelWrapper';
import ChoiceRadioAnswerOptionFields from './ChoiceRadioAnswerOptionFields';
import useReadOnly from '../../../hooks/useReadOnly';
import { findInAnswerOptions } from '@aehrc/fhir-questionnaire-helpers';

interface ChoiceRadioAnswerOptionItemProps
extends PropsWithQrItemChangeHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import React from 'react';
import Grid from '@mui/material/Grid';

import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import { findInAnswerOptions, getQrChoiceValue } from '../../../utils/choice';
import { getQrChoiceValue } from '../../../utils/choice';
import { createEmptyQrItem } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
Expand All @@ -33,6 +33,7 @@ import DisplayInstructions from '../DisplayItem/DisplayInstructions';
import LabelWrapper from '../ItemParts/ItemLabelWrapper';
import ChoiceSelectAnswerOptionFields from './ChoiceSelectAnswerOptionFields';
import useReadOnly from '../../../hooks/useReadOnly';
import { findInAnswerOptions } from '@aehrc/fhir-questionnaire-helpers';

interface ChoiceSelectAnswerOptionItemProps
extends PropsWithQrItemChangeHandler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createEmptyQrItem } from '../../../utils/qrItem';
import { getOpenLabelText } from '../../../utils/itemControl';
import { getOldOpenLabelAnswer } from '../../../utils/openChoice';
import { FullWidthFormComponentBox } from '../../Box.styles';
import { findInAnswerOptions, getQrChoiceValue } from '../../../utils/choice';
import { getQrChoiceValue } from '../../../utils/choice';
import useRenderingExtensions from '../../../hooks/useRenderingExtensions';
import type {
PropsWithIsRepeatedAttribute,
Expand All @@ -34,6 +34,7 @@ import DisplayInstructions from '../DisplayItem/DisplayInstructions';
import LabelWrapper from '../ItemParts/ItemLabelWrapper';
import OpenChoiceRadioAnswerOptionFields from './OpenChoiceRadioAnswerOptionFields';
import useReadOnly from '../../../hooks/useReadOnly';
import { findInAnswerOptions } from '@aehrc/fhir-questionnaire-helpers';

interface OpenChoiceRadioAnswerOptionItemProps
extends PropsWithQrItemChangeHandler,
Expand Down
Loading

0 comments on commit 8b90f17

Please sign in to comment.