-
Notifications
You must be signed in to change notification settings - Fork 35
Cumulus 3624 #1135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Cumulus 3624 #1135
Changes from 13 commits
d4ca242
c808052
daba97a
cc7f1c1
3e05139
7eb8cfb
fe85a38
77114c0
d95a28b
dc7eb65
059dccf
1d2e089
f12ac4f
610c30c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import React, { useEffect, useState, useCallback } from 'react'; | ||
| import { connect } from 'react-redux'; | ||
| import PropTypes from 'prop-types'; | ||
| import get from 'lodash/get'; | ||
| import DefaultModal from './modal'; | ||
| import { logout } from '../../actions'; | ||
|
|
||
| const InactivityModal = ({ | ||
| title = 'Session Timeout Warning', | ||
| children = `We have noticed that you have been inactive for a while. | ||
| We will close this session in 5 minutes. If you want to stay signed in, select ‘Continue Session’.`, | ||
| dispatch | ||
| }) => { | ||
| const [lastKeyPress, setLastKeyPress] = useState(Date.now()); | ||
| const [hasModal, setHasModal] = useState(false); | ||
|
|
||
| function handleConfirm() { | ||
| setLastKeyPress(Date.now()); | ||
| closeModal(); | ||
| } | ||
|
|
||
| const handleLogout = useCallback(() => { | ||
| dispatch(logout()).then(() => { | ||
| if (get(window, 'location.reload')) { | ||
| window.location.reload(); | ||
| } | ||
| }); | ||
| }, [dispatch]); | ||
|
|
||
| function closeModal() { // the X botton | ||
| setLastKeyPress(Date.now()); | ||
| if (hasModal) { | ||
| setHasModal(false); // hide modal if user resumes activity | ||
| } | ||
| } | ||
|
|
||
| // Effect to setup event listeners for keyboard activity | ||
| useEffect(() => { | ||
| // Function to update the lastKeyPress time | ||
| const handleKeypress = () => { | ||
| setLastKeyPress(Date.now()); | ||
| if (hasModal) { | ||
| setHasModal(false); // hide modal if user resumes activity | ||
| } | ||
| }; | ||
| window.addEventListener('keydown', handleKeypress); | ||
|
|
||
| return () => { | ||
| window.removeEventListener('keydown', handleKeypress); | ||
| }; | ||
| }, [hasModal]); | ||
|
|
||
| // Effect to handle showing the modal after 30 minutes of inactivity | ||
| useEffect(() => { | ||
| const checkInactivity = setInterval(() => { | ||
| if (Date.now() - lastKeyPress > 1800000 && !hasModal) { // 1800000 ms = 30 minutes | ||
|
||
| setHasModal(true); | ||
| } | ||
| }, 60000); // Check every minute (60000 ms) | ||
|
|
||
| return () => clearInterval(checkInactivity); | ||
| }, [hasModal, lastKeyPress]); | ||
|
|
||
| return ( | ||
| <div> | ||
| <DefaultModal | ||
| title = {title} | ||
| className='IAModal' | ||
| onCancel={handleLogout} | ||
| onCloseModal={closeModal} | ||
| onConfirm={handleConfirm} | ||
| showModal={hasModal} | ||
| hasConfirmButton={true} | ||
| hasCancelButton={true} | ||
| cancelButtonText='Close Session' | ||
| confirmButtonText='Continue Session' | ||
| children = {children} | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| const mapStateToProps = (state) => ({ | ||
| hasModal: state.hasModal, | ||
| lastKeyPress: state.lastKeyPress, | ||
| // logoutTimer: state.logoutTimer | ||
jennyhliu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| InactivityModal.propTypes = { | ||
| hasModal: PropTypes.bool, | ||
| lastKeyPress: PropTypes.string, | ||
| // logoutTimer: PropTypes.object, | ||
| title: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), | ||
| children: PropTypes.string, | ||
| className: PropTypes.string, | ||
| cancelButtonText: PropTypes.string, | ||
| confirmButtonText: PropTypes.string, | ||
| showModal: PropTypes.bool, | ||
| onCloseModal: PropTypes.func, | ||
| onConfirm: PropTypes.func, | ||
| onCancel: PropTypes.func, | ||
| hasCancelButton: PropTypes.bool, | ||
| hasConfirmButton: PropTypes.bool, | ||
| dispatch: PropTypes.func, | ||
| }; | ||
|
|
||
| export { InactivityModal }; | ||
|
|
||
| export default connect(mapStateToProps)(InactivityModal); | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| 'use strict'; | ||
|
|
||
| import test from 'ava'; | ||
| import Adapter from '@wojtekmaj/enzyme-adapter-react-17'; | ||
| import React from 'react'; | ||
| import { shallow, configure } from 'enzyme'; | ||
| import { InactivityModal } from '../../../app/src/js/components/Modal/InactivityModal'; | ||
|
|
||
| configure({ adapter: new Adapter() }); | ||
|
|
||
| test('Inactivity Modal should render when hasModal is true', function (t) { | ||
| const hasModal = true ; | ||
| const hasCancelButton = 'button--cancel'; | ||
| const hasConfirmButton = 'button--submit'; | ||
|
|
||
| // Create a shallow render of the component | ||
| const modal = shallow( | ||
| <InactivityModal | ||
| hasModal={hasModal} | ||
| hasCancelButton={hasCancelButton} | ||
| hasConfirmButton={hasConfirmButton} | ||
| /> | ||
| ); | ||
| t.true(modal.find('DefaultModal').exists(),'Modal should be present when hasModal is true'); | ||
| t.is(modal.find('DefaultModal').prop('hasCancelButton'), true); | ||
| t.is(modal.find('DefaultModal').prop('hasConfirmButton'), true); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.