Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

noUncheckedIndexedAccess in tsconfig #9880

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/CAREUI/interactive/HumanChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function HumanBodyChart({
>
{getTitle(
`${path.region}`.replace(
new RegExp(Object.keys(HumanBodyPaths)[i], "i"),
new RegExp(Object.keys(HumanBodyPaths)[i] ?? "", "i"),
Rishith25 marked this conversation as resolved.
Show resolved Hide resolved
"",
),
)}
Expand Down
4 changes: 3 additions & 1 deletion src/Providers/PatientUserProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ export default function PatientUserProvider({ children }: Props) {
const selectedPatient =
userData.results.find((patient) => patient.id === localPatient?.id) ||
userData.results[0];
setSelectedPatient(selectedPatient);
if (selectedPatient) {
setSelectedPatient(selectedPatient);
}
localStorage.setItem("selectedPatient", JSON.stringify(selectedPatient));
}
}, [userData]);
Expand Down
3 changes: 2 additions & 1 deletion src/Routers/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ const Routes: AppRoutes = {
...(import.meta.env.PROD ? { "/icons": () => <IconIndex /> } : {}),

"/apps": () => <PlugConfigList />,
"/apps/plug-configs/:slug": ({ slug }) => <PlugConfigEdit slug={slug} />,
"/apps/plug-configs/:slug": ({ slug }) =>
slug ? <PlugConfigEdit slug={slug} /> : <ErrorPage />,
"/login": () => <Redirect to="/" />,
};

Expand Down
46 changes: 27 additions & 19 deletions src/Routers/routes/ConsultationRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ const consultationRoutes: AppRoutes = {
"/facility/:facilityId/encounter/:encounterId/prescriptions/print": ({
facilityId,
encounterId,
}) => <PrintPrescription facilityId={facilityId} encounterId={encounterId} />,
}) => (
<PrintPrescription
facilityId={facilityId ?? ""}
encounterId={encounterId ?? ""}
/>
),
Comment on lines +13 to +18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would the component work with "" being passed to the id fields?

i mean, the whole reason for adding this strict check is so that it's super safe. i don't see much advantage in enabling this check when we are anyways gonna fallback to empty string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rithviknishad you are right. Falling back to an empty string defeats the purpose of strict checks.

I’m planning to display the ErrorPage when the parameters are undefined or invalid:

  "/facility/:facilityId/encounter/:encounterId/prescriptions/print": ({
    facilityId,
    encounterId,
  }) => (
    facilityId && encounterId ? (
      <PrintPrescription facilityId={facilityId} encounterId={encounterId} />
    ) : (
      <ErrorPage />
    )
  ),

Does this approach align with what you had in mind?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the root issue seems to be with the implementation of AppRoutes type itself. It should've inferred type as string automatically instead, hence these checks wouldn't be needed in the first place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what we have now:

image

what's expected:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't need these checks with #9890 i believe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did check after enabling this config in that branch, it still gives string | undefined after enabling it. (atleast it's not never)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  "/facility/:facilityId/encounter/:encounterId/prescriptions/print": ({
    facilityId,
    encounterId,
  }) => (
    facilityId && encounterId ? (
      <PrintPrescription facilityId={facilityId} encounterId={encounterId} />
    ) : (
      <ErrorPage />
    )
  ),

@rithviknishad So should i follow this approach to handle the undefined right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need a better solution than that 😅 this approach is not maintainable. with too many routes, repeating this logic is quite cumbersome.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(there's a reason for the issue being 6 story points) 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rithviknishad I have found two methods

  1. Using the non-null assertion operator (!)
/facility/:facilityId/encounter/:encounterId/prescriptions/print": ({
    facilityId,
    encounterId,
  }) => (
    <PrintPrescription facilityId={facilityId!} encounterId={encounterId!} />
  ),
  1. I have modified AppRoutes type in AppRoute.tsx
export type AppRoutes = {
  [K in keyof RouteFunction<K>]: RouteFunction<K>;
};

by 2nd method params are of type any and if we declare the param types as string

"/facility/:facilityId/encounter/:encounterId/prescriptions/print": ({
    facilityId,
    encounterId,
  }: {
    facilityId: string;
    encounterId: string;
  }) => <PrintPrescription facilityId={facilityId} encounterId={encounterId} />,

image

Currently trying to finding more methods to infer params as directly string instead of string | undefind or never

"/facility/:facilityId/encounter/:encounterId/:tab": ({
facilityId,
encounterId,
tab,
}) => (
<EncounterShow
facilityId={facilityId}
encounterId={encounterId}
facilityId={facilityId ?? ""}
encounterId={encounterId ?? ""}
tab={tab}
/>
),
Expand All @@ -28,57 +33,60 @@ const consultationRoutes: AppRoutes = {
patientId,
}) => (
<EncounterQuestionnaire
facilityId={facilityId}
patientId={patientId}
facilityId={facilityId ?? ""}
patientId={patientId ?? ""}
questionnaireSlug="encounter"
/>
),
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/edit_encounter":
({ facilityId, encounterId, patientId }) => (
<EncounterQuestionnaire
facilityId={facilityId}
facilityId={facilityId ?? ""}
encounterId={encounterId}
questionnaireSlug="encounter"
patientId={patientId}
patientId={patientId ?? ""}
/>
),
"/facility/:facilityId/patient/:patientId/questionnaire": ({
facilityId,
patientId,
}) => (
<EncounterQuestionnaire
facilityId={facilityId}
patientId={patientId}
facilityId={facilityId ?? ""}
patientId={patientId ?? ""}
subjectType="patient"
/>
),
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/questionnaire":
({ facilityId, encounterId, patientId }) => (
<EncounterQuestionnaire
facilityId={facilityId}
facilityId={facilityId ?? ""}
encounterId={encounterId}
patientId={patientId}
patientId={patientId ?? ""}
/>
),
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/questionnaire/:slug":
({ facilityId, encounterId, slug, patientId }) => (
<EncounterQuestionnaire
facilityId={facilityId}
facilityId={facilityId || ""}
encounterId={encounterId}
questionnaireSlug={slug}
patientId={patientId}
patientId={patientId || ""}
/>
),
"/facility/:facilityId/patient/:patientId/encounter/:encounterId/questionnaire_response/:id":
({ patientId, id }) => (
<QuestionnaireResponseView responseId={id} patientId={patientId} />
<QuestionnaireResponseView
responseId={id || ""}
patientId={patientId || ""}
/>
),
"/facility/:facilityId/patient/:patientId/consultation/:id/consent-records":
({ facilityId, patientId, id }) => (
<PatientConsentRecords
facilityId={facilityId}
patientId={patientId}
consultationId={id}
facilityId={facilityId || ""}
patientId={patientId || ""}
consultationId={id || ""}
/>
),
"/facility/:facilityId/patient/:patientId/encounterId/:id/files/": ({
Expand All @@ -87,8 +95,8 @@ const consultationRoutes: AppRoutes = {
id,
}) => (
<FileUploadPage
facilityId={facilityId}
patientId={patientId}
facilityId={facilityId || ""}
patientId={patientId || ""}
encounterId={id}
type="encounter"
/>
Expand Down
18 changes: 12 additions & 6 deletions src/Routers/routes/FacilityRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,28 @@ const FacilityRoutes: AppRoutes = {
<FacilityCreate facilityId={facilityId} />
),
"/facility/:facilityId": ({ facilityId }) => (
<FacilityHome facilityId={facilityId} />
<FacilityHome facilityId={facilityId ? facilityId : ""} />
),
"/facility/:facilityId/users": ({ facilityId }) => (
<FacilityUsers facilityId={facilityId} />
<FacilityUsers facilityId={facilityId ? facilityId : 0} />
),
"/facility/:facilityId/resource/new": ({ facilityId }) => (
<ResourceCreate facilityId={facilityId} />
<ResourceCreate facilityId={facilityId ? facilityId : 0} />
),
"/facility/:facilityId/organization": ({ facilityId }) => (
<FacilityOrganizationIndex facilityId={facilityId} />
<FacilityOrganizationIndex facilityId={facilityId ? facilityId : ""} />
),
"/facility/:facilityId/organization/:id": ({ facilityId, id }) => (
<FacilityOrganizationView facilityId={facilityId} id={id} />
<FacilityOrganizationView
facilityId={facilityId ? facilityId : ""}
id={id ? id : ""}
/>
),
"/facility/:facilityId/organization/:id/users": ({ facilityId, id }) => (
<FacilityOrganizationUsers facilityId={facilityId} id={id} />
<FacilityOrganizationUsers
facilityId={facilityId ? facilityId : ""}
id={id ? id : ""}
/>
),
};

Expand Down
36 changes: 28 additions & 8 deletions src/Routers/routes/OrganizationRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,49 @@ import OrganizationView from "@/pages/Organization/OrganizationView";

const OrganizationRoutes: AppRoutes = {
"/organization": () => <OrganizationIndex />,
"/organization/:id": ({ id }) => <OrganizationView id={id} />,
"/organization/:id/users": ({ id }) => <OrganizationUsers id={id} />,
"/organization/:id/patients": ({ id }) => <OrganizationPatients id={id} />,
"/organization/:id": ({ id }) => <OrganizationView id={id ?? ""} />,
"/organization/:id/users": ({ id }) => <OrganizationUsers id={id ?? ""} />,
"/organization/:id/patients": ({ id }) => (
<OrganizationPatients id={id ?? ""} />
),
"/organization/:id/facilities": ({ id }) => (
<OrganizationFacilities id={id} />
<OrganizationFacilities id={id ?? ""} />
),
"/organization/:navOrganizationId/children/:id": ({
navOrganizationId,
id,
}) => <OrganizationView id={id} navOrganizationId={navOrganizationId} />,
}) => (
<OrganizationView
id={id ?? ""}
navOrganizationId={navOrganizationId ?? ""}
/>
),
"/organization/:navOrganizationId/children/:id/users": ({
navOrganizationId,
id,
}) => <OrganizationUsers id={id} navOrganizationId={navOrganizationId} />,
}) => (
<OrganizationUsers
id={id ?? ""}
navOrganizationId={navOrganizationId ?? ""}
/>
),
"/organization/:navOrganizationId/children/:id/patients": ({
navOrganizationId,
id,
}) => <OrganizationPatients id={id} navOrganizationId={navOrganizationId} />,
}) => (
<OrganizationPatients
id={id ?? ""}
navOrganizationId={navOrganizationId ?? ""}
/>
),
"/organization/:navOrganizationId/children/:id/facilities": ({
navOrganizationId,
id,
}) => (
<OrganizationFacilities id={id} navOrganizationId={navOrganizationId} />
<OrganizationFacilities
id={id ?? ""}
navOrganizationId={navOrganizationId ?? ""}
/>
),
};

Expand Down
20 changes: 11 additions & 9 deletions src/Routers/routes/PatientRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,40 @@ import VerifyPatient from "@/pages/Patients/VerifyPatient";

const PatientRoutes: AppRoutes = {
"/facility/:facilityId/patients": ({ facilityId }) => (
<PatientIndex facilityId={facilityId} />
<PatientIndex facilityId={facilityId ?? ""} />
),
"/facility/:facilityId/encounters": ({ facilityId }) => (
<EncounterList facilityId={facilityId} />
),
"/facility/:facilityId/patients/verify": ({ facilityId }) => (
<VerifyPatient facilityId={facilityId} />
<VerifyPatient facilityId={facilityId ?? ""} />
),
"/patient/:id": ({ id }) => <PatientHome id={id} page="demography" />,
"/patient/:id": ({ id }) => <PatientHome id={id ?? ""} page="demography" />,
"/facility/:facilityId/patient/create": ({ facilityId }) => (
<PatientRegistration facilityId={facilityId} />
<PatientRegistration facilityId={facilityId ?? ""} />
),
"/facility/:facilityId/patient/:id": ({ facilityId, id }) => (
<PatientHome facilityId={facilityId} id={id} page="demography" />
<PatientHome facilityId={facilityId} id={id ?? ""} page="demography" />
),
...patientTabs.reduce((acc: AppRoutes, tab) => {
acc["/facility/:facilityId/patient/:id/" + tab.route] = ({
facilityId,
id,
}) => <PatientHome facilityId={facilityId} id={id} page={tab.route} />;
}) => (
<PatientHome facilityId={facilityId} id={id ?? ""} page={tab.route} />
);
return acc;
}, {}),
"/facility/:facilityId/patient/:id/update": ({ facilityId, id }) => (
<PatientRegistration facilityId={facilityId} patientId={id} />
<PatientRegistration facilityId={facilityId ?? ""} patientId={id} />
),
"/facility/:facilityId/patient/:patientId/files": ({
facilityId,
patientId,
}) => (
<FileUploadPage
facilityId={facilityId}
patientId={patientId}
facilityId={facilityId ?? ""}
patientId={patientId ?? ""}
type="patient"
/>
),
Expand Down
4 changes: 2 additions & 2 deletions src/Routers/routes/ResourceRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const ResourceRoutes: AppRoutes = {
"/resource": () => <Redirect to={`/resource/${getDefaultView()}`} />,
"/resource/board": () => <BoardView />,
"/resource/list": () => <ListView />,
"/resource/:id": ({ id }) => <ResourceDetails id={id} />,
"/resource/:id/update": ({ id }) => <ResourceDetailsUpdate id={id} />,
"/resource/:id": ({ id }) => <ResourceDetails id={id ?? ""} />,
"/resource/:id/update": ({ id }) => <ResourceDetailsUpdate id={id ?? ""} />,
};

export default ResourceRoutes;
8 changes: 5 additions & 3 deletions src/Routers/routes/UserRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ const UserRoutes: AppRoutes = {
facilityId,
username,
tab,
}) => <UserHome facilityId={facilityId} username={username} tab={tab} />,
}) => (
<UserHome facilityId={facilityId} username={username} tab={tab ?? ""} />
),
"/users/:username": ({ username }) => (
<UserHome username={username} tab="profile" />
),
"/users/:username/:tab": ({ username, tab }) => (
<UserHome username={username} tab={tab} />
<UserHome username={username} tab={tab ?? ""} />
),
"/user/:tab": ({ tab }) => <UserHome tab={tab} />,
"/user/:tab": ({ tab }) => <UserHome tab={tab ?? ""} />,
};

export default UserRoutes;
4 changes: 2 additions & 2 deletions src/Routers/routes/questionnaireRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { AppRoutes } from "../AppRouter";

const QuestionnaireRoutes: AppRoutes = {
"/questionnaire": () => <QuestionnaireList />,
"/questionnaire/:id": ({ id }) => <QuestionnaireShow id={id} />,
"/questionnaire/:id/edit": ({ id }) => <QuestionnaireEditor id={id} />,
"/questionnaire/:id": ({ id }) => <QuestionnaireShow id={id ?? ""} />,
"/questionnaire/:id/edit": ({ id }) => <QuestionnaireEditor id={id ?? ""} />,
};

export default QuestionnaireRoutes;
8 changes: 4 additions & 4 deletions src/Utils/AutoSave.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function DraftSection(props: {
const saveKey = useRef(`form_draft_${window.location.pathname}`);
const draftStarted =
drafts.length > 0
? drafts[drafts.length - 1].draft == props.formData
? (drafts[drafts.length - 1]?.draft ?? {}) == props.formData
: false;
Rishith25 marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
Expand Down Expand Up @@ -181,7 +181,7 @@ export const RestoreDraftButton = () => {
className="flex items-center space-x-2"
onClick={() =>
handleDraftSelect(
(draftStarted ? drafts[0] : drafts[drafts.length - 1]).draft,
(draftStarted ? drafts[0] : drafts[drafts.length - 1])?.draft,
)
}
>
Expand All @@ -191,8 +191,8 @@ export const RestoreDraftButton = () => {
(
{relativeTime(
draftStarted
? drafts[0].timestamp
: drafts[drafts.length - 1].timestamp,
? drafts[0]?.timestamp
: drafts[drafts.length - 1]?.timestamp,
)}
)
</span>
Expand Down
14 changes: 9 additions & 5 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ export const formatPhoneNumber = (phoneNumber: string) => {
const phoneCodes: Record<string, CountryData> = phoneCodesJson;
return (
"+" +
phoneCodes[countryCode].code +
(phoneCodes[countryCode]?.code ?? "") +
" " +
phoneNumber.slice(phoneCodes[countryCode].code.length + 1)
phoneNumber.slice((phoneCodes[countryCode]?.code.length ?? 0) + 1)
);
}
return phoneNumber;
Expand All @@ -304,12 +304,16 @@ export const getCountryCode = (phoneNumber: string) => {
for (let i = 0; i < phoneCodesArr.length; i++) {
if (
phoneNumber.startsWith(
phoneCodes[phoneCodesArr[i]].code.replaceAll("-", ""),
phoneCodesArr[i] && phoneCodes[phoneCodesArr[i] as string]?.code
? phoneCodes[phoneCodesArr[i] as string]!.code.replaceAll("-", "")
: "",
)
) {
allMatchedCountries.push({
name: phoneCodesArr[i],
code: phoneCodes[phoneCodesArr[i]].code.replaceAll("-", ""),
name: phoneCodesArr[i] ?? "",
code:
phoneCodes[phoneCodesArr[i] as string]?.code.replaceAll("-", "") ??
"",
});
}
}
Expand Down
Loading
Loading