import { ReactElement, useEffect, useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { Controller, useForm } from "react-hook-form"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // services import { UserService } from "services/user.service"; // hooks import useToast from "hooks/use-toast"; // layout import { ProfileSettingsLayout } from "layouts/settings-layout"; // ui import { Button, Input, Spinner } from "@plane/ui"; // types import { NextPageWithLayout } from "types/app"; interface FormValues { old_password: string; new_password: string; confirm_password: string; } const defaultValues: FormValues = { old_password: "", new_password: "", confirm_password: "", }; const userService = new UserService(); const ChangePasswordPage: NextPageWithLayout = observer(() => { const [isPageLoading, setIsPageLoading] = useState(true); const { user: { currentUser }, } = useMobxStore(); const router = useRouter(); // use form const { control, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const { setToastAlert } = useToast(); const handleChangePassword = async (formData: FormValues) => { if (formData.new_password !== formData.confirm_password) { setToastAlert({ type: "error", title: "Error!", message: "The new password and the confirm password don't match.", }); return; } await userService .changePassword(formData) .then(() => { setToastAlert({ type: "success", title: "Success!", message: "Password changed successfully.", }); }) .catch((error) => { setToastAlert({ type: "error", title: "Error!", message: error?.error ?? "Something went wrong. Please try again.", }); }); }; useEffect(() => { if (!currentUser) return; if (currentUser.is_password_autoset) router.push("/profile"); else setIsPageLoading(false); }, [currentUser, router]); if (isPageLoading) return (
); return (

Change password

Current password

( )} /> {errors.old_password && {errors.old_password.message}}

New password

( )} /> {errors.new_password && {errors.new_password.message}}

Confirm password

( )} /> {errors.confirm_password && {errors.confirm_password.message}}
); }); ChangePasswordPage.getLayout = function getLayout(page: ReactElement) { return {page}; }; export default ChangePasswordPage;