Files
inji-wallet/screens/Settings/SettingScreenController.ts
PoojaBabusing 0af87909c9 Sonar Pipeline tw mosip (#768)
* fix(INJI-223): show the camera in scan screen while sharing the VC and click on scan icon

Issue Link https://mosip.atlassian.net/browse/INJI-223

* chore: update pod deps' to match development team of 1st project

* feat[#211]:[Pooja] fix turncated passcode and confirm password button

* feat[#211]:[Pooja] fix truncated histroy title text in iOS

* feat[#211]:[Pooja] fix multiple error being displayed when the vc binding fails

* fix(INJI-223): disconnect the device while sharing the VC and click on tab icons except scan icon

Issue Link https://mosip.atlassian.net/browse/INJI-223

* feat[#211]:[Pooja] fix download card button styles

* version code for beta build in pipeline

* feat[#211]:[Pooja] add error message when VC activation fails in kebab pop up

* chore(INJI-216): bump up tuvali version to v0.4.3

* feat[#225]:[Pooja] fix about inji url in about page

* refactor(INJI-223): get the scan string from route config to keep it consistent

Issue Link https://mosip.atlassian.net/browse/INJI-223

* feat[mosip#211]:[Pooja] refactor onCancel

Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>

* feat(INJI-222): add popup for error in decryption

* feat(INJI-222): wrap app init component & popups

* feat(INJI-222): propagate event from _store to app state machine

Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>

* chore(INJI-222): move error msg text to errors obj

* Implemented Receive Card & Received Cards options in Settings

* Fixed all the locals

* [Pooja] add quality gate check for critical open bugs in sonar

---------

Co-authored-by: PuBHARGAVI <46226958+PuBHARGAVI@users.noreply.github.com>
Co-authored-by: vijay151096 <94220135+vijay151096@users.noreply.github.com>
Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>
Co-authored-by: Pooja Babusingh <68894211+PoojaBabusingh@users.noreply.github.com>
Co-authored-by: adityankannan-tw <adityan.kannan@thoughtworks.com>
Co-authored-by: Alka <prasadalka1998@gmail.com>
Co-authored-by: anil_majji <majjianilkumar050@gmail.com>
Co-authored-by: KiruthikaJeyashankar <81218987+KiruthikaJeyashankar@users.noreply.github.com>
2023-08-09 11:55:50 +05:30

167 lines
5.2 KiB
TypeScript

import { useMachine, useSelector } from '@xstate/react';
import { useContext, useEffect, useState } from 'react';
import * as LocalAuthentication from 'expo-local-authentication';
import { selectBackendInfo } from '../../machines/app';
import {
AuthEvents,
selectBiometrics,
selectCanUseBiometrics,
} from '../../machines/auth';
import {
selectBiometricUnlockEnabled,
selectName,
selectCredentialRegistryResponse,
selectVcLabel,
selectCredentialRegistry,
SettingsEvents,
selectAppId,
selectIsResetInjiProps,
} from '../../machines/settings';
import {
biometricsMachine,
selectError,
selectIsSuccess,
selectUnenrolledNotice,
} from '../../machines/biometrics';
import { GlobalContext } from '../../shared/GlobalContext';
import { useTranslation } from 'react-i18next';
import { Platform } from 'react-native';
import { RequestRouteProps } from '../../routes';
export function useSettingsScreen(props: RequestRouteProps) {
const { appService } = useContext(GlobalContext);
const authService = appService.children.get('auth');
const settingsService = appService.children.get('settings');
const [isVisible, setIsVisible] = useState(false);
const [alertMsg, setHasAlertMsg] = useState('');
const authBiometrics = useSelector(authService, selectBiometrics);
const [biometricState, biometricSend, bioService] =
useMachine(biometricsMachine);
const isSuccessBio: boolean = useSelector(bioService, selectIsSuccess);
const errorMsgBio: string = useSelector(bioService, selectError);
const unEnrolledNoticeBio: string = useSelector(
bioService,
selectUnenrolledNotice
);
const { t } = useTranslation('AuthScreen');
useEffect(() => {
setTimeout(async () => {
const hasEnrolledBiometrics = await LocalAuthentication.isEnrolledAsync();
if (!hasEnrolledBiometrics) {
authService.send(AuthEvents.SETUP_BIOMETRICS(''));
settingsService.send(SettingsEvents.TOGGLE_BIOMETRIC_UNLOCK(false));
}
}, 0);
// if biometic state is success then lets send auth service BIOMETRICS
if (isSuccessBio) {
authService.send(AuthEvents.SETUP_BIOMETRICS('true'));
settingsService.send(SettingsEvents.TOGGLE_BIOMETRIC_UNLOCK(true));
// handle biometric failure unknown error
} else {
const error: string = errorMsgBio ?? unEnrolledNoticeBio ?? '';
if (error != '') {
setHasAlertMsg(t(error));
}
}
}, [isSuccessBio, errorMsgBio, unEnrolledNoticeBio]);
const useBiometrics = (value: boolean) => {
if (value) {
// But check if we already enrolled biometrics
if (authBiometrics) {
authService.send(AuthEvents.SETUP_BIOMETRICS('true'));
settingsService.send(SettingsEvents.TOGGLE_BIOMETRIC_UNLOCK(true));
// but if device does not have any enrolled biometrics
} else if (biometricState.matches({ failure: 'unenrolled' })) {
biometricSend({ type: 'RETRY_AUTHENTICATE' });
// otherwise lets do a biometric auth
} else {
biometricSend({ type: 'AUTHENTICATE' });
}
} else {
authService.send(AuthEvents.SETUP_BIOMETRICS(''));
settingsService.send(SettingsEvents.TOGGLE_BIOMETRIC_UNLOCK(false));
}
};
const hideAlert = () => {
setHasAlertMsg('');
};
return {
isVisible,
alertMsg,
hideAlert,
appId: useSelector(settingsService, selectAppId),
backendInfo: useSelector(appService, selectBackendInfo),
name: useSelector(settingsService, selectName),
vcLabel: useSelector(settingsService, selectVcLabel),
credentialRegistry: useSelector(settingsService, selectCredentialRegistry),
credentialRegistryResponse: useSelector(
settingsService,
selectCredentialRegistryResponse
),
isBiometricUnlockEnabled: useSelector(
settingsService,
selectBiometricUnlockEnabled
),
isResetInjiProps: useSelector(settingsService, selectIsResetInjiProps),
canUseBiometrics: useSelector(authService, selectCanUseBiometrics),
useBiometrics,
UPDATE_NAME: (name: string) =>
settingsService.send(SettingsEvents.UPDATE_NAME(name)),
TOGGLE_SETTINGS: () => setIsVisible(!isVisible),
UPDATE_VC_LABEL: (label: string) =>
settingsService.send(SettingsEvents.UPDATE_VC_LABEL(label)),
UPDATE_CREDENTIAL_REGISTRY: (credentialRegistry: string) =>
settingsService.send(
SettingsEvents.UPDATE_CREDENTIAL_REGISTRY(credentialRegistry)
),
UPDATE_CREDENTIAL_REGISTRY_RESPONSE: (credentialRegistryResponse: string) =>
settingsService.send(
SettingsEvents.UPDATE_CREDENTIAL_REGISTRY_RESPONSE(
credentialRegistryResponse
)
),
RECEIVE_CARD: () => {
props.navigation.navigate('Request');
setIsVisible(false);
},
TOGGLE_BIOMETRIC: (enable: boolean) =>
settingsService.send(SettingsEvents.TOGGLE_BIOMETRIC_UNLOCK(enable)),
LOGOUT: () => {
setIsVisible(false);
const navigate = () => {
authService.send(AuthEvents.LOGOUT());
};
if (Platform.OS === 'ios') {
setTimeout(() => navigate(), 0);
} else {
navigate();
}
},
CANCEL: () => {
settingsService.send(SettingsEvents.CANCEL());
},
};
}