import { ReactElement, useEffect, useState } from "react"; import { observer } from "mobx-react"; import { useRouter } from "next/router"; import { Controller, useForm } from "react-hook-form"; // ui import { Button, Input, Spinner, TOAST_TYPE, setPromiseToast, setToast } from "@plane/ui"; // components import { PageHead } from "@/components/core"; import { SidebarHamburgerToggle } from "@/components/core/sidebar"; // hooks import { useAppTheme, useUser } from "@/hooks/store"; // layout import { ProfileSettingsLayout } from "@/layouts/settings-layout"; // types import { NextPageWithLayout } from "@/lib/types"; // services import { UserService } from "@/services/user.service"; export interface FormValues { old_password: string; new_password: string; confirm_password: string; } const defaultValues: FormValues = { old_password: "", new_password: "", confirm_password: "", }; export const userService = new UserService(); const ChangePasswordPage: NextPageWithLayout = observer(() => { const [isPageLoading, setIsPageLoading] = useState(true); // router const router = useRouter(); // store hooks const { toggleSidebar } = useAppTheme(); const { data: currentUser } = useUser(); // 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 ( <>
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;