2022-04-22 09:49:26 -04:00
|
|
|
import { useState, useEffect } from 'react'
|
2022-04-14 05:19:18 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { UserEmailData } from '../../../../../../types/user-email'
|
|
|
|
import { Button } from 'react-bootstrap'
|
2022-04-22 09:49:26 -04:00
|
|
|
import { isChangingAffiliation } from '../../utils/selectors'
|
|
|
|
import { useUserEmailsContext } from '../../context/user-email-context'
|
|
|
|
import DownshiftInput from './downshift-input'
|
|
|
|
import Icon from '../../../../shared/components/icon'
|
|
|
|
import useAsync from '../../../../shared/hooks/use-async'
|
|
|
|
import { postJSON } from '../../../../infrastructure/fetch-json'
|
|
|
|
import { defaults as roles } from '../../roles'
|
|
|
|
import { defaults as departments } from '../../departments'
|
2022-04-14 05:19:18 -04:00
|
|
|
|
|
|
|
type InstitutionAndRoleProps = {
|
|
|
|
userEmailData: UserEmailData
|
|
|
|
}
|
|
|
|
|
|
|
|
function InstitutionAndRole({ userEmailData }: InstitutionAndRoleProps) {
|
|
|
|
const { t } = useTranslation()
|
2022-04-22 09:49:26 -04:00
|
|
|
const { isLoading, isError, runAsync } = useAsync()
|
2022-04-14 05:19:18 -04:00
|
|
|
const { affiliation } = userEmailData
|
2022-04-22 09:49:26 -04:00
|
|
|
const {
|
|
|
|
state,
|
|
|
|
setLoading: setUserEmailsContextLoading,
|
|
|
|
setEmailAffiliationBeingEdited,
|
|
|
|
updateAffiliation,
|
|
|
|
} = useUserEmailsContext()
|
|
|
|
const [role, setRole] = useState(affiliation?.role || '')
|
|
|
|
const [department, setDepartment] = useState(affiliation?.department || '')
|
2022-04-14 05:19:18 -04:00
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setUserEmailsContextLoading(isLoading)
|
|
|
|
}, [setUserEmailsContextLoading, isLoading])
|
2022-04-14 05:19:18 -04:00
|
|
|
|
|
|
|
const handleChangeAffiliation = () => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setEmailAffiliationBeingEdited(userEmailData.email)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleCancelAffiliationChange = () => {
|
|
|
|
setEmailAffiliationBeingEdited(null)
|
|
|
|
setRole(affiliation?.role || '')
|
|
|
|
setDepartment(affiliation?.department || '')
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
runAsync(
|
|
|
|
postJSON('/user/emails/endorse', {
|
|
|
|
body: {
|
|
|
|
email: userEmailData.email,
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
updateAffiliation(userEmailData.email, role, department)
|
|
|
|
})
|
|
|
|
.catch(() => {})
|
2022-04-14 05:19:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!affiliation?.institution) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div>{affiliation.institution.name}</div>
|
2022-04-22 09:49:26 -04:00
|
|
|
{!isChangingAffiliation(state, userEmailData.email) ? (
|
2022-04-14 05:19:18 -04:00
|
|
|
<div className="small">
|
2022-04-22 09:49:26 -04:00
|
|
|
{(affiliation.role || affiliation.department) && (
|
|
|
|
<>
|
|
|
|
{[affiliation.role, affiliation.department]
|
|
|
|
.filter(Boolean)
|
|
|
|
.join(', ')}
|
|
|
|
<br />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-14 05:19:18 -04:00
|
|
|
<Button className="btn-inline-link" onClick={handleChangeAffiliation}>
|
2022-04-22 09:49:26 -04:00
|
|
|
{!affiliation.department && !affiliation.role
|
|
|
|
? t('add_role_and_department')
|
|
|
|
: t('change')}
|
2022-04-14 05:19:18 -04:00
|
|
|
</Button>
|
|
|
|
</div>
|
2022-04-22 09:49:26 -04:00
|
|
|
) : (
|
|
|
|
<div className="affiliation-change-container small">
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<DownshiftInput
|
|
|
|
items={roles}
|
|
|
|
inputValue={role}
|
|
|
|
placeholder={t('role')}
|
|
|
|
setValue={setRole}
|
|
|
|
/>
|
|
|
|
<DownshiftInput
|
|
|
|
items={departments}
|
|
|
|
inputValue={department}
|
|
|
|
placeholder={t('department')}
|
|
|
|
setValue={setDepartment}
|
|
|
|
/>
|
|
|
|
<Button
|
2022-04-25 07:05:07 -04:00
|
|
|
bsSize="small"
|
2022-04-22 09:49:26 -04:00
|
|
|
bsStyle="success"
|
|
|
|
type="submit"
|
|
|
|
disabled={!role || !department || isLoading || state.isLoading}
|
|
|
|
>
|
|
|
|
{isLoading ? <>{t('saving')}…</> : t('save_or_cancel-save')}
|
|
|
|
</Button>
|
|
|
|
{!isLoading && (
|
|
|
|
<>
|
|
|
|
<span className="mx-2">{t('save_or_cancel-or')}</span>
|
|
|
|
<Button
|
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={handleCancelAffiliationChange}
|
|
|
|
>
|
|
|
|
{t('save_or_cancel-cancel')}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isError && (
|
|
|
|
<span className="text-danger small">
|
|
|
|
<Icon type="exclamation-triangle" fw />{' '}
|
|
|
|
{t('error_performing_request')}
|
|
|
|
</span>
|
2022-04-14 05:19:18 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InstitutionAndRole
|