import { useState, useCallback } from 'react' import { z } from 'zod' import { Button, Form, Input, YStack, Text } from '@junwon/aesthetics' import { useRouterContext } from '../providers/RouterProvider' import { useVisa } from '../providers/VisaProvider' const updateAccountSchema = z.object({ email: z.string().email('Please enter a valid email address'), }) export function UpdateAccount() { const { setRoute } = useRouterContext() const [newEmail, setNewEmail] = useState('') const [error, setError] = useState('') const [isSending, setIsSending] = useState(false) const { profile, updateAccount, loading } = useVisa() const handleChangeText = useCallback((text: string) => { const noWhitespace = text.replace(/\s/g, '') setNewEmail(noWhitespace) setError('') }, []) const handleUpdateAccount = async () => { try { const validated = updateAccountSchema.parse({ email: newEmail }) setError('') setIsSending(true) const response = await updateAccount({ email: validated.email }, '/account/update/verify') if (response.success) { setRoute('/account/update/verify') } else { setError('Failed to send a verification email. Please try again.') } } catch (err) { if (err instanceof z.ZodError) { setError(err.errors[0].message) } else { console.log('Update account error:', err) setError('Failed to send a verification email. Please try again.') } } setIsSending(false) } return (
Current Email New Email {error}
) }