fixed query

This commit is contained in:
2025-07-28 18:29:37 +05:30
parent b9b449a909
commit aa1005a7a8

View File

@@ -10,8 +10,8 @@ import { z } from "zod";
import { apiRequest, queryClient } from "@/lib/queryClient"; import { apiRequest, queryClient } from "@/lib/queryClient";
import { StaffForm } from "@/components/staffs/staff-form"; import { StaffForm } from "@/components/staffs/staff-form";
import { DeleteConfirmationDialog } from "@/components/ui/deleteDialog"; import { DeleteConfirmationDialog } from "@/components/ui/deleteDialog";
import { CredentialForm } from "@/components/settings/InsuranceCredForm";
import { CredentialTable } from "@/components/settings/insuranceCredTable"; import { CredentialTable } from "@/components/settings/insuranceCredTable";
import { useAuth } from "@/hooks/use-auth";
// Correctly infer Staff type from zod schema // Correctly infer Staff type from zod schema
type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>; type Staff = z.infer<typeof StaffUncheckedCreateInputObjectSchema>;
@@ -221,42 +221,23 @@ export default function SettingsPage() {
); );
// MANAGE USER // MANAGE USER
const [usernameUser, setUsernameUser] = useState(""); const [usernameUser, setUsernameUser] = useState("");
//fetch user //fetch user
const { const { user } = useAuth();
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
useEffect(() => { useEffect(() => {
if (currentUser) { if (user?.username) {
setUsernameUser(currentUser.username); setUsernameUser(user.username);
} }
}, [currentUser]); }, [user]);
//update user mutation //update user mutation
const updateUserMutate = useMutation({ const updateUserMutate = useMutation({
mutationFn: async ( mutationFn: async (
updates: Partial<{ username: string; password: string }> updates: Partial<{ username: string; password: string }>
) => { ) => {
if (!currentUser?.id) throw new Error("User not loaded"); if (!user?.id) throw new Error("User not loaded");
const res = await apiRequest( const res = await apiRequest("PUT", `/api/users/${user.id}`, updates);
"PUT",
`/api/users/${currentUser.id}`,
updates
);
if (!res.ok) { if (!res.ok) {
const errorData = await res.json().catch(() => null); const errorData = await res.json().catch(() => null);
throw new Error(errorData?.error || "Failed to update user"); throw new Error(errorData?.error || "Failed to update user");
@@ -280,7 +261,6 @@ export default function SettingsPage() {
}, },
}); });
return ( return (
<div className="flex h-screen overflow-hidden bg-gray-100"> <div className="flex h-screen overflow-hidden bg-gray-100">
<Sidebar <Sidebar
@@ -339,12 +319,6 @@ export default function SettingsPage() {
<Card className="mt-6"> <Card className="mt-6">
<CardContent className="space-y-4 py-6"> <CardContent className="space-y-4 py-6">
<h3 className="text-lg font-semibold">User Settings</h3> <h3 className="text-lg font-semibold">User Settings</h3>
{isUserLoading ? (
<p>Loading user...</p>
) : isUserError ? (
<p className="text-red-500">{(userError as Error)?.message}</p>
) : (
<form <form
className="space-y-4" className="space-y-4"
onSubmit={(e) => { onSubmit={(e) => {
@@ -360,9 +334,7 @@ export default function SettingsPage() {
}} }}
> >
<div> <div>
<label className="block text-sm font-medium"> <label className="block text-sm font-medium">Username</label>
Username
</label>
<input <input
type="text" type="text"
name="username" name="username"
@@ -395,7 +367,6 @@ export default function SettingsPage() {
{updateUserMutate.isPending ? "Saving..." : "Save Changes"} {updateUserMutate.isPending ? "Saving..." : "Save Changes"}
</button> </button>
</form> </form>
)}
</CardContent> </CardContent>
</Card> </Card>