// File: app/(dashboard)/profile/page.tsx "use client"; import { useState } from "react"; import { useSession } from "next-auth/react"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import * as z from "zod"; import { Loader2, User, Shield, Bell } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Switch } from "@/components/ui/switch"; import apiClient from "@/lib/api/client"; // ----------------------------------------------------------------------------- // Schemas // ----------------------------------------------------------------------------- const passwordSchema = z .object({ currentPassword: z.string().min(1, "กรุณาระบุรหัสผ่านปัจจุบัน"), newPassword: z.string().min(8, "รหัสผ่านใหม่ต้องมีอย่างน้อย 8 ตัวอักษร"), confirmPassword: z.string().min(1, "กรุณายืนยันรหัสผ่านใหม่"), }) .refine((data) => data.newPassword === data.confirmPassword, { message: "รหัสผ่านใหม่ไม่ตรงกัน", path: ["confirmPassword"], }); type PasswordValues = z.infer; export default function ProfilePage() { const { data: session } = useSession(); const [isLoading, setIsLoading] = useState(false); // --- Password Form Handling --- const { register, handleSubmit, reset, formState: { errors }, } = useForm({ resolver: zodResolver(passwordSchema), }); const onPasswordSubmit = async (data: PasswordValues) => { setIsLoading(true); try { // เรียก API เปลี่ยนรหัสผ่าน await apiClient.put("/users/change-password", { currentPassword: data.currentPassword, newPassword: data.newPassword, }); alert("เปลี่ยนรหัสผ่านสำเร็จ"); // ในอนาคตใช้ Toast reset(); } catch (error) { console.error(error); alert("ไม่สามารถเปลี่ยนรหัสผ่านได้: รหัสผ่านปัจจุบันไม่ถูกต้อง"); } finally { setIsLoading(false); } }; // --- Notification State (Mockup) --- // ในการใช้งานจริง ควรดึงค่าจาก API /users/preferences หรือ UserPreferenceService const [notifyEmail, setNotifyEmail] = useState(true); const [notifyLine, setNotifyLine] = useState(true); const [digestMode, setDigestMode] = useState(false); // Helper to get initials const userName = session?.user?.name || "User"; const userInitials = userName .split(" ") .map((n) => n[0]) .join("") .toUpperCase() .substring(0, 2); return (

Profile & Settings

จัดการข้อมูลส่วนตัวและการตั้งค่าระบบของคุณ

General Security Notifications {/* 1. General Tab */} ข้อมูลทั่วไป ข้อมูลพื้นฐานของคุณที่แสดงในระบบ
{userInitials}

{userName}

{session?.user?.email}

{session?.user?.role || "Member"}
{/* ในอนาคตดึงจาก Organization ID */}
{/* */}
{/* 2. Security Tab */} รหัสผ่าน เปลี่ยนรหัสผ่านเพื่อความปลอดภัยของบัญชี (ต้องมีอย่างน้อย 8 ตัวอักษร)
{errors.currentPassword && (

{errors.currentPassword.message}

)}
{errors.newPassword && (

{errors.newPassword.message}

)}
{errors.confirmPassword && (

{errors.confirmPassword.message}

)}
{/* 3. Notifications Tab */} การแจ้งเตือน กำหนดช่องทางที่คุณต้องการรับข้อมูลข่าวสารจากระบบ
); }