import { useEffect, useState } from 'preact/hooks'; import { httpGet, httpPost } from '../../lib/http'; import { pageProgressMessage } from '../../stores/page'; export default function UpdatePasswordForm() { const [authProvider, setAuthProvider] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [newPasswordConfirmation, setNewPasswordConfirmation] = useState(''); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const [isLoading, setIsLoading] = useState(true); const handleSubmit = async (e: Event) => { e.preventDefault(); setIsLoading(true); setError(''); setSuccess(''); if (newPassword !== newPasswordConfirmation) { setError('Passwords do not match'); setIsLoading(false); return; } const { response, error } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-update-password`, { oldPassword: authProvider === 'email' ? currentPassword : 'social-auth', password: newPassword, confirmPassword: newPasswordConfirmation, } ); if (error) { setError(error.message || 'Something went wrong'); setIsLoading(false); return; } setError(''); setCurrentPassword(''); setNewPassword(''); setNewPasswordConfirmation(''); setSuccess('Password updated successfully'); setIsLoading(false); }; const loadProfile = async () => { setIsLoading(true); const { error, response } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-me` ); if (error || !response) { setIsLoading(false); setError(error?.message || 'Something went wrong'); return; } const { authProvider } = response; setAuthProvider(authProvider); setIsLoading(false); }; useEffect(() => { loadProfile().finally(() => { pageProgressMessage.set(''); }); }, []); return (
{authProvider === 'email' && (
setCurrentPassword((e.target as HTMLInputElement).value) } />
)}
setNewPassword((e.target as HTMLInputElement).value) } />
setNewPasswordConfirmation((e.target as HTMLInputElement).value) } />
{error && (

{error}

)} {success && (

{success}

)}
); }