import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
import { Head, useForm } from "@inertiajs/react";
import { User, Lock, Mail } from "lucide-react";
import { FormEvent } from "react";
import { toast } from "sonner";
import { Button } from "@/Components/ui/button";
interface User {
id: number;
name: string;
email: string | null;
username: string;
}
interface Props {
user: User;
}
export default function Edit({ user }: Props) {
// 個人資料表單
const { data: profileData, setData: setProfileData, patch: patchProfile, processing: profileProcessing, errors: profileErrors } = useForm({
name: user.name,
username: user.username,
email: user.email || "",
});
// 密碼表單
const { data: passwordData, setData: setPasswordData, put: putPassword, processing: passwordProcessing, errors: passwordErrors, reset: resetPassword } = useForm({
current_password: "",
password: "",
password_confirmation: "",
});
const handleProfileSubmit = (e: FormEvent) => {
e.preventDefault();
patchProfile(route('profile.update'), {
onSuccess: () => toast.success('個人資料已更新'),
onError: () => toast.error('更新失敗,請檢查輸入內容'),
});
};
const handlePasswordSubmit = (e: FormEvent) => {
e.preventDefault();
putPassword(route('profile.password'), {
onSuccess: () => {
toast.success('密碼已更新');
resetPassword();
},
onError: () => toast.error('密碼更新失敗'),
});
};
return (
管理您的個人資料與帳號安全