import { ReactElement, useEffect, useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { Controller, useForm } from "react-hook-form"; // hooks import { useApplication, useUser } from "hooks/store"; // services import { UserService } from "services/user.service"; // components import { PageHead } from "components/core"; // layout import { ProfileSettingsLayout } from "layouts/settings-layout"; // ui import { Button, Input, Spinner, TOAST_TYPE, setPromiseToast, setToast } from "@plane/ui"; // types import { NextPageWithLayout } from "lib/types"; import { SidebarHamburgerToggle } from "components/core/sidebar/sidebar-menu-hamburger-toggle"; 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;