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 { 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<typeof StaffUncheckedCreateInputObjectSchema>;
@@ -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 (
<div className="flex h-screen overflow-hidden bg-gray-100">
<Sidebar
@@ -339,12 +319,6 @@ export default function SettingsPage() {
<Card className="mt-6">
<CardContent className="space-y-4 py-6">
<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
className="space-y-4"
onSubmit={(e) => {
@@ -360,9 +334,7 @@ export default function SettingsPage() {
}}
>
<div>
<label className="block text-sm font-medium">
Username
</label>
<label className="block text-sm font-medium">Username</label>
<input
type="text"
name="username"
@@ -395,7 +367,6 @@ export default function SettingsPage() {
{updateUserMutate.isPending ? "Saving..." : "Save Changes"}
</button>
</form>
)}
</CardContent>
</Card>