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 : Switch to using CreateFacilityForm #9662

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
03fd617
pincode updated
Mahendar0701 Jan 2, 2025
d1a6ca1
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 2, 2025
7db9c3a
populated geo_organization values
Mahendar0701 Jan 3, 2025
080f442
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 3, 2025
8159f13
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 3, 2025
fc0413e
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 3, 2025
d0daf9c
autofill of state and districts
Mahendar0701 Jan 4, 2025
f7db9bc
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 4, 2025
02a4ca0
Merge branch 'facility-edit' of https://github.com/Mahendar0701/care_…
Mahendar0701 Jan 4, 2025
ab383d5
translation
Mahendar0701 Jan 4, 2025
7dff65f
commit
Mahendar0701 Jan 4, 2025
4eda986
added usequery
Mahendar0701 Jan 4, 2025
c811b60
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 4, 2025
d9078ad
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 5, 2025
884fc82
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 5, 2025
0f49a53
Merge branch 'facility-edit' of https://github.com/Mahendar0701/care_…
Mahendar0701 Jan 5, 2025
b6b2318
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 5, 2025
fd3241f
moved useFetchOrganizationByName organizationapi.ts
Mahendar0701 Jan 5, 2025
f9d4e49
Merge branch 'facility-edit' of https://github.com/Mahendar0701/care_…
Mahendar0701 Jan 5, 2025
3a9dcbf
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 5, 2025
e87535d
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 5, 2025
213eb95
Merge branch 'develop' into facility-edit
Mahendar0701 Jan 6, 2025
87db767
added conditions
Mahendar0701 Jan 6, 2025
4599c93
Merge branch 'facility-edit' of https://github.com/Mahendar0701/care_…
Mahendar0701 Jan 6, 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
1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,7 @@
"select_investigation_groups": "Select Investigation Groups",
"select_investigations": "Select Investigations",
"select_local_body": "Select Local Body",
"select_location_from": "Select location from",
"select_patient": "Select Patient",
"select_policy": "Select an Insurance Policy",
"select_policy_to_add_items": "Select a Policy to Add Items",
Expand Down
25 changes: 25 additions & 0 deletions src/Utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { differenceInMinutes, format } from "date-fns";
import html2canvas from "html2canvas";

Expand All @@ -6,10 +7,12 @@ import phoneCodesJson from "@/common/static/countryPhoneAndFlags.json";

import * as Notification from "@/Utils/Notifications";
import dayjs from "@/Utils/dayjs";
import query from "@/Utils/request/query";
import { Time } from "@/Utils/types";
import { DoseRange, Timing } from "@/types/emr/medicationRequest";
import { Patient } from "@/types/emr/newPatient";
import { PatientModel } from "@/types/emr/patient";
import organizationApi from "@/types/organization/organizationApi";
import { Code } from "@/types/questionnaire/code";
import { Quantity } from "@/types/questionnaire/quantity";

Expand Down Expand Up @@ -189,9 +192,31 @@ export const getPincodeDetails = async (pincode: string, apiKey: string) => {
`https://api.data.gov.in/resource/6176ee09-3d56-4a3b-8115-21841576b2f6?api-key=${apiKey}&format=json&filters[pincode]=${pincode}&limit=1`,
);
const data = await response.json();
if (!data.records || data.records.length === 0) {
Notification.Error({ msg: "Invalid pincode" });
return null;
}
return data.records[0];
};

export function useFetchOrganizationByName(name: string, parentId?: string) {
return useQuery({
queryKey: ["organization", name, parentId],
queryFn: async () => {
const data = await query(organizationApi.list, {
queryParams: {
org_type: "govt",
parent: parentId || "",
name: name || "",
},
})({ signal: new AbortController().signal });

return data.results?.[0];
},
enabled: !!name,
});
}

Copy link
Contributor Author

@Mahendar0701 Mahendar0701 Jan 4, 2025

Choose a reason for hiding this comment

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

@bodhish @rithviknishad @Jacobjeevan you mean something like this?? #9711 (review)

Copy link
Member

Choose a reason for hiding this comment

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

move this to types/organization/organizationApi itself instead of defining it here.

export const includesIgnoreCase = (str1: string, str2: string) => {
if (!str1 || !str2) return false;
const lowerCaseStr1 = str1.toLowerCase();
Expand Down
59 changes: 54 additions & 5 deletions src/components/Facility/FacilityCreate.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import careConfig from "@careConfig";
import {
Popover,
PopoverButton,
Expand Down Expand Up @@ -58,8 +59,13 @@ import * as Notification from "@/Utils/Notifications";
import routes from "@/Utils/request/api";
import query from "@/Utils/request/query";
import request from "@/Utils/request/request";
import { parsePhoneNumber } from "@/Utils/utils";
import {
getPincodeDetails,
parsePhoneNumber,
useFetchOrganizationByName,
} from "@/Utils/utils";
import OrganizationSelector from "@/pages/Organization/components/OrganizationSelector";
import { Organization } from "@/types/organization/organization";

interface FacilityProps {
facilityId?: string;
Expand All @@ -68,6 +74,9 @@ export const FacilityCreate = (props: FacilityProps) => {
const { t } = useTranslation();
const { facilityId } = props;
const [isLoading, setIsLoading] = useState(false);
const [selectedLevels, setSelectedLevels] = useState<Organization[]>([]);
const [pincode, setPincode] = useState("");

const { goBack } = useAppHistory();

const facilityFormSchema = z.object({
Expand Down Expand Up @@ -146,7 +155,7 @@ export const FacilityCreate = (props: FacilityProps) => {
name: facilityData.name,
description: facilityData.description || "",
features: facilityData.features || [],
pincode: facilityData.pincode,
pincode: facilityData.pincode?.toString() || "",
geo_organization: facilityData.geo_organization,
address: facilityData.address,
phone_number: facilityData.phone_number,
Expand Down Expand Up @@ -212,9 +221,43 @@ export const FacilityCreate = (props: FacilityProps) => {
}
};

const { data: pincodeData, isError: isPincodeError } = useQuery({
queryKey: ["pincodeDetails", pincode],
queryFn: () => getPincodeDetails(pincode, careConfig.govDataApiKey),
enabled: validatePincode(pincode),
});

if (isPincodeError) {
Notification.Error({ msg: "Invalid pincode" });
}

Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved
const stateName = pincodeData?.statename;

const districtName = pincodeData?.districtname;

const { data: stateOrg } = useFetchOrganizationByName(stateName);
const { data: districtOrg } = useFetchOrganizationByName(
districtName,
stateOrg?.id,
);

useEffect(() => {
if (stateOrg && districtOrg) {
setSelectedLevels([stateOrg, districtOrg]);
} else {
setSelectedLevels([]);
}
}, [stateOrg, districtOrg]);

console.log("selectedLevels", selectedLevels);

const handlePincodeChange = (value: string) => {
setPincode(value);
};
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

used usequery tanstack, is this approach acceptable??

return (
<Page
title={facilityId ? "Update Facility" : "Create Facility"}
title={facilityId ? t("update_facility") : t("create_facility")}
crumbsReplacements={{
[facilityId || "????"]: { name: form.watch("name") },
}}
Expand Down Expand Up @@ -318,6 +361,7 @@ export const FacilityCreate = (props: FacilityProps) => {
required
onChange={(value) => {
field.onChange(value.value);
handlePincodeChange(value.value);
}}
error={form.formState.errors.pincode?.message}
/>
Expand All @@ -329,7 +373,12 @@ export const FacilityCreate = (props: FacilityProps) => {
<div className="col-span-2 grid grid-cols-2 gap-5">
<OrganizationSelector
required={true}
onChange={(value) => form.setValue("geo_organization", value)}
value={facilityData?.geo_organization}
parentSelectedLevels={selectedLevels}
onChange={(value) => {
// Update geo_organization value in form
form.setValue("geo_organization", value);
}}
/>
</div>

Expand Down Expand Up @@ -411,7 +460,7 @@ export const FacilityCreate = (props: FacilityProps) => {
>
<CareIcon icon="l-map-marker" className="text-xl" />
<span className="tooltip-text tooltip-bottom">
Select location from map
{t("select_location_from")}
</span>
</Button>
</PopoverButton>
Expand Down
26 changes: 23 additions & 3 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,7 @@ interface OrganizationSelectorProps {
onChange: (value: string) => void;
required?: boolean;
authToken?: string;
parentSelectedLevels?: Organization[];
}

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

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

useEffect(() => {
if (value && parentSelectedLevels?.length == 0) {
const parsedValue = typeof value === "string" ? JSON.parse(value) : value;
if (parsedValue) {
const levels: Organization[] = [];
let current = parsedValue;

while (current.parent) {
levels.unshift(current);
current = current.parent;
}

setSelectedLevels(levels);
}
} else if (parentSelectedLevels) {
setSelectedLevels(parentSelectedLevels);
}
}, [value, parentSelectedLevels]);
Mahendar0701 marked this conversation as resolved.
Show resolved Hide resolved

const headers = props.authToken
? {
headers: {
Expand Down Expand Up @@ -148,7 +168,7 @@ export default function OrganizationSelector(props: OrganizationSelectorProps) {
required={selectedLevels.length === 0 && required}
>
<Autocomplete
value=""
value={selectedLevels[selectedLevels.length - 1]?.id || ""}
options={getOrganizationOptions(
selectedLevels.length === 0
? getAllOrganizations?.results
Expand Down
Loading