From 350429417f1bee305e8dad0216efbdc85dc05892 Mon Sep 17 00:00:00 2001 From: Rishith25 Date: Mon, 27 Jan 2025 19:39:40 +0530 Subject: [PATCH] patient autofill pincode --- src/hooks/useOrganization.ts | 10 ++++ src/hooks/useStateAndDistrictFromPincode.ts | 4 ++ .../PatientRegistration.tsx | 56 +++++++++++++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/hooks/useOrganization.ts b/src/hooks/useOrganization.ts index d33678f356b..ae895e22510 100644 --- a/src/hooks/useOrganization.ts +++ b/src/hooks/useOrganization.ts @@ -11,6 +11,7 @@ interface UseOrganizationParams { parentId?: string; name?: string; enabled?: boolean; + authToken?: string; } export function useOrganization({ @@ -18,7 +19,15 @@ export function useOrganization({ parentId = "", name = "", enabled = true, + authToken, }: UseOrganizationParams) { + const headers = authToken + ? { + headers: { + Authorization: `Bearer ${authToken}`, + }, + } + : {}; const { data, isLoading, isError } = useQuery({ queryKey: ["organization", orgType, name, parentId], queryFn: query(organizationApi.list, { @@ -27,6 +36,7 @@ export function useOrganization({ parent: parentId, name, }, + ...headers, }), enabled: enabled && !!name, }); diff --git a/src/hooks/useStateAndDistrictFromPincode.ts b/src/hooks/useStateAndDistrictFromPincode.ts index 1e8b31f3207..e53af5345b4 100644 --- a/src/hooks/useStateAndDistrictFromPincode.ts +++ b/src/hooks/useStateAndDistrictFromPincode.ts @@ -12,6 +12,7 @@ import { getPincodeDetails } from "@/Utils/utils"; interface UseStateAndDistrictProps { pincode: string; + authToken?: string; } interface PincodeResponse { @@ -21,6 +22,7 @@ interface PincodeResponse { export function useStateAndDistrictFromPincode({ pincode, + authToken, }: UseStateAndDistrictProps) { const { data: pincodeDetails, @@ -43,6 +45,7 @@ export function useStateAndDistrictFromPincode({ orgType: "govt", parentId: "", name: stateName, + authToken, enabled: !!stateName, }); @@ -56,6 +59,7 @@ export function useStateAndDistrictFromPincode({ orgType: "govt", parentId: stateOrg?.id, name: districtName, + authToken, enabled: !!stateOrg?.id && !!districtName, }); diff --git a/src/pages/PublicAppointments/PatientRegistration.tsx b/src/pages/PublicAppointments/PatientRegistration.tsx index fc3888defa5..d01724cc607 100644 --- a/src/pages/PublicAppointments/PatientRegistration.tsx +++ b/src/pages/PublicAppointments/PatientRegistration.tsx @@ -1,12 +1,14 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQueryClient } from "@tanstack/react-query"; import { navigate } from "raviger"; -import { Fragment } from "react"; +import { Fragment, useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { z } from "zod"; +import CareIcon from "@/CAREUI/icons/CareIcon"; + import { Button } from "@/components/ui/button"; import DateField from "@/components/ui/date-field"; import { @@ -23,6 +25,7 @@ import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Textarea } from "@/components/ui/textarea"; import { usePatientContext } from "@/hooks/usePatientUser"; +import { useStateAndDistrictFromPincode } from "@/hooks/useStateAndDistrictFromPincode"; import { GENDER_TYPES } from "@/common/constants"; import { validateName, validatePincode } from "@/common/validation"; @@ -37,6 +40,7 @@ import { AppointmentPatient, AppointmentPatientRegister, } from "@/pages/Patient/Utils"; +import { Organization } from "@/types/organization/organization"; import PublicAppointmentApi from "@/types/scheduling/PublicAppointmentApi"; import { Appointment, @@ -79,6 +83,9 @@ export function PatientRegistration(props: PatientRegistrationProps) { const patientUserContext = usePatientContext(); const tokenData = patientUserContext?.tokenData; + const [showAutoFilledPincode, setShowAutoFilledPincode] = useState(false); + const [selectedLevels, setSelectedLevels] = useState([]); + const patientSchema = z .object({ name: z @@ -211,7 +218,27 @@ export function PatientRegistration(props: PatientRegistrationProps) { createPatient(formattedData); }); - // const [showAutoFilledPincode, setShowAutoFilledPincode] = useState(false); + const { stateOrg, districtOrg } = useStateAndDistrictFromPincode({ + pincode: form.watch("pincode")?.toString() || "", + authToken: tokenData.token, + }); + + useEffect(() => { + const levels: Organization[] = []; + if (stateOrg) levels.push(stateOrg); + if (districtOrg) levels.push(districtOrg); + + setSelectedLevels(levels); + + if (levels.length == 2) { + setShowAutoFilledPincode(true); + const timer = setTimeout(() => { + setShowAutoFilledPincode(false); + }, 5000); + return () => clearTimeout(timer); + } + return () => setShowAutoFilledPincode(false); + }, [stateOrg, districtOrg]); return ( <> @@ -383,6 +410,22 @@ export function PatientRegistration(props: PatientRegistrationProps) { + {showAutoFilledPincode && ( +
+
+ )} )} @@ -395,11 +438,14 @@ export function PatientRegistration(props: PatientRegistrationProps) { { - field.onChange(value); - }} + onChange={(value) => + form.setValue("geo_organization", value) + } />