import { Button, DialogTitle, Input, Modal, ModalDialog, Stack, } from "@mui/joy"; import { useForm } from "@tanstack/react-form"; import { useMutation } from "@tanstack/react-query"; import { useTranslation } from "react-i18next"; import type { AlertInterface, ChangePasswordIntf } from "../../misc/interfaces"; import { mutatePassword } from "../../utils/api/auth"; import { useState } from "react"; import { USER_ERROR_CODE } from "@stockhome/shared"; import { MyAlert } from "../MyAlert.tsx"; interface ChangePasswordProps { isOpen: boolean; setOpen: (value: boolean) => void; alert: (alert: AlertInterface) => 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 }) => { if ( value.newPassword === value.newPasswordRep && value.newPassword !== "" ) { mutate(value); } else { setAlert({ isAlert: true, type: "danger", header: t("error"), text: t(USER_ERROR_CODE.PASSWORDS_NOT_MATCHED[0]), }); } }, }); 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.alert({ isAlert: true, type: "success", header: t("success"), text: t("SU005"), }); setAlert({ isAlert: true, type: "success", header: t("success"), text: t("SU005"), }); 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" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("new-password")} variant="outlined" size="lg" className="rounded-2xl" /> )} {(field) => ( field.handleChange(e.target.value)} placeholder={t("new-password-rep")} variant="outlined" size="lg" className="rounded-2xl" /> )}
{alert.isAlert && ( )}
); };