forked from Sage-Bionetworks/synapse-web-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Sage-Bionetworks#1310 from nickgros/redirect-when-…
…not-accept-tou
- Loading branch information
Showing
4 changed files
with
129 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
apps/SageAccountWeb/src/hooks/useMaybeRedirectToSignTermsOfService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { TermsOfServiceState } from '@sage-bionetworks/synapse-types' | ||
import { useEffect, useRef } from 'react' | ||
import { useHistory, useLocation } from 'react-router-dom' | ||
import { | ||
storeLastPlace, | ||
useApplicationSessionContext, | ||
} from 'synapse-react-client' | ||
|
||
type UseMaybeRedirectToSignTermsOfServiceReturn = { | ||
// if true, the user may be redirected (or has already been redirected) to sign the ToS | ||
// i.e. don't assume the ToS has been signed and boot the user to the original app until this returns false! | ||
mayRedirect: boolean | ||
} | ||
|
||
export default function useMaybeRedirectToSignTermsOfService(): UseMaybeRedirectToSignTermsOfServiceReturn { | ||
// Detect if terms of service are up to date. If not, route to either the Pledge or a page where the user can sign the updated terms. | ||
// Note, if the status is "MUST_AGREE_SOON", then the new page will offer a "Skip" button | ||
const skippedSigningUpdatedToS = | ||
sessionStorage.getItem('skippedSigningToS') === 'true' | ||
const { termsOfServiceStatus } = useApplicationSessionContext() | ||
|
||
const history = useHistory() | ||
const location = useLocation() | ||
|
||
// true until we confirm we will not redirect | ||
const mayRedirect = useRef(true) | ||
|
||
useEffect(() => { | ||
if (termsOfServiceStatus) { | ||
if ( | ||
termsOfServiceStatus.userCurrentTermsOfServiceState != | ||
TermsOfServiceState.UP_TO_DATE && | ||
!skippedSigningUpdatedToS | ||
) { | ||
if ( | ||
termsOfServiceStatus.lastAgreementDate == undefined && | ||
location.pathname != '/authenticated/signTermsOfUse' | ||
) { | ||
history.push('/authenticated/signTermsOfUse') | ||
} else if ( | ||
termsOfServiceStatus.lastAgreementDate != null && | ||
location.pathname != '/authenticated/signUpdatedTermsOfUse' | ||
) { | ||
storeLastPlace() | ||
history.push('/authenticated/signUpdatedTermsOfUse') | ||
} | ||
} | ||
mayRedirect.current = false | ||
} | ||
}, [ | ||
termsOfServiceStatus, | ||
skippedSigningUpdatedToS, | ||
location.pathname, | ||
history, | ||
]) | ||
|
||
return { mayRedirect: mayRedirect.current } | ||
} |