Skip to content

Commit

Permalink
fix: lab value display
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmcgrath13 committed Nov 18, 2024
1 parent 70735e9 commit fb53555
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
39 changes: 31 additions & 8 deletions containers/ecr-viewer/src/app/services/formatService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { ToolTipElement } from "@/app/view-data/components/ToolTipElement";
import { ContactPoint } from "fhir/r4";
import { sanitizeAndMap } from "../view-data/utils/utils";
import parse from "html-react-parser";

interface Metadata {
[key: string]: string;
Expand All @@ -26,6 +27,8 @@ export interface TableJson {
tables?: TableRow[][];
}

export type TableValue = string | React.JSX.Element | React.JSX.Element[];

/**
* Formats a person's name using given name(s), family name, optional prefix(es), and optional suffix(es).
* @param given - Optional array of given name(s).
Expand Down Expand Up @@ -359,9 +362,7 @@ export function formatTablesToJSON(htmlString: string): TableJson[] {
const tables: any[] = [];
const resultId = getDataId(li);
const firstChildNode = getFirstNonCommentChild(li);
const resultName = firstChildNode
? getElementContent(firstChildNode)
: "";
const resultName = firstChildNode ? getElementText(firstChildNode) : "";
li.querySelectorAll("table").forEach((table) => {
tables.push(processTable(table));
});
Expand All @@ -375,7 +376,7 @@ export function formatTablesToJSON(htmlString: string): TableJson[] {
const tableWithCaptionArray = doc.querySelectorAll("table:has(caption)");
if (tableWithCaptionArray.length > 0) {
doc.querySelectorAll("table").forEach((table) => {
const resultName = getElementContent(table.caption as Node);
const resultName = getElementText(table.caption as Element);
const resultId = getDataId(table) ?? undefined;
jsonArray.push({ resultId, resultName, tables: [processTable(table)] });
});
Expand All @@ -387,7 +388,7 @@ export function formatTablesToJSON(htmlString: string): TableJson[] {
const contentArray = doc.querySelectorAll("content");
if (contentArray.length > 0) {
contentArray.forEach((content) => {
const resultName = getElementContent(content);
const resultName = getElementText(content);
const tables: any[] = [];
let sibling = content.nextElementSibling;

Expand Down Expand Up @@ -457,7 +458,7 @@ function processTable(table: Element): TableRow[] {
if (headers.length > 0) {
hasHeaders = true;
headers.forEach((header) => {
keys.push(getElementContent(header));
keys.push(getElementText(header));
});
}

Expand Down Expand Up @@ -489,8 +490,30 @@ function processTable(table: Element): TableRow[] {
return jsonArray;
}

function getElementContent(el: Node): string {
return sanitizeAndMap(el.textContent?.trim() ?? "");
/**
* Extracts the html content from an element and sanitizes and maps it so it is safe to render.
* @param el - An HTML element or node.
* @returns A sanitized and parsed snippet of JSX.
* @example @param el - <paragraph><!-- comment -->Values <content>here</content></paragraph>
* @example @returns - <p>Values <span>here</span></p>
*/
function getElementContent(el: Element | Node): TableValue {
const rawValue = (el as Element)?.innerHTML ?? el.textContent;
const value = rawValue?.trim() ?? "";
if (value === "") return value;
const res = parse(sanitizeAndMap(value));
return res;
}

/**
* Extracts the text content from an element and concatenates it.
* @param el - An HTML element or node.
* @returns A string with the text data.
* @example @param el - <paragraph><!-- comment -->Values <content>here</content></paragraph>
* @example @returns - 'Values here'
*/
function getElementText(el: Element | Node): string {
return el.textContent?.trim() ?? "";
}

/**
Expand Down
28 changes: 17 additions & 11 deletions containers/ecr-viewer/src/app/services/labsService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
formatAddress,
formatPhoneNumber,
TableJson,
TableValue,
} from "@/app/services/formatService";
import { Coding, ObservationComponent } from "fhir/r4b";
import EvaluateTable, {
Expand Down Expand Up @@ -119,15 +120,18 @@ export const checkAbnormalTag = (labReportJson: TableJson): boolean => {
* @example result - JSON object that contains the tables for all lab reports
* @example searchKey - Ex. "Analysis Time" or the field that we are searching data for.
*/
export function searchResultRecord(result: any[], searchKey: string) {
let resultsArray: any[] = [];
export function searchResultRecord(
result: any[],
searchKey: string,
): TableValue[] {
let resultsArray: TableValue[] = [];

// Loop through each table
for (const table of result) {
// For each table, recursively search through all nodes
if (Array.isArray(table)) {
const nestedResult: string = searchResultRecord(table, searchKey);
if (nestedResult) {
const nestedResult = searchResultRecord(table, searchKey);
if (nestedResult.length > 0) {
return nestedResult;
}
} else {
Expand All @@ -145,7 +149,7 @@ export function searchResultRecord(result: any[], searchKey: string) {
}
}
}
return [...new Set(resultsArray)].join(", ");
return [...new Set(resultsArray)];
}

/**
Expand Down Expand Up @@ -229,7 +233,7 @@ const returnReceivedTime = (
export const returnFieldValueFromLabHtmlString = (
labReportJson: TableJson,
fieldName: string,
): React.ReactNode => {
): React.ReactNode | TableValue[] => {
if (!labReportJson) {
return noData;
}
Expand Down Expand Up @@ -259,11 +263,13 @@ const returnAnalysisTime = (
return noData;
}

const analysisTimeArray =
typeof fieldVals === "string" ? fieldVals.split(", ") : [];
const analysisTimeArrayFormatted = analysisTimeArray.map((dateTime) => {
return formatDateTime(dateTime);
});
const analysisTimeArrayFormatted = (fieldVals as TableValue[]).map(
(dateTime) => {
const dateTimeNode = parse(`<p>${dateTime}</p>`);
console.log({ dateTime, typeofdate: typeof dateTimeNode, dateTimeNode });
return formatDateTime(dateTime);
},
);

return [...new Set(analysisTimeArrayFormatted)].join(", ");
};
Expand Down
8 changes: 8 additions & 0 deletions containers/ecr-viewer/src/app/tests/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -565,5 +565,13 @@ describe("Utils", () => {
`<p>hi there</p><span>I'm content</span><ul><li>one</li><li>two</li></ul>`,
);
});

it("should remove comments", () => {
const html = `<!-- Data ID: 123 --><paragraph>hi there</paragraph><content ID="abc">I'm content</content><list><item>one</item><item>two</item></list>`;
const actual = sanitizeAndMap(html);
expect(actual).toBe(
`<p>hi there</p><span>I'm content</span><ul><li>one</li><li>two</li></ul>`,
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const DataDisplay: React.FC<{
</div>
<div
className={classNames(
"grid-col maxw7 text-pre-line",
"grid-col maxw7 text-pre-line", // TODO: add back p-list
className,
item.className ? item.className : "",
)}
Expand Down

0 comments on commit fb53555

Please sign in to comment.