diff --git a/src/components/Facility/FacilityForm.tsx b/src/components/Facility/FacilityForm.tsx index 4d5250804c9..4d60d1f50f4 100644 --- a/src/components/Facility/FacilityForm.tsx +++ b/src/components/Facility/FacilityForm.tsx @@ -55,6 +55,15 @@ interface FacilityProps { onSubmitSuccess?: () => void; } +function extractHierarchyLevels(org: Organization | undefined): Organization[] { + const levels: Organization[] = []; + while (org && org.level_cache >= 0) { + levels.unshift(org as Organization); + org = org.parent as Organization | undefined; + } + return levels; +} + export default function FacilityForm(props: FacilityProps) { const { t } = useTranslation(); const queryClient = useQueryClient(); @@ -193,7 +202,11 @@ export default function FacilityForm(props: FacilityProps) { useEffect(() => { if (facilityId) return; + const orgLevels = extractHierarchyLevels(org); + const districtMatch = + districtOrg && orgLevels.some((level) => level.name === districtOrg.name); const levels: Organization[] = []; + if (districtMatch) return; if (stateOrg) levels.push(stateOrg); if (districtOrg) levels.push(districtOrg); if (!stateOrg && !districtOrg && org) levels.push(org); diff --git a/src/components/Users/UserForm.tsx b/src/components/Users/UserForm.tsx index b0750393764..56c77091b6f 100644 --- a/src/components/Users/UserForm.tsx +++ b/src/components/Users/UserForm.tsx @@ -1,6 +1,6 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import { useEffect } from "react"; +import { useEffect, useState } from "react"; import { useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; @@ -36,18 +36,26 @@ import { GENDERS } from "@/common/constants"; import mutate from "@/Utils/request/mutate"; import query from "@/Utils/request/query"; import GovtOrganizationSelector from "@/pages/Organization/components/GovtOrganizationSelector"; +import { Organization } from "@/types/organization/organization"; +import organizationApi from "@/types/organization/organizationApi"; import { CreateUserModel, UpdateUserModel, UserBase } from "@/types/user/user"; import userApi from "@/types/user/userApi"; interface Props { onSubmitSuccess?: (user: UserBase) => void; existingUsername?: string; + organizationId?: string; } -export default function UserForm({ onSubmitSuccess, existingUsername }: Props) { +export default function UserForm({ + onSubmitSuccess, + existingUsername, + organizationId, +}: Props) { const { t } = useTranslation(); const isEditMode = !!existingUsername; const queryClient = useQueryClient(); + const [selectedLevels, setSelectedLevels] = useState([]); const userFormSchema = z .object({ @@ -263,6 +271,20 @@ export default function UserForm({ onSubmitSuccess, existingUsername }: Props) { } }; + const { data: org } = useQuery({ + queryKey: ["organization", organizationId], + queryFn: query(organizationApi.get, { + pathParams: { id: organizationId }, + }), + enabled: !!organizationId, + }); + + useEffect(() => { + const levels: Organization[] = []; + if (org) levels.push(org); + setSelectedLevels(levels); + }, [org, organizationId]); + return (
@@ -585,8 +607,12 @@ export default function UserForm({ onSubmitSuccess, existingUsername }: Props) { + form.setValue("geo_organization", value) + } required={!isEditMode} /> diff --git a/src/pages/Organization/OrganizationUsers.tsx b/src/pages/Organization/OrganizationUsers.tsx index db9f846d8d5..feebdb813ba 100644 --- a/src/pages/Organization/OrganizationUsers.tsx +++ b/src/pages/Organization/OrganizationUsers.tsx @@ -67,6 +67,7 @@ export default function OrganizationUsers({ id, navOrganizationId }: Props) { onUserCreated={(user) => { updateQuery({ sheet: "link", username: user.username }); }} + organizationId={id} /> void; onUserCreated?: (user: UserBase) => void; + organizationId?: string; } export default function AddUserSheet({ open, setOpen, onUserCreated, + organizationId, }: AddUserSheetProps) { const { t } = useTranslation(); return ( @@ -50,6 +52,7 @@ export default function AddUserSheet({ setOpen(false); onUserCreated?.(user); }} + organizationId={organizationId} />