import { ReactElement, useEffect, useState } from "react"; import { observer } from "mobx-react-lite"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; // hooks import { Button, Input, Spinner, TOAST_TYPE, setPromiseToast, setToast } from "@plane/ui"; import { PageHead } from "@/components/core"; import { SidebarHamburgerToggle } from "@/components/core/sidebar/sidebar-menu-hamburger-toggle"; import { useApplication, useUser } from "@/hooks/store"; // services // components // layout import { ProfileSettingsLayout } from "@/layouts/settings-layout"; // ui // types import { NextPageWithLayout } from "@/lib/types"; import { UserService } from "@/services/user.service"; 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); // hooks const { theme: themeStore } = useApplication(); const { currentUser } = useUser(); const router = useRouter(); // use form const { control, handleSubmit, formState: { errors, isSubmitting }, } = useForm({ defaultValues }); const handleChangePassword = async (formData: FormValues) => { if (formData.new_password !== formData.confirm_password) { setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: "The new password and the confirm password don't match.", }); return; } const changePasswordPromise = userService.changePassword(formData); setPromiseToast(changePasswordPromise, { loading: "Changing password...", success: { title: "Success!", message: () => "Password changed successfully.", }, error: { title: "Error!", message: () => "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 ( <>
themeStore.toggleSidebar()} />

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;