-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (107 loc) Β· 3.19 KB
/
index.js
File metadata and controls
122 lines (107 loc) Β· 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Copyright (C) mroads, LLC - All Rights Reserved
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential
* @format
* @flow
*/
import React, {Component} from 'react';
import {View, PanResponder, AppState} from 'react-native';
import moment from 'moment';
export interface Props {
// function to call after timeout
onAction: Function;
//In Minutes
timeToInactivity: number;
children: Array<any> | React.Component;
// Seconds
interval: number;
handleAppState: boolean;
}
export interface State {
logoutTime: moment.Moment;
}
export default class UserInactiveCheck extends Component<Props, State> {
panResponder = {};
timeInterval = null;
constructor(props: Props) {
super(props);
this.state = {
logoutTime: null,
};
//It Listens to the child content/ component actions mentioned in the object
this.panResponder = PanResponder.create({
onStartShouldSetPanResponder: () => {
this.updateInactiveTime();
return true;
},
onMoveShouldSetPanResponder: () => true,
onStartShouldSetPanResponderCapture: () => {
this.updateInactiveTime();
return false;
},
onMoveShouldSetPanResponderCapture: () => false,
onPanResponderTerminationRequest: () => true,
onShouldBlockNativeResponder: () => false,
});
// Timer and countdown starts only if app is active
if (props.handleAppState) {
AppState.addEventListener('change', appState => {
if (appState === 'active') {
this.resetTimer();
} else {
this.clearTimer();
}
});
}
this.timeInterval = null;
}
componentWillUnmount() {
// clearing the timer at unmount
this.clearTimer();
const {handleAppState} = this.props;
if (handleAppState) {
AppState.removeEventListener('change');
}
}
resetTimer = () => {
const {interval = 5} = this.props;
// setting logout time and count down time
this.updateInactiveTime();
this.timeInterval = setInterval(this.checkTimeForAction, interval * 1000);
};
clearTimer = () => {
this.timeInterval && clearInterval(this.timeInterval);
};
// It checks if logout time has come for every given interval
checkTimeForAction = () => {
const {logoutTime} = this.state;
const {onAction, interval = 5} = this.props;
const duration = logoutTime.diff(moment(), 'seconds');
// console.info('check duration ==> ', duration, 'Sec left');
if (duration > -1 && duration <= interval) {
// console.info('calling action');
onAction && onAction();
this.clearTimer()
}
};
// Setting the logout time for the given inactive period.
updateInactiveTime = () => {
let {logoutTime} = this.state;
const {timeToInactivity = 5} = this.props;
logoutTime = moment().add(timeToInactivity, 'minutes');
// console.info(
// 'time got reset ==>',
// logoutTime.diff(moment(), 'seconds'),
// '==> seconds left',
// );
this.setState({logoutTime});
};
render() {
const {children} = this.props;
return (
<View style={{flex: 1}} {...this.panResponder.panHandlers}>
{children}
</View>
);
}
}