Skip to content

Commit

Permalink
use updated patient age formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Mar 14, 2024
1 parent 3be630a commit d115c07
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 44 deletions.
4 changes: 2 additions & 2 deletions cypress/pageobject/Patient/PatientCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ export class PatientPage {
cy.get("#date-input").click().type(dateOfBirth);
}
typePatientAge(age: string) {
cy.get("#select-menu").click();
cy.get("#select-menu").contains("Age").click();
cy.get("#age-input-type-selector").click();
cy.get("#age-input-type-selector").contains("Age").click();
cy.get("button").contains("Confirm").click();
cy.get("#age-div").click().type(age);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import useVitalsAspectRatioConfig from "../../VitalsMonitor/useVitalsAspectRatio
import { DISCHARGE_REASONS, SYMPTOM_CHOICES } from "../../../Common/constants";
import PrescriptionsTable from "../../Medicine/PrescriptionsTable";
import Chip from "../../../CAREUI/display/Chip";
import { formatAge, formatDate, formatDateTime } from "../../../Utils/utils";
import {
formatDate,
formatDateTime,
formatPatientAge,
} from "../../../Utils/utils";
import ReadMore from "../../Common/components/Readmore";
import DailyRoundsList from "../Consultations/DailyRoundsList";
import EventsList from "./Events/EventsList";
Expand Down Expand Up @@ -629,12 +633,7 @@ export const ConsultationUpdatesTab = (props: ConsultationTabProps) => {
<div>
Age {" - "}
<span className="font-semibold">
{props.patientData.age !== undefined // 0 is a valid age, so we need to check for undefined
? formatAge(
props.patientData.age,
props.patientData.date_of_birth
)
: "-"}
{formatPatientAge(props.patientData)}
</span>
</div>
<div id="patient-weight">
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 @@ -7,10 +7,10 @@ import { PatientModel } from "../Patient/models";
import useQuery from "../../Utils/request/useQuery";
import { debounce } from "lodash-es";
import SearchInput from "../Form/SearchInput";
import { formatAge } from "../../Utils/utils";
import { GENDER_TYPES } from "../../Common/constants";
import CareIcon from "../../CAREUI/icons/CareIcon";
import RecordMeta from "../../CAREUI/display/RecordMeta";
import { formatPatientAge } from "../../Utils/utils";

const DischargedPatientsList = ({
facility_external_id,
Expand Down Expand Up @@ -88,7 +88,7 @@ const PatientListItem = ({ patient }: { patient: PatientModel }) => {
<h2 className="text-lg font-bold text-black">{patient.name}</h2>
<span className="text-sm font-medium text-gray-800">
{GENDER_TYPES.find((g) => g.id === patient.gender)?.text} -{" "}
{formatAge(patient.age, patient.date_of_birth)}
{formatPatientAge(patient)}
</span>
<div className="flex-1" />
<RecordMeta
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Patient/ManagePatients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import RecordMeta from "../../CAREUI/display/RecordMeta";
import SearchInput from "../Form/SearchInput";
import SortDropdownMenu from "../Common/SortDropdown";
import SwitchTabs from "../Common/components/SwitchTabs";
import { formatAge, parsePhoneNumber } from "../../Utils/utils.js";
import { formatPatientAge, parsePhoneNumber } from "../../Utils/utils.js";
import useFilters from "../../Common/hooks/useFilters";
import { useTranslation } from "react-i18next";
import Page from "../Common/components/Page.js";
Expand Down Expand Up @@ -526,7 +526,7 @@ export const PatientManager = () => {
>
<span className="text-xl capitalize">{patient.name}</span>
<span className="text-gray-800">
{formatAge(patient.age, patient.date_of_birth, true)}
{formatPatientAge(patient, true)}
</span>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/Components/Patient/PatientRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ export const PatientRegister = (props: PatientRegisterProps) => {
</FieldLabel>
<div className="flex w-full items-center gap-2">
<SelectMenuV2
id="age-input-type-selector"
className="w-32"
options={
[
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Patient/SampleDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Card from "../../CAREUI/display/Card";
import { FileUpload } from "./FileUpload";
import Page from "../Common/components/Page";
import _ from "lodash-es";
import { formatAge, formatDateTime } from "../../Utils/utils";
import { formatDateTime, formatPatientAge } from "../../Utils/utils";

import { navigate } from "raviger";
import { DetailRoute } from "../../Routers/types";
Expand Down Expand Up @@ -102,7 +102,7 @@ export const SampleDetails = ({ id }: DetailRoute) => {
) : (
<div>
<span className="font-semibold leading-relaxed">Age: </span>
{formatAge(patientData?.age, patientData?.date_of_birth)}
{formatPatientAge(patientData)}
</div>
)}
<div>
Expand Down
8 changes: 2 additions & 6 deletions src/Components/Shifting/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ExportButton } from "../Common/Export";
import ListFilter from "./ListFilter";
import Page from "../Common/components/Page";
import SearchInput from "../Form/SearchInput";
import { formatAge, formatDateTime } from "../../Utils/utils";
import { formatDateTime, formatPatientAge } from "../../Utils/utils";
import { formatFilter } from "./Commons";
import { navigate } from "raviger";

Expand Down Expand Up @@ -90,11 +90,7 @@ export default function ListView() {
<div className="flex justify-between">
<div className="mb-2 text-xl font-bold capitalize">
{shift.patient_object.name} -{" "}
{formatAge(
shift.patient_object.age,
shift.patient_object.date_of_birth,
true
)}
{formatPatientAge(shift.patient_object, true)}
</div>
<div>
{shift.emergency && (
Expand Down
17 changes: 6 additions & 11 deletions src/Components/Shifting/ShiftDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard";
import Page from "../Common/components/Page";
import QRCode from "qrcode.react";
import RecordMeta from "../../CAREUI/display/RecordMeta";
import { formatAge, formatDateTime } from "../../Utils/utils";
import { formatDateTime, formatPatientAge } from "../../Utils/utils";
import useConfig from "../../Common/hooks/useConfig";

import { useTranslation } from "react-i18next";
Expand All @@ -24,6 +24,7 @@ import routes from "../../Redux/api.js";
import request from "../../Utils/request/request.js";
import { ConsultationModel } from "../Facility/models.js";
import CareIcon from "../../CAREUI/icons/CareIcon.js";
import { PatientModel } from "../Patient/models.js";

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

Expand Down Expand Up @@ -94,13 +95,7 @@ export default function ShiftDetails(props: { id: string }) {
"\n" +
t("age") +
":" +
+(
formatAge(
data?.patient_object?.age,
data?.patient_object?.date_of_birth,
true
) ?? "-"
) +
+formatPatientAge(data.patient_object, true) +
"\n" +
t("origin_facility") +
":" +
Expand Down Expand Up @@ -128,7 +123,7 @@ export default function ShiftDetails(props: { id: string }) {
setIsCopied(false);
}, 5000);

const showPatientCard = (patientData: any) => {
const showPatientCard = (patientData: PatientModel) => {
const patientGender = GENDER_TYPES.find(
(i) => i.id === patientData?.gender
)?.text;
Expand Down Expand Up @@ -185,7 +180,7 @@ export default function ShiftDetails(props: { id: string }) {
<span className="font-semibold leading-relaxed">
{t("age")}:{" "}
</span>
{formatAge(patientData?.age, patientData?.date_of_birth, true)}
{formatPatientAge(patientData, true)}
</div>
)}
{patientData?.gender === 2 && patientData?.is_antenatal && (
Expand Down Expand Up @@ -370,7 +365,7 @@ export default function ShiftDetails(props: { id: string }) {
<span className="font-semibold leading-relaxed">
{t("age")}:{" "}
</span>
{formatAge(patientData.age, patientData.date_of_birth, true)}
{formatPatientAge(patientData, true)}
</div>
<div>
<span className="font-semibold leading-relaxed">
Expand Down
12 changes: 6 additions & 6 deletions src/Components/Shifting/ShiftingBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
useRef,
useState,
} from "react";
import { classNames, formatAge, formatDateTime } from "../../Utils/utils";
import {
classNames,
formatDateTime,
formatPatientAge,
} from "../../Utils/utils";
import { downloadShiftRequests } from "../../Redux/actions";
import { useDrag, useDrop } from "react-dnd";

Expand Down Expand Up @@ -88,11 +92,7 @@ const ShiftCard = ({ shift, filter }: any) => {
<div className="flex justify-between">
<div className="mb-2 text-xl font-bold capitalize">
{shift.patient_object.name} -{" "}
{formatAge(
shift.patient_object?.age,
shift.patient_object?.age.date_of_birth,
true
)}
{formatPatientAge(shift.patient_object, true)}
</div>
<div>
{shift.emergency && (
Expand Down
62 changes: 56 additions & 6 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import phoneCodesJson from "../Common/static/countryPhoneAndFlags.json";
import dayjs from "./dayjs";
import { UserModel } from "../Components/Users/models";
import { PatientModel } from "../Components/Patient/models";

interface ApacheParams {
age: number;
Expand Down Expand Up @@ -396,6 +397,14 @@ export const getCountryCode = (phoneNumber: string) => {
return undefined;
};

const getRelativeDateSuffix = (abbreviated: boolean) => {
return {
day: abbreviated ? "d" : "days",
month: abbreviated ? "mo" : "months",
year: abbreviated ? "yr" : "years",
};
};

export const formatAge = (
age?: number,
date_of_birth?: string,
Expand All @@ -404,9 +413,7 @@ export const formatAge = (
if (!age && !date_of_birth) return undefined;
if (!age) age = 0;

const daySuffix = abbreviated ? "d" : "days";
const monthSuffix = abbreviated ? "mo" : "months";
const yearSuffix = abbreviated ? "yr" : "years";
const suffixes = getRelativeDateSuffix(abbreviated);

if (age < 1 && date_of_birth) {
const dob = new Date(date_of_birth);
Expand All @@ -416,11 +423,54 @@ export const formatAge = (
const months = Math.floor(diffDays / 30);
const days = diffDays % 30;
if (months === 0) {
return `${days} ${daySuffix}`;
return `${days} ${suffixes.day}`;
}
return `${months} ${monthSuffix} ${days} ${daySuffix}`;
return `${months} ${suffixes.month} ${days} ${suffixes.day}`;
}
return `${age} ${yearSuffix}`;
return `${age} ${suffixes.year}`;
};

export const formatPatientAge = (
data: Pick<
PatientModel,
"date_of_birth" | "year_of_birth" | "age" | "age_days"
>,
abbreviated = false
) => {
const suffixes = getRelativeDateSuffix(abbreviated);

if (data.age) {
return `${data.age}${suffixes.year}`;
}

const formatAgeFromDays = (ageDays: number) => {
const months = Math.floor(ageDays / 30);
const days = ageDays % 30;

if (months) {
return `${months}${suffixes.month} ${days}${suffixes.day}`;
}
return `${days}${suffixes.day}`;
};

// Voluntarily checking if `date_of_birth` non null as age_days could have
// value if date_of_birth is null but has `year_of_birth`.
if (data.age_days != null && data.date_of_birth) {
return formatAgeFromDays(data.age_days);
}

if (data.date_of_birth) {
return formatAgeFromDays(
dayjs(new Date()).diff(dayjs(data.date_of_birth), "days")
);
}

const years = new Date().getFullYear() - data.year_of_birth!;
if (years) {
return `${years}${suffixes.year}`;
}

return `${data.year_of_birth} (birth year)`;
};

export const scrollTo = (id: string | boolean) => {
Expand Down

0 comments on commit d115c07

Please sign in to comment.