import React, { useEffect, useState } from 'react' import { Grid, DialogContent, DialogActions, Button, OutlinedInput, InputAdornment, IconButton, FormControl, InputLabel, FormHelperText } from '@mui/material' import Visibility from '@mui/icons-material/Visibility' import VisibilityOff from '@mui/icons-material/VisibilityOff' import { BootstrapDialogTitle } from './dialogTitle' import { BootstrapDialog } from './modal' type Props = { open: boolean setOpen: React.Dispatch> title: string updatePassword: (currentPassword: string, newPassword: string) => void } const UpdatePasswordModal = (props: Props) => { const { open, setOpen, title, updatePassword } = props const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [hasError, setHasError] = useState(false) const [errorText, setErrorText] = useState('') useEffect(() => { if ( currentPassword.length > 0 && newPassword.length > 0 && newPassword === currentPassword ) { setErrorText('New password should be different to current password.') setHasError(true) } else if (newPassword.length >= 6) { setErrorText('') setHasError(false) } }, [currentPassword, newPassword]) const handleBlur = () => { if (newPassword.length < 6) { setErrorText('Password length should be at least 6 characters.') setHasError(true) } } return (
setOpen(false)} open={open}> {title}
) } export default UpdatePasswordModal type PasswordInputProps = { label: string password: string setPassword: React.Dispatch> hasError?: boolean errorText?: string handleBlur?: () => void } export const PasswordInput = ({ label, password, setPassword, hasError, errorText, handleBlur }: PasswordInputProps) => { const [showPassword, setShowPassword] = useState(false) return ( {label} setPassword(e.target.value)} onBlur={handleBlur} endAdornment={ setShowPassword((val) => !val)} edge="end" > {showPassword ? : } } /> {errorText && {errorText}} ) }