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'
|
|
|
|
import Input, { InstitutionInfo } from './add-email/input'
|
|
|
|
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-11 05:19:10 -04:00
|
|
|
import { isSsoAvailable } from '../../utils/sso'
|
|
|
|
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-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-04-27 12:13:17 -04:00
|
|
|
const [newEmailMatchedInstitution, setNewEmailMatchedInstitution] =
|
|
|
|
useState<InstitutionInfo | 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-04-27 12:13:17 -04:00
|
|
|
const handleEmailChange = (value: string, institution?: InstitutionInfo) => {
|
2022-04-26 07:26:54 -04:00
|
|
|
setNewEmail(value)
|
2022-04-27 12:13:17 -04:00
|
|
|
setNewEmailMatchedInstitution(institution || null)
|
2022-04-25 07:05:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleAddNewEmail = () => {
|
2022-04-27 04:42:48 -04:00
|
|
|
const selectedKnownUniversity = countryCode
|
2022-05-11 05:19:10 -04:00
|
|
|
? universities[countryCode]?.find(({ name }) => name === universityName)
|
2022-04-27 04:42:48 -04:00
|
|
|
: undefined
|
|
|
|
|
2022-05-11 05:19:10 -04:00
|
|
|
const knownUniversityData = universityName &&
|
2022-04-27 04:42:48 -04:00
|
|
|
selectedKnownUniversity && {
|
|
|
|
university: {
|
|
|
|
id: selectedKnownUniversity.id,
|
|
|
|
},
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
}
|
|
|
|
|
2022-05-11 05:19:10 -04:00
|
|
|
const unknownUniversityData = universityName &&
|
2022-04-27 04:42:48 -04:00
|
|
|
!selectedKnownUniversity && {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2022-05-11 05:19:10 -04:00
|
|
|
<Layout isError={isError} error={error}>
|
|
|
|
<form>
|
|
|
|
<Col md={4}>
|
|
|
|
<Cell>
|
|
|
|
<label htmlFor="affiliations-email" className="sr-only">
|
|
|
|
{t('email')}
|
|
|
|
</label>
|
|
|
|
<Input onChange={handleEmailChange} />
|
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
{isSsoAvailable(newEmailMatchedInstitution) ? (
|
|
|
|
<Col md={8}>
|
2022-04-25 07:05:07 -04:00
|
|
|
<Cell>
|
2022-05-11 05:19:10 -04:00
|
|
|
<SsoLinkingInfo
|
|
|
|
email={newEmail}
|
|
|
|
institutionInfo={newEmailMatchedInstitution}
|
|
|
|
/>
|
2022-04-25 07:05:07 -04:00
|
|
|
</Cell>
|
|
|
|
</Col>
|
|
|
|
) : (
|
2022-05-11 05:19:10 -04:00
|
|
|
<>
|
|
|
|
<Col md={5}>
|
2022-04-25 07:05:07 -04:00
|
|
|
<Cell>
|
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-04-25 07:05:07 -04:00
|
|
|
</Cell>
|
|
|
|
</Col>
|
2022-05-11 05:19:10 -04:00
|
|
|
<Col md={3}>
|
|
|
|
<Cell className="text-md-right">
|
|
|
|
<AddNewEmailBtn
|
|
|
|
email={newEmail}
|
|
|
|
disabled={isLoading || state.isLoading}
|
|
|
|
onClick={handleAddNewEmail}
|
|
|
|
/>
|
|
|
|
</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
|