From aa1005a7a8c88bf19b613391bb8cbae43d2300a7 Mon Sep 17 00:00:00 2001 From: Potenz Date: Mon, 28 Jul 2025 18:29:37 +0530 Subject: [PATCH] fixed query --- apps/Frontend/src/pages/settings-page.tsx | 133 +++++++++------------- 1 file changed, 52 insertions(+), 81 deletions(-) diff --git a/apps/Frontend/src/pages/settings-page.tsx b/apps/Frontend/src/pages/settings-page.tsx index 67fec96..6425b37 100644 --- a/apps/Frontend/src/pages/settings-page.tsx +++ b/apps/Frontend/src/pages/settings-page.tsx @@ -10,8 +10,8 @@ import { z } from "zod"; import { apiRequest, queryClient } from "@/lib/queryClient"; import { StaffForm } from "@/components/staffs/staff-form"; import { DeleteConfirmationDialog } from "@/components/ui/deleteDialog"; -import { CredentialForm } from "@/components/settings/InsuranceCredForm"; import { CredentialTable } from "@/components/settings/insuranceCredTable"; +import { useAuth } from "@/hooks/use-auth"; // Correctly infer Staff type from zod schema type Staff = z.infer; @@ -221,42 +221,23 @@ export default function SettingsPage() { ); // MANAGE USER - const [usernameUser, setUsernameUser] = useState(""); //fetch user - const { - data: currentUser, - isLoading: isUserLoading, - isError: isUserError, - error: userError, - } = useQuery({ - queryKey: ["/api/users/"], - queryFn: async () => { - const res = await apiRequest("GET", "/api/users/"); - if (!res.ok) throw new Error("Failed to fetch user"); - return res.json(); - }, - }); - - // Populate fields after fetch + const { user } = useAuth(); useEffect(() => { - if (currentUser) { - setUsernameUser(currentUser.username); + if (user?.username) { + setUsernameUser(user.username); } - }, [currentUser]); + }, [user]); //update user mutation const updateUserMutate = useMutation({ mutationFn: async ( updates: Partial<{ username: string; password: string }> ) => { - if (!currentUser?.id) throw new Error("User not loaded"); - const res = await apiRequest( - "PUT", - `/api/users/${currentUser.id}`, - updates - ); + if (!user?.id) throw new Error("User not loaded"); + const res = await apiRequest("PUT", `/api/users/${user.id}`, updates); if (!res.ok) { const errorData = await res.json().catch(() => null); throw new Error(errorData?.error || "Failed to update user"); @@ -280,7 +261,6 @@ export default function SettingsPage() { }, }); - return (

User Settings

+
{ + e.preventDefault(); + const formData = new FormData(e.currentTarget); + const password = + formData.get("password")?.toString().trim() || undefined; - {isUserLoading ? ( -

Loading user...

- ) : isUserError ? ( -

{(userError as Error)?.message}

- ) : ( - { - e.preventDefault(); - const formData = new FormData(e.currentTarget); - const password = - formData.get("password")?.toString().trim() || undefined; + updateUserMutate.mutate({ + username: usernameUser?.trim() || undefined, + password: password || undefined, + }); + }} + > +
+ + setUsernameUser(e.target.value)} + className="mt-1 p-2 border rounded w-full" + /> +
- updateUserMutate.mutate({ - username: usernameUser?.trim() || undefined, - password: password || undefined, - }); - }} +
+ + +

+ Leave blank to keep current password. +

+
+ + -
- )} + {updateUserMutate.isPending ? "Saving..." : "Save Changes"} + +