Skip to content

Commit

Permalink
Fix initial.valueBoolean parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
fongsean committed Jun 17, 2024
1 parent e224880 commit e2cc497
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@ import { checkIsDateTime, checkIsTime, convertDateTimeToDate } from './construct
export function parseItemInitialToAnswer(
initial: QuestionnaireItemInitial
): QuestionnaireResponseItemAnswer | null {
if (initial.valueBoolean) {
if (typeof initial.valueBoolean === 'boolean') {
return { valueBoolean: initial.valueBoolean };
}

if (initial.valueDecimal) {
if (typeof initial.valueDecimal === 'number') {
return { valueDecimal: initial.valueDecimal };
}

if (initial.valueInteger) {
if (typeof initial.valueInteger === 'number') {
return { valueInteger: initial.valueInteger };
}

if (initial.valueDate) {
if (typeof initial.valueDate === 'string') {
return { valueDate: initial.valueDate };
}

if (initial.valueDateTime) {
if (typeof initial.valueDateTime === 'string') {
return { valueDateTime: initial.valueDateTime };
}

if (initial.valueTime) {
if (typeof initial.valueTime === 'string') {
return { valueTime: initial.valueTime };
}

if (initial.valueString) {
if (typeof initial.valueString === 'string') {
return { valueString: initial.valueString };
}

if (initial.valueUri) {
if (typeof initial.valueUri === 'string') {
return { valueUri: initial.valueUri };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function BooleanItem(props: BooleanItemProps) {

const readOnly = useReadOnly(qItem, parentIsReadOnly);

const valueBoolean = qrItem?.answer && qrItem.answer[0].valueBoolean;
let valueBoolean: boolean | undefined = undefined;
if (qrItem?.answer?.[0]?.valueBoolean !== undefined) {
valueBoolean = qrItem.answer[0].valueBoolean;
}

// Process calculated expressions
const { calcExpUpdated } = useBooleanCalculatedExpression({
Expand Down
54 changes: 27 additions & 27 deletions packages/smart-forms-renderer/src/utils/initialise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function getInitialValueAnswers(qItem: QuestionnaireItem): QuestionnaireResponse
}

return initialValues
.map((initialValue) => initialValueSwitcher(initialValue))
.map((initialValue) => parseItemInitialToAnswer(initialValue))
.filter((item): item is QuestionnaireResponseItemAnswer => item !== null);
}

Expand All @@ -231,55 +231,55 @@ function getInitialValueAnswersFromRepeatGroup(qItem: QuestionnaireItem) {
.filter((childQRItem) => childQRItem.answer && childQRItem.answer.length > 0);
}

function initialValueSwitcher(
initialValue: QuestionnaireItemInitial
export function parseItemInitialToAnswer(
initial: QuestionnaireItemInitial
): QuestionnaireResponseItemAnswer | null {
if (initialValue.valueBoolean) {
return { valueBoolean: initialValue.valueBoolean };
if (typeof initial.valueBoolean === 'boolean') {
return { valueBoolean: initial.valueBoolean };
}

if (initialValue.valueDecimal) {
return { valueDecimal: initialValue.valueDecimal };
if (typeof initial.valueDecimal === 'number') {
return { valueDecimal: initial.valueDecimal };
}

if (initialValue.valueInteger) {
return { valueInteger: initialValue.valueInteger };
if (typeof initial.valueInteger === 'number') {
return { valueInteger: initial.valueInteger };
}

if (initialValue.valueDate) {
return { valueDate: initialValue.valueDate };
if (typeof initial.valueDate === 'string') {
return { valueDate: initial.valueDate };
}

if (initialValue.valueDateTime) {
return { valueDateTime: initialValue.valueDateTime };
if (typeof initial.valueDateTime === 'string') {
return { valueDateTime: initial.valueDateTime };
}

if (initialValue.valueTime) {
return { valueTime: initialValue.valueTime };
if (typeof initial.valueTime === 'string') {
return { valueTime: initial.valueTime };
}

if (initialValue.valueString) {
return { valueString: initialValue.valueString };
if (typeof initial.valueString === 'string') {
return { valueString: initial.valueString };
}

if (initialValue.valueUri) {
return { valueUri: initialValue.valueUri };
if (typeof initial.valueUri === 'string') {
return { valueUri: initial.valueUri };
}

if (initialValue.valueAttachment) {
return { valueAttachment: initialValue.valueAttachment };
if (initial.valueAttachment) {
return { valueAttachment: initial.valueAttachment };
}

if (initialValue.valueCoding) {
return { valueCoding: initialValue.valueCoding };
if (initial.valueCoding) {
return { valueCoding: initial.valueCoding };
}

if (initialValue.valueQuantity) {
return { valueQuantity: initialValue.valueQuantity };
if (initial.valueQuantity) {
return { valueQuantity: initial.valueQuantity };
}

if (initialValue.valueReference) {
return { valueReference: initialValue.valueReference };
if (initial.valueReference) {
return { valueReference: initial.valueReference };
}

return null;
Expand Down

0 comments on commit e2cc497

Please sign in to comment.