|
@@ -13,6 +13,9 @@ import { Affiliation } from '../../../../../types/affiliation'
|
|
|
import { normalize, NormalizedObject } from '../../../utils/normalize'
|
|
import { normalize, NormalizedObject } from '../../../utils/normalize'
|
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
import { getJSON } from '../../../infrastructure/fetch-json'
|
|
|
import useAsync from '../../../shared/hooks/use-async'
|
|
import useAsync from '../../../shared/hooks/use-async'
|
|
|
|
|
+import usePersistedState from '../../../shared/hooks/use-persisted-state'
|
|
|
|
|
+
|
|
|
|
|
+const ONE_WEEK_IN_MS = 7 * 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
export enum Actions {
|
|
export enum Actions {
|
|
@@ -194,6 +197,10 @@ const reducer = (state: State, action: Action) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function useUserEmails() {
|
|
function useUserEmails() {
|
|
|
|
|
+ const [, setExpirationDate] = usePersistedState(
|
|
|
|
|
+ 'showInstitutionalLeaversSurveyUntil',
|
|
|
|
|
+ 0
|
|
|
|
|
+ )
|
|
|
const [state, unsafeDispatch] = useReducer(reducer, initialState)
|
|
const [state, unsafeDispatch] = useReducer(reducer, initialState)
|
|
|
const dispatch = useSafeDispatch(unsafeDispatch)
|
|
const dispatch = useSafeDispatch(unsafeDispatch)
|
|
|
const { data, isLoading, isError, isSuccess, runAsync } = useAsync()
|
|
const { data, isLoading, isError, isSuccess, runAsync } = useAsync()
|
|
@@ -211,12 +218,34 @@ function useUserEmails() {
|
|
|
getEmails()
|
|
getEmails()
|
|
|
}, [getEmails])
|
|
}, [getEmails])
|
|
|
|
|
|
|
|
|
|
+ const resetLeaversSurveyExpiration = useCallback(
|
|
|
|
|
+ (deletedEmail: UserEmailData) => {
|
|
|
|
|
+ if (!data) {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ const emailData = data as UserEmailData[]
|
|
|
|
|
+ if (
|
|
|
|
|
+ deletedEmail.emailHasInstitutionLicence ||
|
|
|
|
|
+ deletedEmail.affiliation?.pastReconfirmDate
|
|
|
|
|
+ ) {
|
|
|
|
|
+ const stillHasLicenseAccess = emailData.some(
|
|
|
|
|
+ userEmail => userEmail.emailHasInstitutionLicence
|
|
|
|
|
+ )
|
|
|
|
|
+ if (stillHasLicenseAccess) {
|
|
|
|
|
+ setExpirationDate(Date.now() + ONE_WEEK_IN_MS)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ [data, setExpirationDate]
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
state,
|
|
state,
|
|
|
isInitializing: isLoading && !data,
|
|
isInitializing: isLoading && !data,
|
|
|
isInitializingSuccess: isSuccess,
|
|
isInitializingSuccess: isSuccess,
|
|
|
isInitializingError: isError,
|
|
isInitializingError: isError,
|
|
|
getEmails,
|
|
getEmails,
|
|
|
|
|
+ resetLeaversSurveyExpiration,
|
|
|
setLoading: useCallback(
|
|
setLoading: useCallback(
|
|
|
(flag: boolean) => dispatch(ActionCreators.setLoading(flag)),
|
|
(flag: boolean) => dispatch(ActionCreators.setLoading(flag)),
|
|
|
[dispatch]
|
|
[dispatch]
|