2022-04-29 07:10:10 -04:00
|
|
|
import { useState, useEffect, useRef } 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 useAsync from '../../../../shared/hooks/use-async'
|
2022-04-29 07:09:53 -04:00
|
|
|
import { getJSON, postJSON } from '../../../../infrastructure/fetch-json'
|
2022-05-11 05:19:10 -04:00
|
|
|
import defaultRoles from '../../data/roles'
|
|
|
|
import defaultDepartments from '../../data/departments'
|
2022-04-29 07:09:53 -04:00
|
|
|
import { University } from '../../../../../../types/university'
|
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-05-18 09:46:18 -04:00
|
|
|
const changeAffiliationAsync = useAsync<University>()
|
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-05-11 05:19:10 -04:00
|
|
|
const [departments, setDepartments] = useState<string[]>(() => [
|
|
|
|
...defaultDepartments,
|
|
|
|
])
|
2022-04-29 07:10:10 -04:00
|
|
|
const roleRef = useRef<HTMLInputElement | null>(null)
|
|
|
|
const isChangingAffiliationInProgress = isChangingAffiliation(
|
|
|
|
state,
|
|
|
|
userEmailData.email
|
|
|
|
)
|
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
|
|
|
|
2022-04-29 07:10:10 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (isChangingAffiliationInProgress && roleRef.current) {
|
|
|
|
roleRef.current?.focus()
|
|
|
|
}
|
|
|
|
}, [roleRef, isChangingAffiliationInProgress])
|
|
|
|
|
2022-04-14 05:19:18 -04:00
|
|
|
const handleChangeAffiliation = () => {
|
2022-04-22 09:49:26 -04:00
|
|
|
setEmailAffiliationBeingEdited(userEmailData.email)
|
2022-04-29 07:09:53 -04:00
|
|
|
|
|
|
|
if (!affiliation?.institution.id) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
changeAffiliationAsync
|
2022-05-18 09:46:18 -04:00
|
|
|
.runAsync(getJSON(`/institutions/list/${affiliation.institution.id}`))
|
2022-04-29 07:09:53 -04:00
|
|
|
.then(data => {
|
|
|
|
if (data.departments.length) {
|
|
|
|
setDepartments(data.departments)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2022-05-11 05:19:10 -04:00
|
|
|
setDepartments([...defaultDepartments])
|
2022-04-29 07:09:53 -04:00
|
|
|
})
|
2022-04-22 09:49:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-29 07:10:10 -04:00
|
|
|
{!isChangingAffiliationInProgress ? (
|
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}>
|
2022-05-16 04:03:04 -04:00
|
|
|
<div className="form-group mb-2">
|
|
|
|
<DownshiftInput
|
|
|
|
items={[...defaultRoles]}
|
|
|
|
inputValue={role}
|
|
|
|
placeholder={t('role')}
|
|
|
|
label={t('role')}
|
|
|
|
setValue={setRole}
|
|
|
|
ref={roleRef}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="form-group mb-2">
|
|
|
|
<DownshiftInput
|
|
|
|
items={departments}
|
|
|
|
inputValue={department}
|
|
|
|
placeholder={t('department')}
|
|
|
|
label={t('department')}
|
|
|
|
setValue={setDepartment}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-04-22 09:49:26 -04:00
|
|
|
<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 && (
|
|
|
|
<>
|
2022-04-29 07:10:10 -04:00
|
|
|
<span className="mx-1">{t('save_or_cancel-or')}</span>
|
2022-04-22 09:49:26 -04:00
|
|
|
<Button
|
|
|
|
className="btn-inline-link"
|
|
|
|
onClick={handleCancelAffiliationChange}
|
|
|
|
>
|
|
|
|
{t('save_or_cancel-cancel')}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isError && (
|
2022-04-29 07:10:10 -04:00
|
|
|
<div className="text-danger small">
|
|
|
|
{t('generic_something_went_wrong')}
|
|
|
|
</div>
|
2022-04-14 05:19:18 -04:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default InstitutionAndRole
|