-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Naomi Aro
committed
Oct 12, 2021
1 parent
eadf23b
commit 8ff0cf3
Showing
3 changed files
with
40 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, {useEffect} from 'react'; | ||
import throttle from 'lodash.throttle'; | ||
|
||
interface Props { | ||
refreshUrl: string; | ||
} | ||
|
||
const SessionRefresher: React.FunctionComponent<Props> = ({refreshUrl}) => { | ||
const extendSession = async () => { | ||
try { | ||
await fetch(refreshUrl); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const throttledSession = throttle(extendSession, 1000 * 60); | ||
window.addEventListener('keydown', throttledSession); | ||
window.addEventListener('mousedown', throttledSession); | ||
window.addEventListener('scroll', throttledSession); | ||
|
||
return () => { | ||
window.removeEventListener('keydown', throttledSession); | ||
window.removeEventListener('mousedown', throttledSession); | ||
window.removeEventListener('scroll', throttledSession); | ||
throttledSession.cancel(); | ||
}; | ||
}, []); | ||
|
||
return null; | ||
}; | ||
|
||
export default SessionRefresher; |
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