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: Facility Creation Form: Pincode Autofill Overwrites Pre-filled Geographic Data & Added Similar Approach in User Creation Form #10166

Merged
merged 1 commit into from
Jan 29, 2025
Merged
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
13 changes: 13 additions & 0 deletions src/components/Facility/FacilityForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 30 additions & 4 deletions src/components/Users/UserForm.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<Organization[]>([]);

const userFormSchema = z
.object({
Expand Down Expand Up @@ -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 (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
Expand Down Expand Up @@ -585,8 +607,12 @@ export default function UserForm({ onSubmitSuccess, existingUsername }: Props) {
<FormItem>
<FormControl>
<GovtOrganizationSelector
value={field.value}
onChange={field.onChange}
{...field}
value={form.watch("geo_organization")}
selected={selectedLevels}
onChange={(value) =>
form.setValue("geo_organization", value)
}
required={!isEditMode}
/>
</FormControl>
Expand Down
1 change: 1 addition & 0 deletions src/pages/Organization/OrganizationUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default function OrganizationUsers({ id, navOrganizationId }: Props) {
onUserCreated={(user) => {
updateQuery({ sheet: "link", username: user.username });
}}
organizationId={id}
/>
<LinkUserSheet
organizationId={id}
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Organization/components/AddUserSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ interface AddUserSheetProps {
open: boolean;
setOpen: (open: boolean) => void;
onUserCreated?: (user: UserBase) => void;
organizationId?: string;
}

export default function AddUserSheet({
open,
setOpen,
onUserCreated,
organizationId,
}: AddUserSheetProps) {
const { t } = useTranslation();
return (
Expand All @@ -50,6 +52,7 @@ export default function AddUserSheet({
setOpen(false);
onUserCreated?.(user);
}}
organizationId={organizationId}
/>
</div>
</SheetContent>
Expand Down
Loading