el.xwx.moe/pages/settings/password.tsx

85 lines
2.3 KiB
TypeScript
Raw Normal View History

import SettingsLayout from "@/layouts/SettingsLayout";
2023-10-18 16:50:55 -05:00
import { useState } from "react";
import useAccountStore from "@/store/account";
import SubmitButton from "@/components/SubmitButton";
import { toast } from "react-hot-toast";
import TextInput from "@/components/TextInput";
2023-10-18 23:09:28 -05:00
export default function Password() {
2024-05-21 06:08:08 -05:00
const [oldPassword, setOldPassword] = useState("");
const [newPassword, setNewPassword] = useState("");
2023-10-18 16:50:55 -05:00
const [submitLoader, setSubmitLoader] = useState(false);
const { account, updateAccount } = useAccountStore();
const submit = async () => {
2024-05-21 06:08:08 -05:00
if (newPassword == "" || oldPassword == "") {
return toast.error("Please fill all the fields.");
2023-10-18 16:50:55 -05:00
}
2024-05-21 06:08:08 -05:00
if (newPassword.length < 8)
2023-10-18 16:50:55 -05:00
return toast.error("Passwords must be at least 8 characters.");
setSubmitLoader(true);
const load = toast.loading("Applying...");
const response = await updateAccount({
...account,
newPassword,
2024-05-21 06:08:08 -05:00
oldPassword,
2023-10-18 16:50:55 -05:00
});
toast.dismiss(load);
if (response.ok) {
toast.success("Settings Applied!");
2024-05-21 06:08:08 -05:00
setNewPassword("");
setOldPassword("");
2023-10-18 16:50:55 -05:00
} else toast.error(response.data as string);
setSubmitLoader(false);
};
return (
<SettingsLayout>
2023-11-20 11:48:41 -06:00
<p className="capitalize text-3xl font-thin inline">Change Password</p>
<div className="divider my-3"></div>
2023-11-20 11:48:41 -06:00
2023-10-18 16:50:55 -05:00
<p className="mb-3">
To change your password, please fill out the following. Your password
should be at least 8 characters.
</p>
<div className="w-full flex flex-col gap-2 justify-between">
2024-05-21 06:08:08 -05:00
<p>Old Password</p>
2023-10-18 16:50:55 -05:00
<TextInput
2024-05-21 06:08:08 -05:00
value={oldPassword}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2024-05-21 06:08:08 -05:00
onChange={(e) => setOldPassword(e.target.value)}
2023-10-18 16:50:55 -05:00
placeholder="••••••••••••••"
type="password"
/>
2024-05-21 06:08:08 -05:00
<p className="mt-3">New Password</p>
2023-10-18 16:50:55 -05:00
<TextInput
2024-05-21 06:08:08 -05:00
value={newPassword}
2023-12-01 13:00:52 -06:00
className="bg-base-200"
2024-05-21 06:08:08 -05:00
onChange={(e) => setNewPassword(e.target.value)}
2023-10-18 16:50:55 -05:00
placeholder="••••••••••••••"
type="password"
/>
<SubmitButton
onClick={submit}
loading={submitLoader}
label="Save Changes"
2024-05-21 06:08:08 -05:00
className="mt-3 w-full sm:w-fit"
2023-10-18 16:50:55 -05:00
/>
</div>
</SettingsLayout>
);
}