2022-05-11 05:19:10 -04:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-04-25 07:05:07 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
2022-05-11 05:19:10 -04:00
|
|
|
import { Col } from 'react-bootstrap'
|
2022-04-25 07:05:07 -04:00
|
|
|
import Cell from './cell'
|
2022-05-11 05:19:10 -04:00
|
|
|
import Layout from './add-email/layout'
|
2022-05-26 04:32:43 -04:00
|
|
|
import Input, { DomainInfo } from './add-email/input'
|
2022-05-11 05:19:10 -04:00
|
|
|
import AddAnotherEmailBtn from './add-email/add-another-email-btn'
|
|
|
|
import InstitutionFields from './add-email/institution-fields'
|
|
|
|
import SsoLinkingInfo from './add-email/sso-linking-info'
|
|
|
|
import AddNewEmailBtn from './add-email/add-new-email-btn'
|
2022-04-27 04:42:48 -04:00
|
|
|
import useAsync from '../../../../shared/hooks/use-async'
|
|
|
|
import { useUserEmailsContext } from '../../context/user-email-context'
|
2022-05-24 07:12:39 -04:00
|
|
|
import { ssoAvailableForDomain } from '../../utils/sso'
|
2022-05-11 05:19:10 -04:00
|
|
|
import { postJSON } from '../../../../infrastructure/fetch-json'
|
2022-04-27 04:42:48 -04:00
|
|
|
import { University } from '../../../../../../types/university'
|
2022-05-11 05:19:10 -04:00
|
|
|
import { CountryCode } from '../../data/countries-list'
|
2022-05-30 09:47:58 -04:00
|
|
|
import { isValidEmail } from '../../../../shared/utils/email'
|
2022-04-27 12:13:17 -04:00
|
|
|
|
2022-04-25 07:05:07 -04:00
|
|
|
function AddEmail() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const [isFormVisible, setIsFormVisible] = useState(
|
|
|
|
() => window.location.hash === '#add-email'
|
|
|
|
)
|
|
|
|
const [newEmail, setNewEmail] = useState('')
|
2022-05-26 04:32:43 -04:00
|
|
|
const [newEmailMatchedDomain, setNewEmailMatchedDomain] =
|
|
|
|
useState<DomainInfo | null>(null)
|
2022-04-27 04:42:48 -04:00
|
|
|
const [countryCode, setCountryCode] = useState<CountryCode | null>(null)
|
|
|
|
const [universities, setUniversities] = useState<
|
|
|
|
Partial<Record<CountryCode, University[]>>
|
|
|
|
>({})
|
2022-05-11 05:19:10 -04:00
|
|
|
const [universityName, setUniversityName] = useState('')
|
2022-04-27 04:42:48 -04:00
|
|
|
const [role, setRole] = useState('')
|
|
|
|
const [department, setDepartment] = useState('')
|
2022-04-29 07:10:10 -04:00
|
|
|
const { isLoading, isError, error, runAsync } = useAsync()
|
2022-04-25 07:05:07 -04:00
|
|
|
const {
|
|
|
|
state,
|
|
|
|
setLoading: setUserEmailsContextLoading,
|
|
|
|
getEmails,
|
|
|
|
} = useUserEmailsContext()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setUserEmailsContextLoading(isLoading)
|
|
|
|
}, [setUserEmailsContextLoading, isLoading])
|
|
|
|
|
|
|
|
const handleShowAddEmailForm = () => {
|
|
|
|
setIsFormVisible(true)
|
|
|
|
}
|
|
|
|
|
2022-05-26 04:32:43 -04:00
|
|
|
const handleEmailChange = (value: string, domain?: DomainInfo) => {
|
2022-04-26 07:26:54 -04:00
|
|
|
setNewEmail(value)
|
2022-05-26 04:32:43 -04:00
|
|
|
setNewEmailMatchedDomain(domain || null)
|
2022-04-25 07:05:07 -04:00
|
|
|
}
|
|
|
|
|
2022-05-16 04:03:04 -04:00
|
|
|
const getSelectedKnownUniversityId = (): number | undefined => {
|
|
|
|
if (countryCode) {
|
|
|
|
return universities[countryCode]?.find(
|
|
|
|
({ name }) => name === universityName
|
|
|
|
)?.id
|
|
|
|
}
|
2022-04-27 04:42:48 -04:00
|
|
|
|
2022-05-26 04:32:43 -04:00
|
|
|
return newEmailMatchedDomain?.university.id
|
2022-05-16 04:03:04 -04:00
|
|
|
}
|
2022-04-27 04:42:48 -04:00
|
|
|
|
2022-05-16 04:03:04 -04:00
|
|
|
const handleAddNewEmail = () => {
|
|
|
|
const selectedKnownUniversityId = getSelectedKnownUniversityId()
|
|
|
|
const knownUniversityData = selectedKnownUniversityId && {
|
|
|
|
university: {
|
|
|
|
id: selectedKnownUniversityId,
|
|
|
|
},
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
}
|
2022-05-11 05:19:10 -04:00
|
|
|
const unknownUniversityData = universityName &&
|
2022-05-16 04:03:04 -04:00
|
|
|
!selectedKnownUniversityId && {
|
2022-04-27 04:42:48 -04:00
|
|
|
university: {
|
2022-05-11 05:19:10 -04:00
|
|
|
name: universityName,
|
2022-04-27 04:42:48 -04:00
|
|
|
country_code: countryCode,
|
|
|
|
},
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
}
|
|
|
|
|
2022-04-25 07:05:07 -04:00
|
|
|
runAsync(
|
|
|
|
postJSON('/user/emails', {
|
|
|
|
body: {
|
|
|
|
email: newEmail,
|
2022-04-27 04:42:48 -04:00
|
|
|
...knownUniversityData,
|
|
|
|
...unknownUniversityData,
|
2022-04-25 07:05:07 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
getEmails()
|
|
|
|
})
|
2022-04-27 04:42:48 -04:00
|
|
|
.catch(() => {})
|
|
|
|
}
|
|
|
|
|
2022-05-11 05:19:10 -04:00
|
|
|
if (!isFormVisible) {
|
|
|
|
return (
|
|
|
|
<Layout isError={isError} error={error}>
|
|
|
|
<Col md={4}>
|
|
|
|
<Cell>
|
|
|
|
<AddAnotherEmailBtn onClick={handleShowAddEmailForm} />
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
</Layout>
|
|
|
|
)
|
2022-04-25 07:05:07 -04:00
|
|
|
}
|
|
|
|
|
2022-07-20 10:00:55 -04:00
|
|
|
const InputComponent = (
|
|
|
|
<>
|
|
|
|
<label htmlFor="affiliations-email" className="sr-only">
|
|
|
|
{t('email')}
|
|
|
|
</label>
|
|
|
|
<Input
|
|
|
|
onChange={handleEmailChange}
|
|
|
|
handleAddNewEmail={handleAddNewEmail}
|
|
|
|
/>
|
|
|
|
</>
|
2022-05-30 09:47:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if (!isValidEmail(newEmail)) {
|
|
|
|
return (
|
|
|
|
<Layout isError={isError} error={error}>
|
|
|
|
<form>
|
2022-07-20 10:00:55 -04:00
|
|
|
<Col md={8}>
|
2022-05-30 09:47:58 -04:00
|
|
|
<Cell>
|
2022-07-20 10:00:55 -04:00
|
|
|
{InputComponent}
|
|
|
|
<div className="affiliations-table-cell-tabbed">
|
|
|
|
<div>{t('start_by_adding_your_email')}</div>
|
|
|
|
</div>
|
2022-05-30 09:47:58 -04:00
|
|
|
</Cell>
|
|
|
|
</Col>
|
2022-07-20 10:00:55 -04:00
|
|
|
<Col md={4}>
|
2022-05-30 09:47:58 -04:00
|
|
|
<Cell className="text-md-right">
|
|
|
|
<AddNewEmailBtn email={newEmail} disabled />
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
</form>
|
|
|
|
</Layout>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:00:55 -04:00
|
|
|
const isSsoAvailableForDomain =
|
|
|
|
newEmailMatchedDomain && ssoAvailableForDomain(newEmailMatchedDomain)
|
|
|
|
|
2022-04-25 07:05:07 -04:00
|
|
|
return (
|
2022-05-11 05:19:10 -04:00
|
|
|
<Layout isError={isError} error={error}>
|
|
|
|
<form>
|
2022-07-20 10:00:55 -04:00
|
|
|
<Col md={8}>
|
|
|
|
<Cell>
|
|
|
|
{InputComponent}
|
|
|
|
{!isSsoAvailableForDomain ? (
|
|
|
|
<div className="affiliations-table-cell-tabbed">
|
2022-05-11 05:19:10 -04:00
|
|
|
<InstitutionFields
|
|
|
|
countryCode={countryCode}
|
|
|
|
setCountryCode={setCountryCode}
|
|
|
|
universities={universities}
|
|
|
|
setUniversities={setUniversities}
|
|
|
|
universityName={universityName}
|
|
|
|
setUniversityName={setUniversityName}
|
|
|
|
role={role}
|
|
|
|
setRole={setRole}
|
|
|
|
department={department}
|
|
|
|
setDepartment={setDepartment}
|
2022-05-26 04:32:43 -04:00
|
|
|
newEmailMatchedDomain={newEmailMatchedDomain}
|
2022-05-11 05:19:10 -04:00
|
|
|
/>
|
2022-07-20 10:00:55 -04:00
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
{!isSsoAvailableForDomain ? (
|
|
|
|
<Col md={4}>
|
|
|
|
<Cell className="text-md-right">
|
|
|
|
<AddNewEmailBtn
|
|
|
|
email={newEmail}
|
|
|
|
disabled={isLoading || state.isLoading}
|
|
|
|
onClick={handleAddNewEmail}
|
|
|
|
/>
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
) : (
|
|
|
|
<Col md={12}>
|
|
|
|
<Cell>
|
|
|
|
<div className="affiliations-table-cell-tabbed">
|
|
|
|
<SsoLinkingInfo
|
2022-05-11 05:19:10 -04:00
|
|
|
email={newEmail}
|
2022-07-20 10:00:55 -04:00
|
|
|
domainInfo={newEmailMatchedDomain as DomainInfo}
|
2022-05-11 05:19:10 -04:00
|
|
|
/>
|
2022-07-20 10:00:55 -04:00
|
|
|
</div>
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
2022-04-25 07:05:07 -04:00
|
|
|
)}
|
2022-05-11 05:19:10 -04:00
|
|
|
</form>
|
|
|
|
</Layout>
|
2022-04-25 07:05:07 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default AddEmail
|