import { useState, useEffect, useMemo } from 'react'; import { Box, Typography, Button, CircularProgress, useTheme, useMediaQuery, } from '@mui/material'; import { useTranslation } from 'react-i18next'; import { CardContainer } from '@/components/CardContainer'; import { PageWrapper } from '../PageWrapper'; import { PasswordDialog } from './PasswordDialog'; import { regex } from '@/utils/regex'; import { useManualApi } from '@/hooks/useApi'; import { addPassword, changePassword, fetchProfile, } from '../../api/settingsApi'; export function PasswordSecurity() { const theme = useTheme(); const fullScreen = useMediaQuery(theme.breakpoints.down('sm')); const { t } = useTranslation('setting'); const [open, setOpen] = useState(false); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [showValidation, setShowValidation] = useState(false); const [showPasswordAlert, setShowPasswordAlert] = useState(false); const [changePasswordUI, setChangePasswordUI] = useState(false); const { validPassword } = regex(password); const matchPassword = password === confirmPassword; const { data: profileData, loading: isFetching, error: fetchError, execute: executeFetchProfile, } = useManualApi(fetchProfile); const { data: addData, loading: isAdding, error: addError, execute: executeAddPassword, } = useManualApi(addPassword); const { data: changeData, loading: isChanging, error: changeError, execute: executeChangePassword, } = useManualApi(changePassword); useEffect(() => { executeFetchProfile(); }, []); useEffect(() => { if (profileData?.success) { setChangePasswordUI(profileData.hasPassword || false); } }, [profileData]); useEffect(() => { if (password) { setShowValidation(!validPassword); } else { setShowValidation(false); } }, [password, validPassword]); useEffect(() => { if (addData?.success || changeData?.success) { setShowPasswordAlert(true); if (!changePasswordUI) { setChangePasswordUI(true); } setOpen(false); setPassword(''); setConfirmPassword(''); setCurrentPassword(''); } }, [addData, changeData, changePasswordUI]); const handleOpen = () => setOpen(true); const handleClose = () => setOpen(false); const handlePasswordSubmit = () => { if (changePasswordUI) { executeChangePassword({ oldPassword: currentPassword, newPassword: password, }); } else { executeAddPassword({ password }); } }; const getErrorMessage = (error: unknown): string | null => { if (!error) return null; if (error instanceof Error) return error.message; return String(error); }; const apiError = useMemo( () => addError || changeError, [addError, changeError], ); return ( {changePasswordUI ? t('securityForm.changePassword') : t('securityForm.addPassword')} } > {isFetching ? ( ) : fetchError ? ( {getErrorMessage(fetchError)} ) : ( {changePasswordUI ? ( {t('securityForm.activePassword')} {t('securityForm.lastChange')} ) : ( {t('securityForm.notDeterminedPassword')} )} )} ); }