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: Pincode autofill in Patient Registration page in public page #10272

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions src/hooks/useOrganization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@ interface UseOrganizationParams {
parentId?: string;
name?: string;
enabled?: boolean;
authToken?: string;
}

export function useOrganization({
orgType = "",
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, {
Expand All @@ -27,6 +36,7 @@ export function useOrganization({
parent: parentId,
name,
},
...headers,
}),
enabled: enabled && !!name,
});
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useStateAndDistrictFromPincode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getPincodeDetails } from "@/Utils/utils";

interface UseStateAndDistrictProps {
pincode: string;
authToken?: string;
}

interface PincodeResponse {
Expand All @@ -21,6 +22,7 @@ interface PincodeResponse {

export function useStateAndDistrictFromPincode({
pincode,
authToken,
}: UseStateAndDistrictProps) {
const {
data: pincodeDetails,
Expand All @@ -43,6 +45,7 @@ export function useStateAndDistrictFromPincode({
orgType: "govt",
parentId: "",
name: stateName,
authToken,
enabled: !!stateName,
});

Expand All @@ -56,6 +59,7 @@ export function useStateAndDistrictFromPincode({
orgType: "govt",
parentId: stateOrg?.id,
name: districtName,
authToken,
enabled: !!stateOrg?.id && !!districtName,
});

Expand Down
56 changes: 51 additions & 5 deletions src/pages/PublicAppointments/PatientRegistration.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { navigate, useNavigationPrompt } 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 {
Expand All @@ -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";
Expand All @@ -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,
Expand Down Expand Up @@ -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<Organization[]>([]);

const patientSchema = z
.object({
name: z
Expand Down Expand Up @@ -219,7 +226,27 @@ export function PatientRegistration(props: PatientRegistrationProps) {
t("unsaved_changes"),
);

// 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 (
<>
Expand Down Expand Up @@ -391,6 +418,22 @@ export function PatientRegistration(props: PatientRegistrationProps) {
<FormControl>
<Input {...field} />
</FormControl>
{showAutoFilledPincode && (
<div
role="status"
aria-live="polite"
className="flex items-center"
>
<CareIcon
icon="l-check-circle"
className="mr-2 text-sm text-green-500"
aria-hidden="true"
/>
<span className="text-sm text-primary-500">
{t("pincode_autofill")}
</span>
</div>
)}
<FormMessage />
</FormItem>
)}
Expand All @@ -403,11 +446,14 @@ export function PatientRegistration(props: PatientRegistrationProps) {
<FormItem className="flex flex-col">
<FormControl>
<GovtOrganizationSelector
{...field}
value={form.watch("geo_organization")}
selected={selectedLevels}
required
authToken={tokenData.token}
onChange={(value) => {
field.onChange(value);
}}
onChange={(value) =>
form.setValue("geo_organization", value)
}
/>
</FormControl>
<FormMessage />
Expand Down
Loading