import { useState } from 'react' import { Dialog, DialogTitle, DialogContent, DialogActions, TextField, Button, Alert, Box, IconButton, InputAdornment, CircularProgress, } from '@mui/material' import { Visibility, VisibilityOff } from '@mui/icons-material' import { useAuth } from '../context/AuthContext' interface ChangePasswordDialogProps { open: boolean onClose: () => void } export default function ChangePasswordDialog({ open, onClose }: ChangePasswordDialogProps) { const { changePassword } = useAuth() const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [confirmPassword, setConfirmPassword] = useState('') const [showCurrentPassword, setShowCurrentPassword] = useState(false) const [showNewPassword, setShowNewPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) const [isLoading, setIsLoading] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(false) const handleClose = () => { // Reset form state setCurrentPassword('') setNewPassword('') setConfirmPassword('') setShowCurrentPassword(false) setShowNewPassword(false) setShowConfirmPassword(false) setError(null) setSuccess(false) onClose() } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setError(null) // Validate passwords if (!currentPassword || !newPassword || !confirmPassword) { setError('Vul alle velden in') return } if (newPassword.length < 8) { setError('Nieuw wachtwoord moet minimaal 8 tekens bevatten') return } if (newPassword !== confirmPassword) { setError('Nieuwe wachtwoorden komen niet overeen') return } if (currentPassword === newPassword) { setError('Nieuw wachtwoord moet anders zijn dan het huidige wachtwoord') return } setIsLoading(true) try { const result = await changePassword(currentPassword, newPassword) if (result.success) { setSuccess(true) // Close dialog after showing success message setTimeout(() => { handleClose() }, 1500) } else { setError(result.error || 'Er is een fout opgetreden') } } catch { setError('Er is een fout opgetreden bij het wijzigen van het wachtwoord') } finally { setIsLoading(false) } } return ( Wachtwoord wijzigen
{error && ( {error} )} {success && ( Wachtwoord succesvol gewijzigd )} setCurrentPassword(e.target.value)} disabled={isLoading || success} fullWidth autoFocus InputProps={{ endAdornment: ( setShowCurrentPassword(!showCurrentPassword)} edge="end" tabIndex={-1} > {showCurrentPassword ? : } ), }} /> setNewPassword(e.target.value)} disabled={isLoading || success} fullWidth helperText="Minimaal 8 tekens" InputProps={{ endAdornment: ( setShowNewPassword(!showNewPassword)} edge="end" tabIndex={-1} > {showNewPassword ? : } ), }} /> setConfirmPassword(e.target.value)} disabled={isLoading || success} fullWidth InputProps={{ endAdornment: ( setShowConfirmPassword(!showConfirmPassword)} edge="end" tabIndex={-1} > {showConfirmPassword ? : } ), }} />
) }