import type { FunctionComponent } from "react"; import { useState } from "react"; import { useMutation } from "blitz"; import { useForm } from "react-hook-form"; import Alert from "../../core/components/alert"; import Button from "./button"; import SettingsSection from "./settings-section"; import appLogger from "../../../integrations/logger"; import changePassword from "../mutations/change-password"; const logger = appLogger.child({ module: "update-password" }); type Form = { currentPassword: string; newPassword: string; }; const UpdatePassword: FunctionComponent = () => { const changePasswordMutation = useMutation(changePassword)[0]; const { register, handleSubmit, formState: { isSubmitting, isSubmitSuccessful }, } = useForm
(); const [errorMessage, setErrorMessage] = useState(""); const onSubmit = handleSubmit(async ({ currentPassword, newPassword }) => { if (isSubmitting) { return; } setErrorMessage(""); try { await changePasswordMutation({ currentPassword, newPassword }); } catch (error: any) { logger.error(error, "error updating user infos"); setErrorMessage(error.message); } }); return ( {errorMessage ? (
) : null} {!isSubmitting && isSubmitSuccessful && !errorMessage ? (
) : null}
); }; export default UpdatePassword;