From 5c0991f83701fdb28ff7de14ad1cbadf2df90abf Mon Sep 17 00:00:00 2001 From: Leon Serfaty G Date: Fri, 18 Jul 2025 03:01:54 +0000 Subject: [PATCH] oit says profile succesfully updated, yet when I reload nothing is saved --- src/app/admin/settings/user/page.tsx | 25 ++-------- src/lib/actions/user.ts | 74 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 src/lib/actions/user.ts diff --git a/src/app/admin/settings/user/page.tsx b/src/app/admin/settings/user/page.tsx index 052b47e..797cc9c 100644 --- a/src/app/admin/settings/user/page.tsx +++ b/src/app/admin/settings/user/page.tsx @@ -7,7 +7,6 @@ import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; -import { Label } from '@/components/ui/label'; import { useToast } from '@/hooks/use-toast'; import { Card, @@ -17,6 +16,7 @@ import { CardDescription, } from '@/components/ui/card'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; +import { getUser, updateUser } from '@/lib/actions/user'; const formSchema = z.object({ name: z.string().min(1, 'Name is required'), @@ -26,27 +26,6 @@ const formSchema = z.object({ type UserFormValues = z.infer; -// Mock server actions -async function getUser() { - console.log("Mocking getUser"); - return { id: '1', name: 'Admin User', email: 'admin@example.com' }; -} - -async function updateUser(data: UserFormValues) { - console.log("Mocking updateUser with data:", data); - // Simulate API call - return new Promise<{ success: boolean; message: string }>((resolve) => { - setTimeout(() => { - if (data.email.includes('fail')) { - resolve({ success: false, message: 'This email is already taken.' }); - } else { - resolve({ success: true, message: 'Profile updated successfully!' }); - } - }, 1000); - }); -} - - export default function UserProfilePage() { const { toast } = useToast(); const form = useForm({ @@ -68,6 +47,8 @@ export default function UserProfilePage() { email: user.email, password: '', }); + } else { + throw new Error('User not found.'); } } catch (error) { toast({ diff --git a/src/lib/actions/user.ts b/src/lib/actions/user.ts new file mode 100644 index 0000000..7f3a543 --- /dev/null +++ b/src/lib/actions/user.ts @@ -0,0 +1,74 @@ + +'use server'; + +import { z } from 'zod'; +import db from '@/lib/db'; +import { revalidatePath } from 'next/cache'; + +const formSchema = z.object({ + name: z.string().min(1, 'Name is required'), + email: z.string().email('Invalid email address'), + password: z.string().optional(), +}); + +type UserFormValues = z.infer; + +/** + * Gets the user from the database. + * Since authentication isn't fully implemented, it defaults to the user with id 1. + */ +export async function getUser(): Promise<{ id: number; name: string; email: string } | null> { + try { + const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?'); + // For now, we'll hardcode the user ID to 1 as login is simulated. + const user = stmt.get(1) as { id: number; name: string; email: string } | undefined; + if (!user) { + return null; + } + return user; + } catch (error) { + console.error('Failed to get user:', error); + return null; + } +} + +/** + * Updates a user's profile information in the database. + */ +export async function updateUser(data: UserFormValues): Promise<{ success: boolean; message: string }> { + const validation = formSchema.safeParse(data); + if (!validation.success) { + return { success: false, message: 'Invalid data provided.' }; + } + + const { name, email, password } = validation.data; + + try { + // For now, we'll assume we're updating the user with ID 1. + const userId = 1; + + // Check if the new email is already taken by another user + const checkEmailStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?'); + const existingUser = checkEmailStmt.get(email, userId); + + if (existingUser) { + return { success: false, message: 'This email is already taken.' }; + } + + if (password) { + // If a new password is provided, update it along with name and email + const stmt = db.prepare('UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'); + stmt.run(name, email, password, userId); + } else { + // If no new password, only update name and email + const stmt = db.prepare('UPDATE users SET name = ?, email = ? WHERE id = ?'); + stmt.run(name, email, userId); + } + + revalidatePath('/admin/settings/user'); + return { success: true, message: 'Profile updated successfully!' }; + } catch (error) { + console.error('Failed to update user:', error); + return { success: false, message: 'An unexpected error occurred.' }; + } +}