import { Dispatch, SetStateAction, useEffect, useState } from "react"; import { AccountSettings } from "@/types/global"; import useAccountStore from "@/store/account"; import { signOut, useSession } from "next-auth/react"; import { faPenToSquare } from "@fortawesome/free-regular-svg-icons"; import SubmitButton from "@/components/SubmitButton"; import { toast } from "react-hot-toast"; type Props = { togglePasswordFormModal: Function; setUser: Dispatch>; user: AccountSettings; }; export default function ChangePassword({ togglePasswordFormModal, setUser, user, }: Props) { const [newPassword, setNewPassword1] = useState(""); const [newPassword2, setNewPassword2] = useState(""); const [submitLoader, setSubmitLoader] = useState(false); const { account, updateAccount } = useAccountStore(); const { update } = useSession(); useEffect(() => { if ( !(newPassword == "" || newPassword2 == "") && newPassword === newPassword2 ) { setUser({ ...user, newPassword }); } }, [newPassword, newPassword2]); const submit = async () => { if (newPassword == "" || newPassword2 == "") { toast.error("Please fill all the fields."); } else if (newPassword === newPassword2) { setSubmitLoader(true); const load = toast.loading("Applying..."); const response = await updateAccount({ ...user, }); toast.dismiss(load); if (response.ok) { toast.success("Settings Applied!"); if ( user.email !== account.email || user.username !== account.username || user.name !== account.name ) { update({ username: user.username, email: user.email, name: user.name, }); signOut(); } setUser({ ...user, newPassword: undefined }); togglePasswordFormModal(); } else toast.error(response.data as string); } else { toast.error("Passwords do not match."); } setSubmitLoader(false); }; return (

New Password

setNewPassword1(e.target.value)} type="password" placeholder="••••••••••••••" className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />

Confirm New Password

setNewPassword2(e.target.value)} type="password" placeholder="••••••••••••••" className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100" />
); }