From 8f5bf6dc5aff384c1b27c984b369d829b7da2005 Mon Sep 17 00:00:00 2001 From: Aman Sethi Date: Thu, 13 Apr 2023 19:34:03 +0530 Subject: [PATCH] TP-25164 location api fix (#240) * TP-25164 location api fix * TP-25164 fix error message * TP-25164 warn eslint * fix error message TP-25164 * remove gibberish TP-25164 --- .eslintrc.json | 1 + App.tsx | 1 - ProtectedRouter.tsx | 2 ++ src/hooks/useNativeButton.tsx | 54 ++++++++++++++++++++++++++--------- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index c2a8a26a..280d28b3 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -22,5 +22,6 @@ "react-native" ], "rules": { + "@typescript-eslint/strict-boolean-expressions": 1 } } diff --git a/App.tsx b/App.tsx index 3a0700f8..8f4a429a 100644 --- a/App.tsx +++ b/App.tsx @@ -85,7 +85,6 @@ const askForPermissions = async ( }; const App = () => { - useNativeButtons(); const [permissions, setPermissions] = React.useState(true); const [isGlobalDocumentMapLoaded, setIsGlobalDocumentMapLoaded] = React.useState(false); diff --git a/ProtectedRouter.tsx b/ProtectedRouter.tsx index b2864312..f5a991c8 100644 --- a/ProtectedRouter.tsx +++ b/ProtectedRouter.tsx @@ -44,6 +44,7 @@ import VKYCFullScreen from './src/screens/caseDetails/VKYCFullScreen'; import { verifyGoogleSignIn } from './src/action/authActions'; import { Linking } from 'react-native'; import { getParamsObject } from './src/components/utlis/commonFunctions'; +import useNativeButtons from './src/hooks/useNativeButton'; const ANIMATION_DURATION = 300; @@ -59,6 +60,7 @@ export enum PageRouteEnum { } const ProtectedRouter = () => { + useNativeButtons(); const user = useSelector( (state: RootState) => state.user, ); diff --git a/src/hooks/useNativeButton.tsx b/src/hooks/useNativeButton.tsx index 3d976283..f09725f5 100644 --- a/src/hooks/useNativeButton.tsx +++ b/src/hooks/useNativeButton.tsx @@ -1,13 +1,18 @@ -import React, { useEffect, useRef, useState } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import { BackHandler, AppState, AppStateStatus } from 'react-native'; -import { GeoCoordinates } from 'react-native-geolocation-service'; +import { useAppSelector } from '.'; import { CLICKSTREAM_EVENT_NAMES } from '../common/Constants'; import { CaptureGeolocation } from '../components/form/services/geoLocation.service'; +import { logError } from '../components/utlis/errorUtils'; import { addClickstreamEvent } from '../services/clickstreamEventService'; +import { RootState } from '../store/store'; import { sendLocationToServer } from './capturingApi'; const THREE_MINUTES = 3 * 60 * 1000; const useNativeButtons = () => { + const { + user: { isLoggedIn }, + } = useAppSelector((state: RootState) => state); const appState = useRef(AppState.currentState); const intervalRef = useRef(0); @@ -16,15 +21,22 @@ const useNativeButtons = () => { return false; }; - const fetchAndSendToserver = async () => { - const location = await CaptureGeolocation.fetchLocation( - 'FETCH_LOCATION', - 0, - ); - if (location) { - sendLocationToServer(location); - } - }; + const fetchAndSendToserver = useCallback( + async () => { + if(!isLoggedIn) { + return; + } + const location = await CaptureGeolocation.fetchLocation( + 'FETCH_LOCATION', + 0, + ); + if (location) { + await sendLocationToServer(location); + } + }, + [isLoggedIn], + ) + const handleAppStateChange = async (nextAppState: AppStateStatus) => { if ( @@ -32,7 +44,7 @@ const useNativeButtons = () => { nextAppState === 'active' ) { addClickstreamEvent(CLICKSTREAM_EVENT_NAMES.AV_APP_FOREGROUND); - fetchAndSendToserver(); + await fetchAndSendToserver(); } if ( appState.current === 'active' && @@ -43,6 +55,16 @@ const useNativeButtons = () => { appState.current = nextAppState; }; + useEffect(() => { + if(isLoggedIn) { + (async () => { + await fetchAndSendToserver(); + })().catch(e => { + logError(e, 'Error during login background location sending'); + }); + } + }, [isLoggedIn, fetchAndSendToserver]); + useEffect(() => { const backHandler = BackHandler.addEventListener( 'hardwareBackPress', @@ -52,9 +74,13 @@ const useNativeButtons = () => { 'change', handleAppStateChange, ); - fetchAndSendToserver(); + fetchAndSendToserver().catch(e => { + logError(e, 'Error during background location sending.'); + }); intervalRef.current = setInterval(() => { - fetchAndSendToserver(); + fetchAndSendToserver().catch(e => { + logError(e, 'Error during background location sending.'); + }); }, THREE_MINUTES); return () => {