import { useState, useEffect } from 'react' import { useTranslation } from 'react-i18next' import { Button, Row, Col } from 'react-bootstrap' import Cell from './cell' import useAsync from '../../../../shared/hooks/use-async' import { useUserEmailsContext } from '../../context/user-email-context' import { postJSON } from '../../../../infrastructure/fetch-json' import Icon from '../../../../shared/components/icon' import { AddEmailInput } from './add-email-input' const isValidEmail = (email: string) => { return Boolean(email) } function AddEmail() { const { t } = useTranslation() const [isFormVisible, setIsFormVisible] = useState( () => window.location.hash === '#add-email' ) const [newEmail, setNewEmail] = useState('') const [isInstitutionFieldsVisible, setIsInstitutionFieldsVisible] = useState(false) const { isLoading, isError, runAsync } = useAsync() const { state, setLoading: setUserEmailsContextLoading, getEmails, } = useUserEmailsContext() useEffect(() => { setUserEmailsContextLoading(isLoading) }, [setUserEmailsContextLoading, isLoading]) const handleShowAddEmailForm = () => { setIsFormVisible(true) } const handleShowInstitutionFields = () => { setIsInstitutionFieldsVisible(true) } const handleEmailChange = (value: string) => { setNewEmail(value) } const handleAddNewEmail = () => { runAsync( postJSON('/user/emails', { body: { email: newEmail, }, }) ) .then(() => { getEmails() setIsFormVisible(false) setNewEmail('') }) .catch(error => { console.error(error) }) } return (