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

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4dd0e93
Autofill the State & District
yash-learner Jan 3, 2025
645bd96
Merge branch 'develop' into issues/9668/fix-auto-fill
yash-learner Jan 3, 2025
9cb67df
Show asterisk properly and pass error message
yash-learner Jan 3, 2025
d1042e4
Merge branch 'develop' into issues/9668/fix-auto-fill
yash-learner Jan 3, 2025
c15621d
Remove console logs
yash-learner Jan 3, 2025
67073f8
Inform parent about deletion & handle it
yash-learner Jan 3, 2025
5ba881e
Add cursor style to address input when disabled
yash-learner Jan 3, 2025
3b77133
Use organization type instead of any
yash-learner Jan 3, 2025
f58e7b5
Merge branch 'develop' into issues/9668/fix-auto-fill
yash-learner Jan 3, 2025
bdd739f
Add custom hook for organization data fetching
yash-learner Jan 6, 2025
6905db3
Add custom hook to fetch state and district from pincode
yash-learner Jan 6, 2025
45a3d9f
Refactor pincode handling to use custom hook for state and district r…
yash-learner Jan 6, 2025
ca922bb
Merge branch 'develop' into issues/9668/fix-auto-fill
yash-learner Jan 6, 2025
0ca6b73
Add error handling messages for organization and pincode fetching
yash-learner Jan 6, 2025
c6b1783
Update condition for showing auto-filled pincode in PatientRegistrati…
yash-learner Jan 6, 2025
d3fb25d
Merge branch 'develop' into issues/9668/fix-auto-fill
Jacobjeevan Jan 14, 2025
8ee3d60
switch to query debounce for phone
Jacobjeevan Jan 14, 2025
1d3740e
minor fixes
Jacobjeevan Jan 14, 2025
e4b9514
coderabbit suggestions
Jacobjeevan Jan 14, 2025
22687f3
minor changes
Jacobjeevan Jan 15, 2025
5558a67
rm select
Jacobjeevan Jan 15, 2025
0b5a74d
added useEffect for error alerts
Jacobjeevan Jan 15, 2025
e150868
rm type from useQuery (redundant)
Jacobjeevan Jan 15, 2025
ebee745
Merge branch 'develop' into issues/9668/fix-auto-fill
nihal467 Jan 16, 2025
8d1f0c8
Merge branch 'develop' into issues/9668/fix-auto-fill
Jacobjeevan Jan 16, 2025
060f3e3
fixed the cypress failure
nihal467 Jan 16, 2025
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
38 changes: 38 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,8 @@ import {
} from "@/Utils/utils";
import OrganizationSelector from "@/pages/Organization/components/OrganizationSelector";
import { PatientModel, validatePatient } from "@/types/emr/patient";
import { Organization } from "@/types/organization/organization";
import organizationApi from "@/types/organization/organizationApi";

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

const sidebarItems = [
{ label: t("patient__general-info"), id: "general-info" },
Expand Down Expand Up @@ -201,6 +205,21 @@ 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,
);

if (stateOrg && districtOrg) {
setSelectedLevels([stateOrg, districtOrg]);
}

setShowAutoFilledPincode(true);
setTimeout(() => {
setShowAutoFilledPincode(false);
Expand Down Expand Up @@ -304,6 +323,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 +622,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 +673,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