Skip to content

Commit

Permalink
Refactor rendering of array of strings to utilise humanizeStrings u…
Browse files Browse the repository at this point in the history
…tility method (#8128)

* Refactor rendering of array of strings to utilise `humanizeStrings` utility method

* handle empty arrays gracefully
  • Loading branch information
rithviknishad authored Jul 16, 2024
1 parent 465f8e0 commit 4e32402
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 50 deletions.
7 changes: 2 additions & 5 deletions src/Common/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTranslation } from "react-i18next";
import GenericFilterBadge from "../../CAREUI/display/FilterBadge";
import PaginationComponent from "../../Components/Common/Pagination";
import useConfig from "./useConfig";
import { classNames } from "../../Utils/utils";
import { classNames, humanizeStrings } from "../../Utils/utils";
import FiltersCache from "../../Utils/FiltersCache";

export type FilterState = Record<string, unknown>;
Expand Down Expand Up @@ -87,10 +87,7 @@ export default function useFilters({
name={name}
value={
value === undefined
? paramKey
.map((k) => qParams[k])
.filter(Boolean)
.join(", ")
? humanizeStrings(paramKey.map((k) => qParams[k]).filter(Boolean))
: value
}
onRemove={() => removeFilters(paramKey)}
Expand Down
18 changes: 10 additions & 8 deletions src/Common/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { useEffect } from "react";
import { OptionsType } from "./constants";
import { humanizeStrings } from "../Utils/utils";

export interface statusType {
aborted?: boolean;
Expand Down Expand Up @@ -34,14 +35,15 @@ export const parseOptionId: (
options: readonly OptionsType[],
id: string | string[],
) => string = (options, id) => {
const textArray = options
.filter((option) => {
return id instanceof Array
? id.map((i) => String(i)).includes(String(option.id))
: String(option.id) === String(id);
})
.map((option) => option.text);
return textArray.join(", ");
return humanizeStrings(
options
.filter((option) => {
return id instanceof Array
? id.map((i) => String(i)).includes(String(option.id))
: String(option.id) === String(id);
})
.map((option) => option.text),
);
};

export const deepEqual = (x: any, y: any): boolean => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PrescriptionMultiDropdown } from "./PrescriptionMultiselect";
import CareIcon from "../../../CAREUI/icons/CareIcon";
import request from "../../../Utils/request/request";
import routes from "../../../Redux/api";
import { humanizeStrings } from "../../../Utils/utils";

export type InvestigationType = {
type?: string[];
Expand Down Expand Up @@ -86,9 +87,9 @@ export default function InvestigationBuilder(
return (
data?.results.map(
(investigation) =>
`${investigation.name} -- ${investigation.groups
.map((group) => ` ( ${group.name} ) `)
.join(", ")}`,
`${investigation.name} -- ${humanizeStrings(
investigation.groups.map((group) => ` ( ${group.name} ) `),
)}`,
) ?? []
);
};
Expand Down
11 changes: 8 additions & 3 deletions src/Components/DeathReport/DeathReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import TextFormField from "../Form/FormFields/TextFormField";
import TextAreaFormField from "../Form/FormFields/TextAreaFormField";
import DateFormField from "../Form/FormFields/DateFormField";
import PhoneNumberFormField from "../Form/FormFields/PhoneNumberFormField";
import { formatDateTime, patientAgeInYears } from "../../Utils/utils";
import {
formatDateTime,
humanizeStrings,
patientAgeInYears,
} from "../../Utils/utils";
import Page from "../Common/components/Page";
import Form from "../Form/Form";
import { useTranslation } from "react-i18next";
Expand Down Expand Up @@ -89,8 +93,9 @@ export default function PrintDeathReport(props: { id: string }) {
patientData.medical_history &&
patientData.medical_history.length
) {
const medHis = patientData.medical_history;
return medHis.map((item: any) => item.disease).join(", ");
return humanizeStrings(
patientData.medical_history.map((item: any) => item.disease),
);
} else {
return "None";
}
Expand Down
20 changes: 8 additions & 12 deletions src/Components/Facility/ConsultationDetails/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { lazy, useCallback, useState } from "react";
import DoctorVideoSlideover from "../DoctorVideoSlideover";
import { make as Link } from "../../Common/components/Link.bs";
import { PatientModel } from "../../Patient/models";
import { formatDateTime, relativeTime } from "../../../Utils/utils";
import {
formatDateTime,
humanizeStrings,
relativeTime,
} from "../../../Utils/utils";

import { navigate, useQueryParams } from "raviger";
import { useDispatch } from "react-redux";
Expand Down Expand Up @@ -93,8 +97,9 @@ export const ConsultationDetails = (props: any) => {

const getPatientComorbidities = (patientData: any) => {
if (patientData?.medical_history?.length) {
const medHis = patientData.medical_history;
return medHis.map((item: any) => item.disease).join(", ");
return humanizeStrings(
patientData.medical_history.map((item: any) => item.disease),
);
} else {
return "None";
}
Expand All @@ -113,15 +118,6 @@ export const ConsultationDetails = (props: any) => {
...res.data,
symptoms_text: "",
};
// if (res.data.symptoms?.length) {
// const symptoms = res.data.symptoms
// .filter((symptom: number) => symptom !== 9)
// .map((symptom: number) => {
// const option = symptomChoices.find((i) => i.id === symptom);
// return option ? option.text.toLowerCase() : symptom;
// });
// data.symptoms_text = symptoms.join(", ");
// }
if (facilityId != data.facility || patientId != data.patient) {
navigate(
`/facility/${data.facility}/patient/${data.patient}/consultation/${data?.id}`,
Expand Down
6 changes: 4 additions & 2 deletions src/Components/Facility/Consultations/Mews.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DailyRoundsModel } from "../../Patient/models";
import { formatDateTime } from "../../../Utils/utils";
import { formatDateTime, humanizeStrings } from "../../../Utils/utils";

const getRespScore = (value?: number) => {
if (typeof value !== "number") return;
Expand Down Expand Up @@ -80,7 +80,9 @@ export const Mews = ({ dailyRound }: { dailyRound: DailyRoundsModel }) => {
MEWS
</span>
<div className="tooltip-text tooltip-bottom w-48 -translate-x-1/2 translate-y-3 whitespace-pre-wrap text-xs font-medium lg:w-64">
<span className="font-bold">{(data as string[]).join(", ")}</span>{" "}
<span className="font-bold">
{humanizeStrings(data as string[])}
</span>{" "}
data is missing from the last log update.
<br /> Last Updated: {formatDateTime(dailyRound.modified_date)}
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Facility/DischargedPatientsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "../../Common/constants";
import CareIcon from "../../CAREUI/icons/CareIcon";
import RecordMeta from "../../CAREUI/display/RecordMeta";
import { formatPatientAge } from "../../Utils/utils";
import { formatPatientAge, humanizeStrings } from "../../Utils/utils";
import { useTranslation } from "react-i18next";
import SwitchTabs from "../Common/components/SwitchTabs";
import SortDropdownMenu from "../Common/SortDropdown";
Expand Down Expand Up @@ -360,7 +360,7 @@ const DischargedPatientsList = ({
value(
DIAGNOSES_FILTER_LABELS[key],
key,
getDiagnosisFilterValue(key).join(", "),
humanizeStrings(getDiagnosisFilterValue(key)),
),
),
badge("Declared Status", "is_declared_positive"),
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Medicine/MedicinePrescriptionSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { lazy } from "react";
import Timeline, { TimelineNode } from "../../CAREUI/display/Timeline";
import { MedibaseMedicine, Prescription } from "../Medicine/models";
import { useTranslation } from "react-i18next";
import { humanizeStrings } from "../../Utils/utils";

const Loading = lazy(() => import("../Common/Loading"));

Expand Down Expand Up @@ -310,7 +311,7 @@ export default function ConsultationMedicineLogs({

// If there are changes, add them to the changes array
if (changesForPrescription.length > 0 && !prevPrescription.discontinued) {
const message = `Changes: ${changesForPrescription.join(", ")}`;
const message = `Changes: ${humanizeStrings(changesForPrescription)}`;
changes.push({
prescriptionId: currentPrescription.id,
changeMessage: message,
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Patient/ManagePatients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import SortDropdownMenu from "../Common/SortDropdown";
import SwitchTabs from "../Common/components/SwitchTabs";
import {
formatPatientAge,
humanizeStrings,
isAntenatal,
parsePhoneNumber,
} from "../../Utils/utils.js";
Expand Down Expand Up @@ -1031,7 +1032,7 @@ export const PatientManager = () => {
value(
DIAGNOSES_FILTER_LABELS[key],
key,
getDiagnosisFilterValue(key).join(", "),
humanizeStrings(getDiagnosisFilterValue(key)),
),
),
badge("Declared Status", "is_declared_positive"),
Expand Down
13 changes: 3 additions & 10 deletions src/Components/Patient/PatientInfoCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
formatDate,
formatDateTime,
formatPatientAge,
humanizeStrings,
} from "../../Utils/utils.js";
import ABHAProfileModal from "../ABDM/ABHAProfileModal.js";
import LinkABHANumberModal from "../ABDM/LinkABHANumberModal.js";
Expand All @@ -45,16 +46,8 @@ import { AuthorizedForConsultationRelatedActions } from "../../CAREUI/misc/Autho
const formatSkills = (arr: SkillModel[]) => {
const skills = arr.map((skill) => skill.skill_object.name);

if (skills.length === 1) {
return skills[0];
}

if (skills.length === 2) {
return `${skills[0]} and ${skills[1]}`;
}

if (skills.length === 3) {
return `${skills[0]}, ${skills[1]} and ${skills[2]}`;
if (skills.length <= 3) {
return humanizeStrings(skills);
}

return `${skills[0]}, ${skills[1]} and ${skills.length - 2} other skills...`;
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Patient/SampleDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export const SampleDetails = ({ id }: DetailRoute) => {
<span className="font-semibold leading-relaxed">
Countries travelled:{" "}
</span>
{sampleDetails.patient_travel_history.join(", ")}
{sampleDetails.patient_travel_history}
</div>
)}
{sampleDetails?.sample_type && (
Expand Down
5 changes: 3 additions & 2 deletions src/Components/Patient/SamplePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { classNames, formatDateTime } from "../../Utils/utils";
import { classNames, formatDateTime, humanizeStrings } from "../../Utils/utils";

import { lazy } from "react";

Expand Down Expand Up @@ -331,7 +331,8 @@ export default function SampleReport(props: ISamplePreviewProps) {
{
title: "Symptoms",
value:
sampleData?.medical_conditions?.symptoms?.join(", "),
sampleData?.medical_conditions?.symptoms &&
humanizeStrings(sampleData.medical_conditions.symptoms),
},
{ title: "First Symptom", value: "............." },
{
Expand Down

0 comments on commit 4e32402

Please sign in to comment.