import { useState, useEffect } from 'react' import { useTranslation } from 'react-i18next' import { Button, Row, Col } from 'react-bootstrap' import Cell from './cell' import Icon from '../../../../shared/components/icon' import DownshiftInput from './downshift-input' import CountryInput from './country-input' import { AddEmailInput } from './add-email-input' import useAsync from '../../../../shared/hooks/use-async' import { useUserEmailsContext } from '../../context/user-email-context' import { getJSON, postJSON } from '../../../../infrastructure/fetch-json' import { defaults as roles } from '../../roles' import { defaults as departments } from '../../departments' import { University } from '../../../../../../types/university' import { CountryCode } from '../../../../../../types/country' 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 [countryCode, setCountryCode] = useState(null) const [universities, setUniversities] = useState< Partial> >({}) const [university, setUniversity] = useState('') const [role, setRole] = useState('') const [department, setDepartment] = useState('') const [isInstitutionFieldsVisible, setIsInstitutionFieldsVisible] = useState(false) const [isUniversityDirty, setIsUniversityDirty] = useState(false) const { isLoading, isError, runAsync } = useAsync() const { runAsync: institutionRunAsync } = useAsync() const { state, setLoading: setUserEmailsContextLoading, getEmails, } = useUserEmailsContext() useEffect(() => { setUserEmailsContextLoading(isLoading) }, [setUserEmailsContextLoading, isLoading]) useEffect(() => { if (university) { setIsUniversityDirty(true) } }, [setIsUniversityDirty, university]) // Fetch country institution useEffect(() => { // Skip if country not selected or universities for // that country are already fetched if (!countryCode || universities[countryCode]) { return } institutionRunAsync( getJSON(`/institutions/list?country_code=${countryCode}`) ) .then(data => { setUniversities(state => ({ ...state, [countryCode]: data })) }) .catch(() => {}) }, [countryCode, universities, setUniversities, institutionRunAsync]) const handleShowAddEmailForm = () => { setIsFormVisible(true) } const handleShowInstitutionFields = () => { setIsInstitutionFieldsVisible(true) } const handleEmailChange = (value: string) => { setNewEmail(value) } const handleAddNewEmail = () => { const selectedKnownUniversity = countryCode ? universities[countryCode]?.find(({ name }) => name === university) : undefined const knownUniversityData = university && selectedKnownUniversity && { university: { id: selectedKnownUniversity.id, }, role, department, } const unknownUniversityData = university && !selectedKnownUniversity && { university: { name: university, country_code: countryCode, }, role, department, } runAsync( postJSON('/user/emails', { body: { email: newEmail, ...knownUniversityData, ...unknownUniversityData, }, }) ) .then(() => { getEmails() setIsFormVisible(false) setNewEmail('') setCountryCode(null) setIsUniversityDirty(false) setUniversity('') setRole('') setDepartment('') setIsInstitutionFieldsVisible(false) }) .catch(() => {}) } const getUniversityItems = () => { if (!countryCode) { return [] } return universities[countryCode]?.map(({ name }) => name) ?? [] } return (
{!isFormVisible ? ( ) : (
{isInstitutionFieldsVisible ? ( <>
{isUniversityDirty && ( <>
)} ) : (
{t('is_email_affiliated')}
)}
{isError && (
{' '} {t('error_performing_request')}
)}
)}
) } export default AddEmail