From 5a6d9eada75256f71a0f3d2e4cfe2ac19952b0ea Mon Sep 17 00:00:00 2001 From: ialakey Date: Sun, 23 Jun 2024 11:51:02 +0200 Subject: [PATCH] #69 Restore functionality of the SDC-IDE --- web/package.json | 1 - .../group/RepeatableGroups/index.tsx | 95 +++++++++++-------- web/src/utils/questionnaire.ts | 10 +- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/web/package.json b/web/package.json index 6a12b457..2d0284e1 100644 --- a/web/package.json +++ b/web/package.json @@ -52,7 +52,6 @@ "react-select": "^5.4.0", "react-toastify": "^9.0.5", "sass": "^1.50.0", - "sdc-qrf": "*", "shared": "0.0.2", "ts-jest": "^27.1.4", "vitest": "^0.33.0", diff --git a/web/src/components/QRFormWrapper/components/group/RepeatableGroups/index.tsx b/web/src/components/QRFormWrapper/components/group/RepeatableGroups/index.tsx index efd00eb6..c14094da 100644 --- a/web/src/components/QRFormWrapper/components/group/RepeatableGroups/index.tsx +++ b/web/src/components/QRFormWrapper/components/group/RepeatableGroups/index.tsx @@ -1,6 +1,6 @@ import { GroupItemProps } from '@beda.software/fhir-questionnaire/vendor/sdc-qrf'; import React from 'react'; -import { useFormContext, useFieldArray } from 'react-hook-form'; +import { useForm, useFieldArray } from 'react-hook-form'; import s from './RepeatableGroups.module.scss'; import { QuestionItems } from '../../questionItems'; @@ -18,43 +18,46 @@ export function RepeatableGroups(props: RepeatableGroupsProps) { const baseFieldPath = [...parentPath, linkId]; const fieldName = baseFieldPath.join('.'); - const { control, watch } = useFormContext(); + const { control } = useForm({ + defaultValues: { + [fieldName]: { + items: required ? [{}] : [], + }, + }, + }); + const { fields, append, remove } = useFieldArray({ control, - name: fieldName, + name: `${fieldName}.items`, }); - const items = watch(fieldName) || (required ? [{}] : []); + const handleAddAnswer = () => { + append({}); + }; return (
- {fields.map((field, index) => { - if (!items[index]) { - return null; - } - - return renderGroup ? ( - - {renderGroup({ + {fields.map((item, index) => ( + + {renderGroup ? ( + renderGroup({ index, - field, + input: item, groupItem, - remove: () => remove(index), - })} - - ) : ( - remove(index)} - /> - ); - })} + }) + ) : ( + remove(index)} + /> + )} + + ))}
-
@@ -63,36 +66,42 @@ export function RepeatableGroups(props: RepeatableGroupsProps) { interface RepeatableGroupProps { index: number; - field: Record<'id', string>; + input: any; groupItem: GroupItemProps; - remove: () => void; + onRemove?: () => void; } function useRepeatableGroup(props: RepeatableGroupProps) { - const { index, groupItem, remove } = props; + const { index, input, groupItem } = props; const { parentPath, questionItem, context } = groupItem; const { linkId } = questionItem; + const onRemove = () => { + input.remove(index); + }; + return { - onRemove: remove, + onRemove, parentPath: [...parentPath, linkId, 'items', index.toString()], context: context[0]!, }; } export function RepeatableGroupDefault(props: RepeatableGroupProps) { - const { index, groupItem } = props; + const { index, groupItem, onRemove } = props; const { questionItem } = groupItem; const { item } = questionItem; - const { onRemove, parentPath, context } = useRepeatableGroup(props); + const { parentPath, context } = useRepeatableGroup(props); return ( <>
{`${questionItem.text} #${index + 1}`} - + {onRemove && ( + + )}
@@ -100,18 +109,20 @@ export function RepeatableGroupDefault(props: RepeatableGroupProps) { } export function RepeatableGroupRow(props: RepeatableGroupProps) { - const { groupItem } = props; + const { groupItem, onRemove } = props; const { questionItem } = groupItem; const { item } = questionItem; - const { onRemove, parentPath, context } = useRepeatableGroup(props); + const { parentPath, context } = useRepeatableGroup(props); return (
- + {onRemove && ( + + )}
); diff --git a/web/src/utils/questionnaire.ts b/web/src/utils/questionnaire.ts index 2c29300b..425a787d 100644 --- a/web/src/utils/questionnaire.ts +++ b/web/src/utils/questionnaire.ts @@ -12,7 +12,6 @@ import { } from 'shared/src/contrib/aidbox'; import { getByPath, setByPath } from 'shared/src/utils/path'; - // TODO: Write own type type AnswerValue = Required['value'] & Required['value']; @@ -322,9 +321,8 @@ export function mapResponseToForm( const answerPath = preparePathForAnswers(path, []); const questionPath = preparePathForQuestion(path); const question = getByPath(questionnaire, questionPath); - const answers: - | QuestionnaireResponseItemAnswer - | QuestionnaireResponseItemAnswer[] = getByPath(resource, answerPath); + const answers: QuestionnaireResponseItemAnswer | QuestionnaireResponseItemAnswer[] = + getByPath(resource, answerPath); if (typeof answers === 'undefined') { if (initial) { @@ -585,7 +583,6 @@ export function extractAnswersDisplay( ); } - export function getAnswerDisplay( o: QuestionnaireItemAnswerOption['value'] | QuestionnaireResponseItemAnswer['value'], ) { @@ -614,7 +611,8 @@ export function getAnswerCode( } if (o?.Reference) { - return o.Reference.id; + const ref = o.Reference as { id?: string; reference?: string }; + return ref.id ?? ref.reference ?? ''; } return JSON.stringify(o);