-
Notifications
You must be signed in to change notification settings - Fork 1
Session Timeout #46
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
Open
isaqueha
wants to merge
44
commits into
master
Choose a base branch
from
feat-session-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Session Timeout #46
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
079aaad
feat: information dialog
isaqueha 2c6784c
feat: session timeout dialog
isaqueha 3dedc7e
feat: snapshot tests
isaqueha 3442097
fix: renaming and fixes
isaqueha 8f86ab3
fix: move useref to sessiontimeout
isaqueha 5c1bbc3
fix: use text instead of textwrapper
isaqueha 52f25bd
fix: move constants to their own context
isaqueha b19ec16
fix: use constant styles definition
isaqueha 7b24481
feat: informationZone component
isaqueha d2b1998
fix: refactor tests
isaqueha 48f5a58
Merge branch 'master' into feat-session-timeout
isaqueha 58e512c
Merge branch 'master' into feat-session-timeout
isaqueha 56ec5de
fix: prettier rules
isaqueha aa76f33
fix: prettier rules
isaqueha f8b5c94
Merge branch 'master' into feat-session-timeout
isaqueha fef0b89
fix: remove old files
isaqueha 2d91b35
fix: prettier path was blocking push
isaqueha a29d9be
fix: remove old files
isaqueha ee9c4db
refactor: reverted since it does not need anymore
8a5f2ff
fix: existing bug of changin mode
13209c0
refactor: removed specific close i18n
fada466
refactor: removed stale prop
a4ff8e3
refactor: ioc timeouts
9dd3bec
add sample link to information dialog
210815b
Merge branch 'feat-session-timeout' of https://github.com/LuisValgoi/…
9408a07
Adding tests to information dialog
1405d4c
fix: information zone as a function component
isaqueha 75caa09
fix: prettier formatting
isaqueha b6ebf71
fix: prettier formatting
isaqueha 4b0beac
fix: tests for session timeout
isaqueha cd6e159
update snapshot
36fd956
removing snapshot test
7f2da29
remove snapshot testing
isaqueha ce89b44
Merge branch 'master' into feat-session-timeout
isaqueha aebb38a
remove failing test
isaqueha e8de4e6
remove duplicated import
isaqueha 40a54a8
fix: remove bad snapshot test
isaqueha c7ae4e5
fix: warning inside constant
isaqueha 90901f6
remove unecessary step
55cfea9
remove blank space
lpohren 1341685
remove blank space
lpohren 479b022
remove blank space 1
lpohren af633b9
Merge branch 'feat-session-timeout' of https://github.com/LuisValgoi/…
lpohren f840d99
chore: remove blank space 2
lpohren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,132 @@ | ||
import React, { useEffect } from 'react'; | ||
isaqueha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { useTranslation } from 'react-i18next'; | ||
import { spacing } from '@ui5/webcomponents-react-base'; | ||
import { Icon } from '@ui5/webcomponents-react/lib/Icon'; | ||
import { Button } from '@ui5/webcomponents-react/lib/Button'; | ||
import { ButtonDesign } from '@ui5/webcomponents-react/lib/ButtonDesign'; | ||
import { Dialog } from '@ui5/webcomponents-react/lib/Dialog'; | ||
import { FlexBox } from '@ui5/webcomponents-react/lib/FlexBox'; | ||
import { FlexBoxDirection } from '@ui5/webcomponents-react/lib/FlexBoxDirection'; | ||
import { FlexBoxAlignItems } from '@ui5/webcomponents-react/lib/FlexBoxAlignItems'; | ||
import { FlexBoxJustifyContent } from '@ui5/webcomponents-react/lib/FlexBoxJustifyContent'; | ||
import { Text } from '@ui5/webcomponents-react/lib/Text'; | ||
|
||
const KEYBOARD_KEYS = { | ||
ESCAPE: 27, | ||
}; | ||
|
||
const style = { | ||
warning: { | ||
width: '1.5rem', | ||
height: '1.5rem', | ||
color: '#feb60a', | ||
}, | ||
error: { | ||
width: '1.5rem', | ||
height: '1.5rem', | ||
color: '#ff5254', | ||
}, | ||
information: { | ||
width: '1.5rem', | ||
height: '1.5rem', | ||
color: 'black', | ||
}, | ||
text: { | ||
lineHeight: '20px', | ||
}, | ||
}; | ||
|
||
const _getHeaderIcon = (type) => { | ||
switch (type) { | ||
case Type.Warning: | ||
return _getHeaderWarningIcon(); | ||
case Type.Error: | ||
return _getHeaderErrorIcon(); | ||
default: | ||
return _getHeaderInfoIcon(); | ||
} | ||
}; | ||
|
||
const _getHeaderWarningIcon = () => { | ||
return <Icon name="message-warning" style={style.warning} />; | ||
}; | ||
|
||
const _getHeaderErrorIcon = () => { | ||
return <Icon name="message-error" style={style.error} />; | ||
}; | ||
|
||
const _getHeaderInfoIcon = () => { | ||
return <Icon name="message-information" style={style.information} />; | ||
}; | ||
|
||
const _handleAvoidEscapeClosing = (avoidEscapeClose) => { | ||
document.addEventListener( | ||
'keydown', | ||
(e) => { | ||
if (e.keyCode === KEYBOARD_KEYS.ESCAPE && avoidEscapeClose) { | ||
e.stopPropagation(); | ||
} | ||
}, | ||
true, | ||
); | ||
}; | ||
|
||
const InformationDialog = ({ dialogRef, avoidEscapeClose, headerText, innerText, closeButtonText, children, onClose, type }) => { | ||
const { t } = useTranslation(); | ||
|
||
useEffect(() => { | ||
_handleAvoidEscapeClosing(avoidEscapeClose); | ||
}); | ||
|
||
const _onClose = () => { | ||
onClose && onClose(); | ||
if (dialogRef.current) { | ||
dialogRef.current.close(); | ||
} | ||
}; | ||
|
||
const _getFooter = () => { | ||
return ( | ||
<FlexBox alignItems={FlexBoxAlignItems.Center} direction={FlexBoxDirection.RowReverse} style={spacing.sapUiTinyMargin}> | ||
<Button design={ButtonDesign.Transparent} onClick={_onClose}> | ||
{closeButtonText ? closeButtonText : t('app.generics.close')} | ||
</Button> | ||
</FlexBox> | ||
); | ||
}; | ||
|
||
const _getHeader = () => { | ||
return ( | ||
<FlexBox alignItems={FlexBoxAlignItems.Center} justifyContent={FlexBoxJustifyContent.Center} style={spacing.sapUiContentPadding}> | ||
{_getHeaderIcon(type)} | ||
<Text tooltip={headerText} wrapping style={{ ...spacing.sapUiTinyMarginBegin, ...style.text }}> | ||
{headerText} | ||
</Text> | ||
</FlexBox> | ||
); | ||
}; | ||
|
||
return ( | ||
<Dialog ref={dialogRef} slot="header" header={_getHeader()} footer={_getFooter()} onAfterClose={_onClose} data-testid="information-dialog"> | ||
<div style={{ ...spacing.sapUiContentPadding }}> | ||
<FlexBox direction={FlexBoxDirection.Column}> | ||
{innerText ? ( | ||
<Text tooltip={innerText} wrapping style={{ ...spacing.sapUiTinyMarginBegin, ...style.text }}> | ||
{innerText} | ||
</Text> | ||
) : ( | ||
children | ||
)} | ||
</FlexBox> | ||
</div> | ||
</Dialog> | ||
); | ||
}; | ||
|
||
export default InformationDialog; | ||
|
||
export const Type = { | ||
Warning: 'WARNING', | ||
Error: 'ERROR', | ||
Info: 'INFO', | ||
}; |
25 changes: 25 additions & 0 deletions
25
src/components/InformationDialog/InformationDialog.test.js
This file contains hidden or 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,25 @@ | ||
import React from 'react'; | ||
|
||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen } from '../../util/TestSetup'; | ||
import InformationDialog, { Type } from './InformationDialog'; | ||
|
||
describe('InformationDialog.js Test Suite', () => { | ||
test('should render', () => { | ||
const dialog = <InformationDialog avoidEscapeClose type={Type.Warning} headerText={'Header text'} closeButtonText={'Close'} innerText={'Inner text'} />; | ||
render(dialog); | ||
const infoDialog = screen.getByTestId('information-dialog'); | ||
expect(infoDialog).toBeInTheDocument(); | ||
}); | ||
|
||
test('should render child when not inner text is passed', () => { | ||
const dialog = ( | ||
<InformationDialog avoidEscapeClose type={Type.Warning} headerText={'Header text'} closeButtonText={'Close'}> | ||
<div data-testid="information-dialog-child"></div> | ||
</InformationDialog> | ||
); | ||
render(dialog); | ||
const child = screen.getByTestId('information-dialog-child'); | ||
expect(child).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains hidden or 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,3 @@ | ||
export default function InformationZone({ children }) { | ||
return children; | ||
} |
This file contains hidden or 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,70 @@ | ||
import React, { useEffect, useState, useRef } from 'react'; | ||
import InformationDialog, { Type } from '../InformationDialog/InformationDialog'; | ||
import i18n from '../../util/i18n'; | ||
|
||
const SESSION = { | ||
TIMEOUT_INTERVAL: 60000, | ||
REFRESH_LIMIT: 15, | ||
REFRESH_WARNING: 13, | ||
}; | ||
|
||
const TIMEOUT_MODE = { | ||
type: Type.Error, | ||
headerText: i18n.t('session.expired'), | ||
closeButtonText: i18n.t('session.expired.button.reload'), | ||
innerText: i18n.t('session.expired.text'), | ||
onClose: () => window.location.reload(), | ||
}; | ||
|
||
const WARNING_MODE = { | ||
type: Type.Warning, | ||
headerText: i18n.t('session.warning'), | ||
closeButtonText: i18n.t('app.generics.close'), | ||
innerText: i18n.t('session.warning.text'), | ||
onClose: null, | ||
}; | ||
|
||
const SessionTimeoutDialog = ({ timeoutScale = SESSION.TIMEOUT_INTERVAL, hasExpiredLimit = SESSION.REFRESH_LIMIT, isExpiringLimit = SESSION.REFRESH_WARNING }) => { | ||
const dialogRef = useRef(null); | ||
const ACTIVITY_EVENTS = ['click', 'focus', 'blur', 'keyup', 'keydown', 'mousemove', 'scroll']; | ||
const [sessionIntervalCount, setSessionIntervalCount] = useState(1); | ||
const [options, setOptions] = useState(WARNING_MODE); | ||
|
||
useEffect(() => { | ||
let sessionIntervalFinder = setInterval(() => { | ||
if (sessionIntervalCount >= isExpiringLimit && sessionIntervalCount < hasExpiredLimit) { | ||
setOptions(WARNING_MODE); | ||
dialogRef.current && dialogRef.current.open(); | ||
} else if (sessionIntervalCount >= hasExpiredLimit) { | ||
setOptions(TIMEOUT_MODE); | ||
dialogRef.current && dialogRef.current.open(); | ||
} | ||
|
||
setSessionIntervalCount(sessionIntervalCount + 1); | ||
}, timeoutScale); | ||
handleUserActivity(sessionIntervalFinder); | ||
}); | ||
|
||
const handleUserActivity = (sessionIntervalFinder) => { | ||
ACTIVITY_EVENTS.forEach((EVENT) => { | ||
window.addEventListener(EVENT, () => { | ||
setSessionIntervalCount(0); | ||
clearInterval(sessionIntervalFinder); | ||
}); | ||
}); | ||
}; | ||
|
||
return ( | ||
<InformationDialog | ||
avoidEscapeClose | ||
dialogRef={dialogRef} | ||
type={options.type} | ||
onClose={options.onClose} | ||
headerText={options.headerText} | ||
closeButtonText={options.closeButtonText} | ||
innerText={options.innerText} | ||
/> | ||
); | ||
}; | ||
|
||
export default SessionTimeoutDialog; |
17 changes: 17 additions & 0 deletions
17
src/components/SessionTimeout/SessionTimeoutDialog.test.js
This file contains hidden or 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,17 @@ | ||
import React from 'react'; | ||
|
||
import '@testing-library/jest-dom/extend-expect'; | ||
import { render, screen, waitFor } from '../../util/TestSetup'; | ||
import SessionTimeoutDialog from './SessionTimeoutDialog'; | ||
|
||
describe('SessionTimeoutDialog.js Test Suite', () => { | ||
beforeEach(() => { | ||
render(<SessionTimeoutDialog timeoutScale={1000} hasExpiredLimit={15} isExpiringLimit={13} />); | ||
}); | ||
|
||
test('should render, wait 13 cycles and see the text “Session Almost Expiring”', async () => { | ||
const text = 'Session Almost Expiring'; | ||
const warning = await waitFor(() => screen.getByText(text)); | ||
expect(warning).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.