im afraid we cant move forward if you are not able to resolve the simple

This commit is contained in:
Leon Serfaty G
2025-07-17 11:54:26 +00:00
parent 3f9ee7d79e
commit 9aebf5b604
2 changed files with 52 additions and 18 deletions
+28 -3
View File
@@ -16,8 +16,9 @@ import {
import { Input } from "@/components/ui/input"; import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label"; import { Label } from "@/components/ui/label";
import { useToast } from "@/hooks/use-toast"; import { useToast } from "@/hooks/use-toast";
import { useEffect, useTransition } from "react"; import { useEffect, useState, useTransition } from "react";
import { getUser, updateUser } from "@/lib/actions/user"; import { getUser, updateUser } from "@/lib/actions/user";
import { Skeleton } from "@/components/ui/skeleton";
const userProfileSchema = z.object({ const userProfileSchema = z.object({
name: z.string().min(1, "Name is required"), name: z.string().min(1, "Name is required"),
@@ -34,6 +35,7 @@ type UserProfileFormValues = z.infer<typeof userProfileSchema>;
export default function UserProfilePage() { export default function UserProfilePage() {
const { toast } = useToast(); const { toast } = useToast();
const [isSaving, startSavingTransition] = useTransition(); const [isSaving, startSavingTransition] = useTransition();
const [isLoading, setIsLoading] = useState(true);
const { const {
register, register,
@@ -52,6 +54,7 @@ export default function UserProfilePage() {
useEffect(() => { useEffect(() => {
const fetchUser = async () => { const fetchUser = async () => {
setIsLoading(true);
const user = await getUser(); const user = await getUser();
if (user) { if (user) {
reset({ reset({
@@ -60,10 +63,17 @@ export default function UserProfilePage() {
password: "", password: "",
confirmPassword: "" confirmPassword: ""
}); });
} else {
toast({
title: "Error",
description: "Could not load user profile.",
variant: "destructive",
});
} }
setIsLoading(false);
}; };
fetchUser(); fetchUser();
}, [reset]); }, [reset, toast]);
const onSubmit: SubmitHandler<UserProfileFormValues> = (data) => { const onSubmit: SubmitHandler<UserProfileFormValues> = (data) => {
startSavingTransition(async () => { startSavingTransition(async () => {
@@ -99,6 +109,19 @@ export default function UserProfilePage() {
</CardDescription> </CardDescription>
</CardHeader> </CardHeader>
<CardContent className="grid gap-6"> <CardContent className="grid gap-6">
{isLoading ? (
<>
<div className="grid gap-2">
<Label htmlFor="name">Name</Label>
<Skeleton className="h-10 w-full" />
</div>
<div className="grid gap-2">
<Label htmlFor="email">Email</Label>
<Skeleton className="h-10 w-full" />
</div>
</>
) : (
<>
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="name">Name</Label> <Label htmlFor="name">Name</Label>
<Input id="name" {...register("name")} /> <Input id="name" {...register("name")} />
@@ -109,6 +132,8 @@ export default function UserProfilePage() {
<Input id="email" type="email" {...register("email")} /> <Input id="email" type="email" {...register("email")} />
{errors.email && <p className="text-sm text-destructive">{errors.email.message}</p>} {errors.email && <p className="text-sm text-destructive">{errors.email.message}</p>}
</div> </div>
</>
)}
<div className="grid gap-2"> <div className="grid gap-2">
<Label htmlFor="password">New Password</Label> <Label htmlFor="password">New Password</Label>
<Input id="password" type="password" {...register("password")} /> <Input id="password" type="password" {...register("password")} />
@@ -121,7 +146,7 @@ export default function UserProfilePage() {
</div> </div>
</CardContent> </CardContent>
<CardFooter className="flex justify-end"> <CardFooter className="flex justify-end">
<Button type="submit" disabled={isSaving || !isDirty}> <Button type="submit" disabled={isSaving || !isDirty || isLoading}>
{isSaving ? 'Saving...' : 'Save Changes'} {isSaving ? 'Saving...' : 'Save Changes'}
</Button> </Button>
</CardFooter> </CardFooter>
+14 -5
View File
@@ -2,9 +2,9 @@
'use server'; 'use server';
import db from '@/lib/db'; import db from '@/lib/db';
import type { User as DbUser } from '@/lib/types';
import { z } from 'zod'; import { z } from 'zod';
import { auth } from '@/app/api/auth/[...nextauth]/route'; import { auth } from '@/app/api/auth/[...nextauth]/route';
import { User as DbUser } from '@/lib/types';
const UserUpdateSchema = z.object({ const UserUpdateSchema = z.object({
name: z.string().min(1, 'Name is required'), name: z.string().min(1, 'Name is required'),
@@ -12,18 +12,26 @@ const UserUpdateSchema = z.object({
password: z.string().optional(), password: z.string().optional(),
}); });
type UserWithId = DbUser & { id: string }; type UserForClient = {
id: string;
name: string;
email: string;
}
export async function getUser(): Promise<UserWithId | null> { export async function getUser(): Promise<UserForClient | null> {
const session = await auth(); const session = await auth();
if (!session?.user?.id) { if (!session?.user?.id) {
console.error('getUser: Not authenticated');
return null; return null;
} }
try { try {
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?'); const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
const user = stmt.get(session.user.id) as DbUser | undefined; const user = stmt.get(session.user.id) as DbUser | undefined;
if (!user) return null; if (!user) {
return { ...user, id: user.id.toString() }; console.error('getUser: User not found in DB');
return null;
}
return { id: user.id.toString(), name: user.name, email: user.email };
} catch (error) { } catch (error) {
console.error('Failed to get user:', error); console.error('Failed to get user:', error);
return null; return null;
@@ -50,6 +58,7 @@ export async function updateUser(
try { try {
if (password && password.trim().length > 0) { if (password && password.trim().length > 0) {
// In a real app, you would hash the password here
const stmt = db.prepare( const stmt = db.prepare(
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?' 'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
); );