import type { FunctionComponent } from "react"; import { useState } from "react"; import { useRouter } from "blitz"; import { useForm } from "react-hook-form"; import Alert from "./alert"; import Button from "./button"; import SettingsSection from "./settings-section"; import appLogger from "../../../integrations/logger"; const logger = appLogger.child({ module: "update-password" }); type Form = { newPassword: string; newPasswordConfirmation: string; }; const UpdatePassword: FunctionComponent = () => { const router = useRouter(); const { register, handleSubmit, formState: { isSubmitting, isSubmitSuccessful }, } = useForm
(); const [errorMessage, setErrorMessage] = useState(""); const onSubmit = handleSubmit(async ({ newPassword, newPasswordConfirmation }) => { if (isSubmitting) { return; } if (newPassword !== newPasswordConfirmation) { setErrorMessage("New passwords don't match"); return; } try { // TODO // await customer.updateUser({ password: newPassword }); } catch (error: any) { logger.error(error.response, "error updating user infos"); if (error.response.status === 401) { logger.error("session expired, redirecting to sign in page"); return router.push("/auth/sign-in"); } setErrorMessage(error.response.data.errorMessage); } }); return ( {errorMessage ? (
) : null} {isSubmitSuccessful ? (
) : null}
); }; export default UpdatePassword;