el.xwx.moe/components/Modal/User/ChangePassword.tsx

116 lines
3.1 KiB
TypeScript
Raw Normal View History

import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { AccountSettings } from "@/types/global";
import useAccountStore from "@/store/account";
2023-07-19 10:30:11 -05:00
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";
2023-08-17 15:05:44 -05:00
import TextInput from "@/components/TextInput";
type Props = {
togglePasswordFormModal: Function;
setUser: Dispatch<SetStateAction<AccountSettings>>;
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();
2023-07-19 15:39:59 -05:00
const { update, data } = useSession();
useEffect(() => {
if (
2023-07-12 13:26:34 -05:00
!(newPassword == "" || newPassword2 == "") &&
newPassword === newPassword2
) {
2023-07-12 13:26:34 -05:00
setUser({ ...user, newPassword });
}
2023-07-12 13:26:34 -05:00
}, [newPassword, newPassword2]);
const submit = async () => {
2023-07-12 13:26:34 -05:00
if (newPassword == "" || newPassword2 == "") {
toast.error("Please fill all the fields.");
}
2023-07-19 16:08:05 -05:00
if (newPassword !== newPassword2)
return toast.error("Passwords do not match.");
else if (newPassword.length < 8)
return toast.error("Passwords must be at least 8 characters.");
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) {
update({
id: data?.user.id,
});
signOut();
} else if (
user.username !== account.username ||
user.name !== account.name
)
update({
id: data?.user.id,
});
setUser({ ...user, newPassword: undefined });
togglePasswordFormModal();
} else toast.error(response.data as string);
2023-07-19 13:00:45 -05:00
setSubmitLoader(false);
};
return (
<div className="mx-auto sm:w-[35rem] w-80">
2023-08-17 15:05:44 -05:00
<div className="max-w-[25rem] w-full mx-auto flex flex-col gap-2 justify-between">
2023-08-11 00:11:02 -05:00
<p className="text-sm text-black dark:text-white">New Password</p>
2023-08-17 15:05:44 -05:00
<TextInput
value={newPassword}
onChange={(e) => setNewPassword1(e.target.value)}
placeholder="••••••••••••••"
2023-08-17 15:05:44 -05:00
type="password"
/>
2023-08-17 15:05:44 -05:00
2023-08-11 00:11:02 -05:00
<p className="text-sm text-black dark:text-white">
2023-08-10 23:44:44 -05:00
Confirm New Password
</p>
2023-08-17 15:05:44 -05:00
<TextInput
value={newPassword2}
onChange={(e) => setNewPassword2(e.target.value)}
placeholder="••••••••••••••"
2023-08-17 15:05:44 -05:00
type="password"
/>
<SubmitButton
onClick={submit}
loading={submitLoader}
label="Apply Settings"
icon={faPenToSquare}
className="mx-auto mt-2"
/>
</div>
</div>
);
}