Files
inji-wallet/screens/Home/ViewVcModalController.ts
KiruthikaJeyashankar 55c666b121 feat: download credentials from Esignet using openId4VCI (#851)
* feat(INJI-245): dowload and view card via issuers

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

* fix(INJI-245): remove vc from wallet

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

* feat(INJI-245): pin card downloaded via eSignet

* refactor(INJI-245): remove debug logs

* refactor(INJI-245): rename vcItem related component to ExistingVcItem

* refactor(INJI-245): add lock file modifications

* refactor(INJI-245): add styles in purple theme for issuer related components

* refactor(INJI-245): update VID for wallet binding usecase and issuer logo display in vc

* refactor(INJI-245): remove duplicate loader component

* refactor(INJI-245): remove unused props in vc details container

---------

Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>
Co-authored-by: Vijay <94220135+vijay151096@users.noreply.github.com>
2023-09-22 17:22:59 +05:30

183 lines
5.8 KiB
TypeScript

import {useMachine, useSelector} from '@xstate/react';
import {useContext, useEffect, useState} from 'react';
import {ActorRefFrom} from 'xstate';
import {useTranslation} from 'react-i18next';
import NetInfo from '@react-native-community/netinfo';
import {ModalProps} from '../../components/ui/Modal';
import {GlobalContext} from '../../shared/GlobalContext';
import {
selectOtpError,
selectIsAcceptingOtpInput,
selectIsAcceptingRevokeInput,
selectIsEditingTag,
selectIsLockingVc,
selectIsRevokingVc,
selectIsLoggingRevoke,
selectVc,
ExistingMosipVCItemEvents,
ExistingMosipVCItemMachine,
selectWalletBindingError,
selectRequestBindingOtp,
selectAcceptingBindingOtp,
selectEmptyWalletBindingId,
selectWalletBindingInProgress,
selectShowWalletBindingError,
selectBindingWarning,
} from '../../machines/VCItemMachine/ExistingMosipVCItem/ExistingMosipVCItemMachine';
import {selectPasscode} from '../../machines/auth';
import {biometricsMachine, selectIsSuccess} from '../../machines/biometrics';
export function useViewVcModal({
vcItemActor,
isVisible,
onRevokeDelete,
}: ViewVcModalProps) {
const {t} = useTranslation('ViewVcModal');
const [toastVisible, setToastVisible] = useState(false);
const [message, setMessage] = useState('');
const [reAuthenticating, setReAuthenticating] = useState('');
const [isRevoking, setRevoking] = useState(false);
const [error, setError] = useState('');
const {appService} = useContext(GlobalContext);
const authService = appService.children.get('auth');
const [, bioSend, bioService] = useMachine(biometricsMachine);
const isSuccessBio = useSelector(bioService, selectIsSuccess);
const isLockingVc = useSelector(vcItemActor, selectIsLockingVc);
const isRevokingVc = useSelector(vcItemActor, selectIsRevokingVc);
const isLoggingRevoke = useSelector(vcItemActor, selectIsLoggingRevoke);
const vc = useSelector(vcItemActor, selectVc);
const otError = useSelector(vcItemActor, selectOtpError);
const onSuccess = () => {
bioSend({type: 'SET_IS_AVAILABLE', data: true});
setError('');
setReAuthenticating('');
vcItemActor.send(ExistingMosipVCItemEvents.LOCK_VC());
};
const onError = (value: string) => {
setError(value);
};
const showToast = (message: string) => {
setToastVisible(true);
setMessage(message);
setTimeout(() => {
setToastVisible(false);
setMessage('');
}, 3000);
};
const netInfoFetch = (otp: string) => {
NetInfo.fetch().then(state => {
if (state.isConnected) {
vcItemActor.send(ExistingMosipVCItemEvents.INPUT_OTP(otp));
} else {
vcItemActor.send(ExistingMosipVCItemEvents.DISMISS());
showToast('Request network failed');
}
});
};
useEffect(() => {
if (isLockingVc) {
showToast(vc.locked ? t('success.locked') : t('success.unlocked'));
}
if (isRevokingVc) {
showToast(t('success.revoked', {vid: vc.id}));
}
if (isLoggingRevoke) {
vcItemActor.send(ExistingMosipVCItemEvents.DISMISS());
onRevokeDelete();
}
if (isSuccessBio && reAuthenticating != '') {
onSuccess();
}
}, [
reAuthenticating,
isLockingVc,
isSuccessBio,
otError,
isRevokingVc,
isLoggingRevoke,
vc,
]);
useEffect(() => {
vcItemActor.send(ExistingMosipVCItemEvents.REFRESH());
}, [isVisible]);
return {
error,
message,
toastVisible,
vc,
otpError: useSelector(vcItemActor, selectOtpError),
reAuthenticating,
isRevoking,
isEditingTag: useSelector(vcItemActor, selectIsEditingTag),
isLockingVc,
isAcceptingOtpInput: useSelector(vcItemActor, selectIsAcceptingOtpInput),
isAcceptingRevokeInput: useSelector(
vcItemActor,
selectIsAcceptingRevokeInput,
),
storedPasscode: useSelector(authService, selectPasscode),
isBindingOtp: useSelector(vcItemActor, selectRequestBindingOtp),
isAcceptingBindingOtp: useSelector(vcItemActor, selectAcceptingBindingOtp),
walletBindingError: useSelector(vcItemActor, selectWalletBindingError),
isWalletBindingPending: useSelector(
vcItemActor,
selectEmptyWalletBindingId,
),
isWalletBindingInProgress: useSelector(
vcItemActor,
selectWalletBindingInProgress,
),
isBindingError: useSelector(vcItemActor, selectShowWalletBindingError),
isBindingWarning: useSelector(vcItemActor, selectBindingWarning),
CONFIRM_REVOKE_VC: () => {
setRevoking(true);
},
REVOKE_VC: () => {
vcItemActor.send(ExistingMosipVCItemEvents.REVOKE_VC());
setRevoking(false);
},
setReAuthenticating,
setRevoking,
onError,
addtoWallet: () => {
vcItemActor.send(ExistingMosipVCItemEvents.ADD_WALLET_BINDING_ID());
},
lockVc: () => {
vcItemActor.send(ExistingMosipVCItemEvents.LOCK_VC());
},
inputOtp: (otp: string) => {
netInfoFetch(otp);
},
revokeVc: (otp: string) => {
netInfoFetch(otp);
},
ADD_WALLET: () =>
vcItemActor.send(ExistingMosipVCItemEvents.ADD_WALLET_BINDING_ID()),
onSuccess,
EDIT_TAG: () => vcItemActor.send(ExistingMosipVCItemEvents.EDIT_TAG()),
SAVE_TAG: (tag: string) =>
vcItemActor.send(ExistingMosipVCItemEvents.SAVE_TAG(tag)),
DISMISS: () => vcItemActor.send(ExistingMosipVCItemEvents.DISMISS()),
LOCK_VC: () => vcItemActor.send(ExistingMosipVCItemEvents.LOCK_VC()),
INPUT_OTP: (otp: string) =>
vcItemActor.send(ExistingMosipVCItemEvents.INPUT_OTP(otp)),
CANCEL: () => vcItemActor.send(ExistingMosipVCItemEvents.CANCEL()),
CONFIRM: () => vcItemActor.send(ExistingMosipVCItemEvents.CONFIRM()),
};
}
export interface ViewVcModalProps extends ModalProps {
vcItemActor: ActorRefFrom<typeof ExistingMosipVCItemMachine>;
onDismiss: () => void;
onRevokeDelete: () => void;
activeTab: Number;
}