2023-10-16 12:10:52 -05:00
|
|
|
import SettingsLayout from "@/layouts/SettingsLayout";
|
2023-10-18 16:50:55 -05:00
|
|
|
import { useState } from "react";
|
|
|
|
import SubmitButton from "@/components/SubmitButton";
|
|
|
|
import { toast } from "react-hot-toast";
|
|
|
|
import TextInput from "@/components/TextInput";
|
2024-06-04 15:59:49 -05:00
|
|
|
import { useTranslation } from "next-i18next";
|
|
|
|
import getServerSideProps from "@/lib/client/getServerSideProps";
|
2024-07-31 13:15:50 -05:00
|
|
|
import { useUpdateUser, useUser } from "@/hooks/store/user";
|
2023-10-16 12:10:52 -05:00
|
|
|
|
2023-10-18 23:09:28 -05:00
|
|
|
export default function Password() {
|
2024-06-04 15:59:49 -05:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
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);
|
2024-08-01 17:40:08 -05:00
|
|
|
const { data: account } = useUser();
|
2024-07-30 22:19:29 -05:00
|
|
|
const updateUser = useUpdateUser();
|
2023-10-18 16:50:55 -05:00
|
|
|
|
|
|
|
const submit = async () => {
|
2024-06-04 15:59:49 -05:00
|
|
|
if (newPassword === "" || oldPassword === "") {
|
|
|
|
return toast.error(t("fill_all_fields"));
|
2023-10-18 16:50:55 -05:00
|
|
|
}
|
2024-06-04 15:59:49 -05:00
|
|
|
if (newPassword.length < 8) return toast.error(t("password_length_error"));
|
2023-10-18 16:50:55 -05:00
|
|
|
|
|
|
|
setSubmitLoader(true);
|
|
|
|
|
2024-07-30 22:19:29 -05:00
|
|
|
await updateUser.mutateAsync(
|
|
|
|
{
|
|
|
|
...account,
|
|
|
|
newPassword,
|
|
|
|
oldPassword,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
onSuccess: () => {
|
|
|
|
setNewPassword("");
|
|
|
|
setOldPassword("");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2023-10-18 16:50:55 -05:00
|
|
|
|
|
|
|
setSubmitLoader(false);
|
|
|
|
};
|
|
|
|
|
2023-10-16 12:10:52 -05:00
|
|
|
return (
|
|
|
|
<SettingsLayout>
|
2024-06-04 15:59:49 -05:00
|
|
|
<p className="capitalize text-3xl font-thin inline">
|
|
|
|
{t("change_password")}
|
|
|
|
</p>
|
2023-11-20 11:48:41 -06:00
|
|
|
|
2023-12-01 11:01:56 -06:00
|
|
|
<div className="divider my-3"></div>
|
2023-11-20 11:48:41 -06:00
|
|
|
|
2024-06-04 15:59:49 -05:00
|
|
|
<p className="mb-3">{t("password_change_instructions")}</p>
|
2023-10-18 16:50:55 -05:00
|
|
|
<div className="w-full flex flex-col gap-2 justify-between">
|
2024-06-04 15:59:49 -05:00
|
|
|
<p>{t("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-06-04 15:59:49 -05:00
|
|
|
<p className="mt-3">{t("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}
|
2024-06-04 15:59:49 -05:00
|
|
|
label={t("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>
|
2023-10-16 12:10:52 -05:00
|
|
|
</SettingsLayout>
|
|
|
|
);
|
|
|
|
}
|
2024-06-04 15:59:49 -05:00
|
|
|
|
|
|
|
export { getServerSideProps };
|