import { Modal, ModalDialog, DialogTitle, Stack, Input, Button, Alert, } from "@mui/joy"; import { useForm } from "@tanstack/react-form"; import { useMutation } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import type { ChangePasswordIntf, AlertInterface } from "../../misc/interfaces"; import { mutatePassword } from "../../utils/api/auth"; import { useState } from "react"; interface ChangePasswordProps { isOpen: boolean; setOpen: (value: boolean) => void; } export const ChangePasswordModal = (props: ChangePasswordProps) => { const { t } = useTranslation(); const [alert, setAlert] = useState({ isAlert: false, type: "neutral", header: "", text: "", }); const form = useForm({ defaultValues: { currentPassword: "", newPassword: "", newPasswordRep: "", }, onSubmit: async ({ value }) => { mutate(value); }, }); const { mutate } = useMutation({ mutationFn: (values: ChangePasswordIntf) => mutatePassword(values), onError: (error: { code?: string }) => { setAlert({ isAlert: true, type: "danger", header: t("error"), text: error.code ? t(error.code) : t("unknown-error"), }); }, onSuccess: () => { props.setOpen(false); }, }); return ( <> props.setOpen(false)}> {t("new-password-title")}
{ e.preventDefault(); form.handleSubmit(); }} > {(field) => ( field.handleChange(e.target.value)} placeholder={t("current-password")} variant="outlined" size="lg" className="rounded-2xl bg-white/90 shadow-[0_10px_24px_rgba(15,23,42,0.08)]" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("new-password")} variant="outlined" size="lg" className="rounded-2xl bg-white/90 shadow-[0_10px_24px_rgba(15,23,42,0.08)]" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("new-password-rep")} variant="outlined" size="lg" className="rounded-2xl bg-white/90 shadow-[0_10px_24px_rgba(15,23,42,0.08)]" /> )}
{alert.isAlert && ( {alert.header}
{alert.text}
)}
); };