From eaa8e46f6b6b677cbdc11b89bcb6780b0c83f858 Mon Sep 17 00:00:00 2001 From: James Lewis Date: Thu, 12 Nov 2020 15:00:21 +1100 Subject: [PATCH 1/2] adding isEnabled prop --- src/index.tsx | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 232b10b..f72b389 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -51,6 +51,12 @@ export interface UserInactivityProps { */ children: React.ReactNode; + /** + * When set to false, cancels running timers + * When set to true, resets timer + */ + isEnabled?: boolean; + /** * If set to true, the timer is not reset when the keyboard appears * or disappears. @@ -77,6 +83,7 @@ const UserInactivity: React.FC = ({ isActive, onAction, skipKeyboard, + isEnabled, style, timeForInactivity, timeoutHandler, @@ -107,6 +114,20 @@ const UserInactivity: React.FC = ({ } }, [isActive]); + /** + * Handle enabled on or off + */ + const initialEnabled = isEnabled === undefined ? true : isEnabled; + const [enabled, setEnabled] = useState(initialActive); + useEffect(() => { + setEnabled(isEnabled); + if (isEnabled) { + setTimer(); + } else { + cancelTimer(); + } + }, [isEnabled]); + const [date, setDate] = useState(Date.now()); /** @@ -127,6 +148,9 @@ const UserInactivity: React.FC = ({ useEffect(() => { if (isFirstRender.current) { isFirstRender.current = false; + if (!initialEnabled) { + cancelTimer(); + } } else { if (active) { onAction(true); @@ -159,12 +183,19 @@ const UserInactivity: React.FC = ({ * `this.state.inactive` turns to true. */ function resetTimerDueToActivity() { + if (!enabled) { + return; + } cancelTimer(); - setActive(true); - /** - * Causes `useTimeout` to restart. - */ + setTimer(); + } + + /** + * Causes `useTimeout` to restart. + */ + function setTimer() { + setActive(true); setDate(Date.now()); } From d2b378240dfa0ce39ae2cf6d98eb14574429e832 Mon Sep 17 00:00:00 2001 From: James Lewis Date: Thu, 26 Nov 2020 10:05:22 +1100 Subject: [PATCH 2/2] using a ref for the isEnabled so the resetTimer function has the up to date value. --- src/index.tsx | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index f72b389..703fb99 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -115,12 +115,13 @@ const UserInactivity: React.FC = ({ }, [isActive]); /** - * Handle enabled on or off - */ + * When ths isEnabled prop is changed to true, set the timer. + * When the pro is set to false, cancel the timer. + */ const initialEnabled = isEnabled === undefined ? true : isEnabled; - const [enabled, setEnabled] = useState(initialActive); + const enabled = useRef(initialEnabled); useEffect(() => { - setEnabled(isEnabled); + enabled.current = isEnabled === undefined ? true : isEnabled; if (isEnabled) { setTimer(); } else { @@ -148,9 +149,10 @@ const UserInactivity: React.FC = ({ useEffect(() => { if (isFirstRender.current) { isFirstRender.current = false; + // on the first render, cancel the initial timer if the component's isEnabled is false if (!initialEnabled) { cancelTimer(); - } + } } else { if (active) { onAction(true); @@ -181,16 +183,17 @@ const UserInactivity: React.FC = ({ * This method is called whenever a touch is detected. If no touch is * detected after `this.props.timeForInactivity` milliseconds, then * `this.state.inactive` turns to true. + * isEnabled set to false will supress resetting the timer */ function resetTimerDueToActivity() { - if (!enabled) { + if (!enabled.current) { return; } cancelTimer(); setTimer(); } - + /** * Causes `useTimeout` to restart. */