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

Fix state & district autofill & multiple other small issues in Patient Registration page #9711

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/components/Patient/PatientRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMutation, useQuery } from "@tanstack/react-query";
import { navigate, useQueryParams } from "raviger";
import { Fragment, useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";

import SectionNavigator from "@/CAREUI/misc/SectionNavigator";

Expand Down Expand Up @@ -47,6 +48,7 @@ import {
} from "@/Utils/utils";
import OrganizationSelector from "@/pages/Organization/components/OrganizationSelector";
import { PatientModel, validatePatient } from "@/types/emr/patient";
import organizationApi from "@/types/organization/organizationApi";

import Autocomplete from "../ui/autocomplete";
import InputWithError from "../ui/input-with-error";
Expand Down Expand Up @@ -79,6 +81,7 @@ export default function PatientRegistration(
const [suppressDuplicateWarning, setSuppressDuplicateWarning] =
useState(!!patientId);
const [debouncedNumber, setDebouncedNumber] = useState<string>();
const [selectedLevels, setSelectedLevels] = useState<any[]>([]);

const sidebarItems = [
{ label: t("patient__general-info"), id: "general-info" },
Expand Down Expand Up @@ -201,6 +204,19 @@ export default function PatientRegistration(
const { statename: _stateName, districtname: _districtName } =
pincodeDetails;

const stateOrg = await fetchOrganizationByName(_stateName);
if (!stateOrg) {
setSelectedLevels([]);
return;
}

const districtOrg = await fetchOrganizationByName(
_districtName,
stateOrg.id,
);

setSelectedLevels([stateOrg, districtOrg]);

setShowAutoFilledPincode(true);
setTimeout(() => {
setShowAutoFilledPincode(false);
Expand Down Expand Up @@ -304,6 +320,22 @@ export default function PatientRegistration(
return <Loading />;
}

async function fetchOrganizationByName(name: string, parentId?: string) {
try {
const data = await query(organizationApi.list, {
queryParams: {
org_type: "govt",
parent: parentId || "",
name,
},
})({ signal: new AbortController().signal });
return data.results?.[0];
} catch (error) {
toast.error("Error fetching organization");
return undefined;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

At some point, we may need to extract this into a separate hook

Copy link
Contributor

@Jacobjeevan Jacobjeevan Jan 4, 2025

Choose a reason for hiding this comment

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

Yea, go ahead and do that since same logic is being used in #9662.

Also check Rithvik's comment below.

cc: @Mahendar0701


return (
<Page title={title}>
<hr className="mt-4" />
Expand Down Expand Up @@ -587,6 +619,7 @@ export default function PatientRegistration(
setForm((f) => ({ ...f, permanent_address: e.target.value }))
}
disabled={sameAddress}
className={sameAddress ? "cursor-not-allowed" : ""}
/>
</InputWithError>
{/* <br />
Expand Down Expand Up @@ -637,12 +670,14 @@ export default function PatientRegistration(
<>
<OrganizationSelector
required={true}
parentSelectedLevels={selectedLevels}
onChange={(value) =>
setForm((f) => ({
...f,
geo_organization: value,
}))
}
errorMessage={errors.geo_organization?.[0]}
/>
</>
)}
Expand Down
33 changes: 28 additions & 5 deletions src/pages/Organization/components/OrganizationSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import { useEffect, useState } from "react";

import CareIcon from "@/CAREUI/icons/CareIcon";

Expand All @@ -20,6 +20,8 @@ interface OrganizationSelectorProps {
onChange: (value: string) => void;
required?: boolean;
authToken?: string;
parentSelectedLevels?: Organization[];
errorMessage?: string;
}

interface AutoCompleteOption {
Expand All @@ -28,8 +30,10 @@ interface AutoCompleteOption {
}

export default function OrganizationSelector(props: OrganizationSelectorProps) {
const { onChange, required } = props;
const [selectedLevels, setSelectedLevels] = useState<Organization[]>([]);
const { onChange, required, parentSelectedLevels } = props;
const [selectedLevels, setSelectedLevels] = useState<Organization[]>(
parentSelectedLevels || [],
);
const [searchQuery, setSearchQuery] = useDebouncedState("", 500);

const headers = props.authToken
Expand Down Expand Up @@ -105,9 +109,27 @@ export default function OrganizationSelector(props: OrganizationSelectorProps) {
};

const handleEdit = (level: number) => {
setSelectedLevels((prev) => prev.slice(0, level));
const newLevels = selectedLevels.slice(0, level);
setSelectedLevels(newLevels);

if (!newLevels.length) {
onChange("");
} else {
const lastOrg = newLevels[newLevels.length - 1];
if (!lastOrg.has_children) {
onChange(lastOrg.id);
} else {
onChange("");
}
}
};

useEffect(() => {
if (parentSelectedLevels) {
setSelectedLevels(parentSelectedLevels);
}
}, [parentSelectedLevels]);

return (
<>
{/* Selected Levels */}
Expand Down Expand Up @@ -145,7 +167,8 @@ export default function OrganizationSelector(props: OrganizationSelectorProps) {
<div>
<InputWithError
label={ORGANIZATION_LEVELS.govt[selectedLevels.length]}
required={selectedLevels.length === 0 && required}
required={!!required}
errors={[props.errorMessage || ""]}
>
<Autocomplete
value=""
Expand Down
Loading