2023-06-09 17:31:14 -05:00
|
|
|
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
|
|
|
import { AccountSettings } from "@/types/global";
|
|
|
|
import useAccountStore from "@/store/account";
|
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
import { faPenToSquare } from "@fortawesome/free-regular-svg-icons";
|
|
|
|
import SubmitButton from "@/components/SubmitButton";
|
2023-06-26 17:33:40 -05:00
|
|
|
import { toast } from "react-hot-toast";
|
2023-06-09 17:31:14 -05:00
|
|
|
|
|
|
|
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("");
|
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
const [submitLoader, setSubmitLoader] = useState(false);
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
const { account, updateAccount } = useAccountStore();
|
|
|
|
const { update } = useSession();
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (
|
2023-07-12 13:26:34 -05:00
|
|
|
!(newPassword == "" || newPassword2 == "") &&
|
2023-06-09 17:31:14 -05:00
|
|
|
newPassword === newPassword2
|
|
|
|
) {
|
2023-07-12 13:26:34 -05:00
|
|
|
setUser({ ...user, newPassword });
|
2023-06-09 17:31:14 -05:00
|
|
|
}
|
2023-07-12 13:26:34 -05:00
|
|
|
}, [newPassword, newPassword2]);
|
2023-06-09 17:31:14 -05:00
|
|
|
|
|
|
|
const submit = async () => {
|
2023-07-12 13:26:34 -05:00
|
|
|
if (newPassword == "" || newPassword2 == "") {
|
2023-06-26 17:33:40 -05:00
|
|
|
toast.error("Please fill all the fields.");
|
2023-06-09 17:31:14 -05:00
|
|
|
} else if (newPassword === newPassword2) {
|
2023-06-26 17:33:40 -05:00
|
|
|
setSubmitLoader(true);
|
|
|
|
|
|
|
|
const load = toast.loading("Applying...");
|
|
|
|
|
2023-06-09 17:31:14 -05:00
|
|
|
const response = await updateAccount({
|
|
|
|
...user,
|
|
|
|
});
|
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
toast.dismiss(load);
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
toast.success("Settings Applied!");
|
|
|
|
togglePasswordFormModal();
|
|
|
|
} else toast.error(response.data as string);
|
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
|
2023-07-12 13:26:34 -05:00
|
|
|
if (
|
|
|
|
(user.username !== account.username || user.name !== account.name) &&
|
|
|
|
user.username &&
|
|
|
|
user.email
|
|
|
|
)
|
2023-07-08 05:35:43 -05:00
|
|
|
update({ username: user.username, name: user.name });
|
2023-06-09 17:31:14 -05:00
|
|
|
|
2023-06-26 17:33:40 -05:00
|
|
|
if (response.ok) {
|
2023-07-12 13:26:34 -05:00
|
|
|
setUser({ ...user, newPassword: undefined });
|
2023-06-09 17:31:14 -05:00
|
|
|
togglePasswordFormModal();
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-26 17:33:40 -05:00
|
|
|
toast.error("Passwords do not match.");
|
2023-06-09 17:31:14 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-06-26 17:33:40 -05:00
|
|
|
<div className="mx-auto sm:w-[35rem] w-80">
|
|
|
|
<div className="max-w-[25rem] w-full mx-auto flex flex-col gap-3 justify-between">
|
|
|
|
<p className="text-sm text-sky-500">New Password</p>
|
|
|
|
|
|
|
|
<input
|
|
|
|
value={newPassword}
|
|
|
|
onChange={(e) => setNewPassword1(e.target.value)}
|
|
|
|
type="password"
|
2023-07-13 17:19:49 -05:00
|
|
|
placeholder="***********"
|
2023-06-26 17:33:40 -05:00
|
|
|
className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
|
|
|
|
/>
|
2023-07-13 17:19:49 -05:00
|
|
|
<p className="text-sm text-sky-500">Confirm New Password</p>
|
2023-06-26 17:33:40 -05:00
|
|
|
|
|
|
|
<input
|
|
|
|
value={newPassword2}
|
|
|
|
onChange={(e) => setNewPassword2(e.target.value)}
|
|
|
|
type="password"
|
2023-07-13 17:19:49 -05:00
|
|
|
placeholder="***********"
|
2023-06-26 17:33:40 -05:00
|
|
|
className="w-full rounded-md p-3 mx-auto border-sky-100 border-solid border outline-none focus:border-sky-500 duration-100"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<SubmitButton
|
|
|
|
onClick={submit}
|
|
|
|
loading={submitLoader}
|
|
|
|
label="Apply Settings"
|
|
|
|
icon={faPenToSquare}
|
|
|
|
className="mx-auto mt-2"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-06-09 17:31:14 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|