import React from "react"; import { LoadingButton } from "@mui/lab"; import { FormControl, FormGroup, Stack, TextField, Typography } from "@mui/material"; import { useSnackbar } from "notistack"; import { useI18n } from "../../../contexts/I18nContext"; import { useUser } from "../../../contexts/UserContext"; export interface PasswordChangeFields { "Old password": string; Password: string; "Password (again)": string; } const PasswordChangeForm: React.FC = () => { const { t } = useI18n(); const { enqueueSnackbar } = useSnackbar(); const [loading, setLoading] = React.useState(false); const [fields, setFields] = React.useState({ "Old password": "", Password: "", "Password (again)": "", }); const { changePassword } = useUser(); const onSubmit = async (event: React.FormEvent) => { event.preventDefault(); setLoading(true); try { await changePassword(fields["Old password"], fields["Password"], fields["Password (again)"]); enqueueSnackbar(t("Password changed successfully."), { variant: "success", }); setFields({ "Old password": "", Password: "", "Password (again)": "", }); } catch { enqueueSnackbar(t("Incorrect authentication credentials."), { variant: "error", }); } finally { setLoading(false); } }; const onChange = (event: React.ChangeEvent<{ name: string; value: string }>) => { setFields({ ...fields, [event.target.name]: event.target.value, }); }; return ( {t( "Please enter your old password, for security’s sake, and then enter your new password twice so we can verify you typed it in correctly.", )} {(["Old password", "Password", "Password (again)"] as const).map((field) => ( ))} {t("Change my password")} ); }; export default PasswordChangeForm;