2022-04-22 09:49:26 -04:00
|
|
|
import {
|
|
|
|
createContext,
|
|
|
|
useEffect,
|
|
|
|
useContext,
|
|
|
|
useReducer,
|
|
|
|
useCallback,
|
|
|
|
} from 'react'
|
2022-04-08 07:00:46 -04:00
|
|
|
import useSafeDispatch from '../../../shared/hooks/use-safe-dispatch'
|
2022-04-22 09:49:26 -04:00
|
|
|
import * as ActionCreators from '../utils/action-creators'
|
2022-04-08 07:00:46 -04:00
|
|
|
import { UserEmailData } from '../../../../../types/user-email'
|
2022-04-22 09:49:26 -04:00
|
|
|
import { Nullable } from '../../../../../types/utils'
|
|
|
|
import { Affiliation } from '../../../../../types/affiliation'
|
2022-04-08 07:00:46 -04:00
|
|
|
import { normalize, NormalizedObject } from '../../../utils/normalize'
|
2022-04-22 09:49:26 -04:00
|
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
|
|
import useAsync from '../../../shared/hooks/use-async'
|
2022-05-16 04:03:42 -04:00
|
|
|
import usePersistedState from '../../../shared/hooks/use-persisted-state'
|
|
|
|
|
|
|
|
const ONE_WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-04-22 09:49:26 -04:00
|
|
|
export enum Actions {
|
|
|
|
SET_DATA = 'SET_DATA', // eslint-disable-line no-unused-vars
|
2022-04-08 07:00:46 -04:00
|
|
|
SET_LOADING_STATE = 'SET_LOADING_STATE', // eslint-disable-line no-unused-vars
|
2022-04-22 09:49:26 -04:00
|
|
|
MAKE_PRIMARY = 'MAKE_PRIMARY', // eslint-disable-line no-unused-vars
|
|
|
|
DELETE_EMAIL = 'DELETE_EMAIL', // eslint-disable-line no-unused-vars
|
|
|
|
SET_EMAIL_AFFILIATION_BEING_EDITED = 'SET_EMAIL_AFFILIATION_BEING_EDITED', // eslint-disable-line no-unused-vars
|
|
|
|
UPDATE_AFFILIATION = 'UPDATE_AFFILIATION', // eslint-disable-line no-unused-vars
|
2022-04-08 07:00:46 -04:00
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
export type ActionSetData = {
|
|
|
|
type: Actions.SET_DATA
|
|
|
|
payload: UserEmailData[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ActionSetLoading = {
|
2022-04-08 07:00:46 -04:00
|
|
|
type: Actions.SET_LOADING_STATE
|
|
|
|
payload: boolean
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
export type ActionMakePrimary = {
|
|
|
|
type: Actions.MAKE_PRIMARY
|
|
|
|
payload: UserEmailData['email']
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ActionDeleteEmail = {
|
|
|
|
type: Actions.DELETE_EMAIL
|
|
|
|
payload: UserEmailData['email']
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ActionSetEmailAffiliationBeingEdited = {
|
|
|
|
type: Actions.SET_EMAIL_AFFILIATION_BEING_EDITED
|
|
|
|
payload: Nullable<UserEmailData['email']>
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ActionUpdateAffiliation = {
|
|
|
|
type: Actions.UPDATE_AFFILIATION
|
|
|
|
payload: {
|
|
|
|
email: UserEmailData['email']
|
|
|
|
role: Affiliation['role']
|
|
|
|
department: Affiliation['department']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type State = {
|
2022-04-08 07:00:46 -04:00
|
|
|
isLoading: boolean
|
|
|
|
data: {
|
|
|
|
byId: NormalizedObject<UserEmailData>
|
2023-05-26 06:56:15 -04:00
|
|
|
emailCount: number
|
2022-04-22 09:49:26 -04:00
|
|
|
linkedInstitutionIds: NonNullable<UserEmailData['samlProviderId']>[]
|
|
|
|
emailAffiliationBeingEdited: Nullable<UserEmailData['email']>
|
2022-04-08 07:00:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
type Action =
|
|
|
|
| ActionSetData
|
|
|
|
| ActionSetLoading
|
|
|
|
| ActionMakePrimary
|
|
|
|
| ActionDeleteEmail
|
|
|
|
| ActionSetEmailAffiliationBeingEdited
|
|
|
|
| ActionUpdateAffiliation
|
|
|
|
|
|
|
|
const setData = (state: State, action: ActionSetData) => {
|
|
|
|
const normalized = normalize<UserEmailData>(action.payload, {
|
|
|
|
idAttribute: 'email',
|
|
|
|
})
|
2023-05-26 06:56:15 -04:00
|
|
|
const emailCount = action.payload.length
|
2022-04-22 09:49:26 -04:00
|
|
|
const byId = normalized || {}
|
2022-05-19 08:02:17 -04:00
|
|
|
const linkedInstitutionIds = action.payload
|
|
|
|
.filter(email => Boolean(email.samlProviderId))
|
|
|
|
.map(email => email.samlProviderId) as NonNullable<
|
|
|
|
UserEmailData['samlProviderId']
|
|
|
|
>[]
|
2022-04-22 09:49:26 -04:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: {
|
|
|
|
...initialState.data,
|
|
|
|
byId,
|
2023-05-26 06:56:15 -04:00
|
|
|
emailCount,
|
2022-05-19 08:02:17 -04:00
|
|
|
linkedInstitutionIds,
|
2022-04-22 09:49:26 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 07:00:46 -04:00
|
|
|
|
|
|
|
const setLoadingAction = (state: State, action: ActionSetLoading) => ({
|
|
|
|
...state,
|
|
|
|
isLoading: action.payload,
|
|
|
|
})
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
const makePrimaryAction = (state: State, action: ActionMakePrimary) => {
|
2022-05-24 03:48:06 -04:00
|
|
|
if (!state.data.byId[action.payload]) {
|
|
|
|
return state
|
|
|
|
}
|
2022-04-22 09:49:26 -04:00
|
|
|
const byId: State['data']['byId'] = {}
|
|
|
|
for (const id of Object.keys(state.data.byId)) {
|
|
|
|
byId[id] = {
|
|
|
|
...state.data.byId[id],
|
|
|
|
default: state.data.byId[id].email === action.payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: {
|
|
|
|
...state.data,
|
|
|
|
byId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteEmailAction = (state: State, action: ActionDeleteEmail) => {
|
|
|
|
const { [action.payload]: _, ...byId } = state.data.byId
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: {
|
|
|
|
...state.data,
|
2023-05-26 06:56:15 -04:00
|
|
|
emailCount: state.data.emailCount - 1,
|
2022-04-22 09:49:26 -04:00
|
|
|
byId,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const setEmailAffiliationBeingEditedAction = (
|
|
|
|
state: State,
|
|
|
|
action: ActionSetEmailAffiliationBeingEdited
|
2022-05-24 03:48:06 -04:00
|
|
|
) => {
|
|
|
|
if (action.payload && !state.data.byId[action.payload]) {
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: {
|
|
|
|
...state.data,
|
|
|
|
emailAffiliationBeingEdited: action.payload,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-04-22 09:49:26 -04:00
|
|
|
|
|
|
|
const updateAffiliationAction = (
|
|
|
|
state: State,
|
|
|
|
action: ActionUpdateAffiliation
|
|
|
|
) => {
|
|
|
|
const { email, role, department } = action.payload
|
2022-05-24 03:48:06 -04:00
|
|
|
|
|
|
|
if (action.payload && !state.data.byId[email]) {
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-04-22 09:49:26 -04:00
|
|
|
const affiliation = state.data.byId[email].affiliation
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
data: {
|
|
|
|
...state.data,
|
|
|
|
byId: {
|
|
|
|
...state.data.byId,
|
|
|
|
[email]: {
|
|
|
|
...state.data.byId[email],
|
|
|
|
...(affiliation && {
|
|
|
|
affiliation: {
|
|
|
|
...affiliation,
|
|
|
|
role,
|
|
|
|
department,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
emailAffiliationBeingEdited: null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
const initialState: State = {
|
|
|
|
isLoading: false,
|
|
|
|
data: {
|
|
|
|
byId: {},
|
2023-05-26 06:56:15 -04:00
|
|
|
emailCount: 0,
|
2022-04-22 09:49:26 -04:00
|
|
|
linkedInstitutionIds: [],
|
|
|
|
emailAffiliationBeingEdited: null,
|
2022-04-08 07:00:46 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const reducer = (state: State, action: Action) => {
|
|
|
|
switch (action.type) {
|
2022-04-22 09:49:26 -04:00
|
|
|
case Actions.SET_DATA:
|
|
|
|
return setData(state, action)
|
2022-04-08 07:00:46 -04:00
|
|
|
case Actions.SET_LOADING_STATE:
|
|
|
|
return setLoadingAction(state, action)
|
2022-04-22 09:49:26 -04:00
|
|
|
case Actions.MAKE_PRIMARY:
|
|
|
|
return makePrimaryAction(state, action)
|
|
|
|
case Actions.DELETE_EMAIL:
|
|
|
|
return deleteEmailAction(state, action)
|
|
|
|
case Actions.SET_EMAIL_AFFILIATION_BEING_EDITED:
|
|
|
|
return setEmailAffiliationBeingEditedAction(state, action)
|
|
|
|
case Actions.UPDATE_AFFILIATION:
|
|
|
|
return updateAffiliationAction(state, action)
|
|
|
|
default:
|
|
|
|
return state
|
2022-04-08 07:00:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function useUserEmails() {
|
2022-05-24 03:47:08 -04:00
|
|
|
const [
|
|
|
|
showInstitutionalLeaversSurveyUntil,
|
|
|
|
setShowInstitutionalLeaversSurveyUntil,
|
|
|
|
] = usePersistedState('showInstitutionalLeaversSurveyUntil', 0, true)
|
2022-04-22 09:49:26 -04:00
|
|
|
const [state, unsafeDispatch] = useReducer(reducer, initialState)
|
|
|
|
const dispatch = useSafeDispatch(unsafeDispatch)
|
2022-05-18 09:46:18 -04:00
|
|
|
const { data, isLoading, isError, isSuccess, runAsync } =
|
|
|
|
useAsync<UserEmailData[]>()
|
2022-04-22 09:49:26 -04:00
|
|
|
|
2022-04-25 07:05:07 -04:00
|
|
|
const getEmails = useCallback(() => {
|
2022-05-24 03:48:06 -04:00
|
|
|
dispatch(ActionCreators.setLoading(true))
|
2022-05-18 09:46:18 -04:00
|
|
|
runAsync(getJSON('/user/emails?ensureAffiliation=true'))
|
2022-04-22 09:49:26 -04:00
|
|
|
.then(data => {
|
|
|
|
dispatch(ActionCreators.setData(data))
|
2022-04-08 07:00:46 -04:00
|
|
|
})
|
2022-04-22 09:49:26 -04:00
|
|
|
.catch(() => {})
|
2022-05-24 03:48:06 -04:00
|
|
|
.finally(() => dispatch(ActionCreators.setLoading(false)))
|
2022-04-22 09:49:26 -04:00
|
|
|
}, [runAsync, dispatch])
|
2022-04-08 07:00:46 -04:00
|
|
|
|
2022-04-25 07:05:07 -04:00
|
|
|
// Get emails on page load
|
|
|
|
useEffect(() => {
|
|
|
|
getEmails()
|
|
|
|
}, [getEmails])
|
|
|
|
|
2022-05-16 04:03:42 -04:00
|
|
|
const resetLeaversSurveyExpiration = useCallback(
|
|
|
|
(deletedEmail: UserEmailData) => {
|
|
|
|
if (
|
|
|
|
deletedEmail.emailHasInstitutionLicence ||
|
|
|
|
deletedEmail.affiliation?.pastReconfirmDate
|
|
|
|
) {
|
2022-05-18 09:45:31 -04:00
|
|
|
const stillHasLicenseAccess = Object.values(state.data.byId).some(
|
|
|
|
userEmail =>
|
|
|
|
userEmail.email !== deletedEmail.email &&
|
|
|
|
userEmail.emailHasInstitutionLicence
|
2022-05-16 04:03:42 -04:00
|
|
|
)
|
2022-05-18 09:45:31 -04:00
|
|
|
if (!stillHasLicenseAccess) {
|
2022-05-24 03:47:08 -04:00
|
|
|
setShowInstitutionalLeaversSurveyUntil(Date.now() + ONE_WEEK_IN_MS)
|
2022-05-16 04:03:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-05-24 03:47:08 -04:00
|
|
|
[state, setShowInstitutionalLeaversSurveyUntil]
|
2022-05-16 04:03:42 -04:00
|
|
|
)
|
|
|
|
|
2022-04-08 07:00:46 -04:00
|
|
|
return {
|
|
|
|
state,
|
2022-04-25 07:05:07 -04:00
|
|
|
isInitializing: isLoading && !data,
|
2022-04-29 07:10:10 -04:00
|
|
|
isInitializingSuccess: isSuccess,
|
2022-04-22 09:49:26 -04:00
|
|
|
isInitializingError: isError,
|
2022-04-25 07:05:07 -04:00
|
|
|
getEmails,
|
2022-05-24 03:47:08 -04:00
|
|
|
showInstitutionalLeaversSurveyUntil,
|
|
|
|
setShowInstitutionalLeaversSurveyUntil,
|
2022-05-16 04:03:42 -04:00
|
|
|
resetLeaversSurveyExpiration,
|
2022-04-22 09:49:26 -04:00
|
|
|
setLoading: useCallback(
|
|
|
|
(flag: boolean) => dispatch(ActionCreators.setLoading(flag)),
|
|
|
|
[dispatch]
|
|
|
|
),
|
|
|
|
makePrimary: useCallback(
|
|
|
|
(email: UserEmailData['email']) =>
|
|
|
|
dispatch(ActionCreators.makePrimary(email)),
|
|
|
|
[dispatch]
|
|
|
|
),
|
|
|
|
deleteEmail: useCallback(
|
|
|
|
(email: UserEmailData['email']) =>
|
|
|
|
dispatch(ActionCreators.deleteEmail(email)),
|
|
|
|
[dispatch]
|
|
|
|
),
|
|
|
|
setEmailAffiliationBeingEdited: useCallback(
|
|
|
|
(email: Nullable<UserEmailData['email']>) =>
|
|
|
|
dispatch(ActionCreators.setEmailAffiliationBeingEdited(email)),
|
|
|
|
[dispatch]
|
|
|
|
),
|
|
|
|
updateAffiliation: useCallback(
|
|
|
|
(
|
|
|
|
email: UserEmailData['email'],
|
|
|
|
role: Affiliation['role'],
|
|
|
|
department: Affiliation['department']
|
|
|
|
) => dispatch(ActionCreators.updateAffiliation(email, role, department)),
|
|
|
|
[dispatch]
|
|
|
|
),
|
2022-04-08 07:00:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserEmailsContext = createContext<
|
|
|
|
ReturnType<typeof useUserEmails> | undefined
|
|
|
|
>(undefined)
|
|
|
|
UserEmailsContext.displayName = 'UserEmailsContext'
|
|
|
|
|
|
|
|
type UserEmailsProviderProps = {
|
|
|
|
children: React.ReactNode
|
|
|
|
} & Record<string, unknown>
|
|
|
|
|
|
|
|
function UserEmailsProvider(props: UserEmailsProviderProps) {
|
|
|
|
const value = useUserEmails()
|
|
|
|
|
|
|
|
return <UserEmailsContext.Provider value={value} {...props} />
|
|
|
|
}
|
|
|
|
|
|
|
|
const useUserEmailsContext = () => {
|
|
|
|
const context = useContext(UserEmailsContext)
|
|
|
|
|
|
|
|
if (context === undefined) {
|
|
|
|
throw new Error('useUserEmailsContext must be used in a UserEmailsProvider')
|
|
|
|
}
|
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
2022-05-24 03:48:06 -04:00
|
|
|
type EmailContextType = ReturnType<typeof useUserEmailsContext>
|
|
|
|
|
|
|
|
export { UserEmailsProvider, useUserEmailsContext, EmailContextType }
|