Skip to content

Commit

Permalink
Merge pull request #175 from docknetwork/feat/better-events-for-rn-wa…
Browse files Browse the repository at this point in the history
…llet
  • Loading branch information
maycon-mello authored Nov 2, 2023
2 parents cc8137e + 0440d31 commit 15518ad
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
64 changes: 37 additions & 27 deletions packages/react-native/lib/credentials/credentialHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async function getCredentialValidityStatus(credential) {
}

export function useCredentialUtils() {
const { documents, wallet } = useWallet();
const {documents, wallet} = useWallet();

const credentials = useMemo(() => {
if (Array.isArray(documents)) {
Expand All @@ -83,31 +83,39 @@ export function useCredentialUtils() {
return [];
}, [documents]);

const doesCredentialExist = (allCredentials, credentialToAdd) => {
const doesCredentialExist = useCallback((allCredentials, credentialToAdd) => {
return !!allCredentials.find(item => item.id === credentialToAdd.id);
};
}, []);

const saveCredential = async jsonData => {
validateCredential(jsonData);
if (doesCredentialExist(credentials, jsonData)) {
throw new Error('This credential already exists in the wallet');
}
await wallet.addDocument(jsonData);
};
const deleteCredential = async credentialId => {
assert(
typeof credentialId === 'string' && credentialId.length > 0,
'Credential ID is not set',
);
return await wallet.remove(credentialId);
};
const saveCredential = useCallback(
async jsonData => {
validateCredential(jsonData);
if (doesCredentialExist(credentials, jsonData)) {
throw new Error('This credential already exists in the wallet');
}
await wallet.addDocument(jsonData);
},
[credentials, doesCredentialExist, wallet],
);
const deleteCredential = useCallback(
async credentialId => {
assert(
typeof credentialId === 'string' && credentialId.length > 0,
'Credential ID is not set',
);
return await wallet.remove(credentialId);
},
[wallet],
);

return {
credentials,
doesCredentialExist,
saveCredential,
deleteCredential,
};
return useMemo(() => {
return {
credentials,
doesCredentialExist,
saveCredential,
deleteCredential,
};
}, [credentials, doesCredentialExist, saveCredential, deleteCredential]);
}
export function isInThePast(date) {
const today = new Date();
Expand All @@ -122,7 +130,7 @@ export async function getCredentialStatus(credential) {
return buildStatusResponse(CREDENTIAL_STATUS.EXPIRED);
}

const { verified, error } = await getCredentialValidityStatus(credential);
const {verified, error} = await getCredentialValidityStatus(credential);

if (isCredentialRevoked(error)) {
return buildStatusResponse(CREDENTIAL_STATUS.REVOKED, error);
Expand All @@ -142,7 +150,9 @@ function isCredentialExpired(credential) {
}

function isCredentialRevoked(error) {
return typeof error === 'string' && error.toLowerCase().includes('revocation');
return (
typeof error === 'string' && error.toLowerCase().includes('revocation')
);
}

function buildStatusResponse(status, error = null) {
Expand All @@ -152,12 +162,12 @@ function buildStatusResponse(status, error = null) {
};
}

//TODO: Implement a caching mechanism that is attached to credentials context instead of global
export let cachedCredentialStatus = {};

export function useGetCredentialStatus({credential}) {
const [status, setStatus] = useState(
cachedCredentialStatus[credential.id] || buildStatusResponse(CREDENTIAL_STATUS.PENDING),
cachedCredentialStatus[credential.id] ||
buildStatusResponse(CREDENTIAL_STATUS.PENDING),
);

useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions packages/react-native/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function _useWalletController() {
wallet.getAllDocuments().then(setDocuments);
}, [documents, wallet, firstFetch]);

const refetch = async ({fetchBalances} = {fetchBalances: true}) => {
const refetch = useCallback(async ({fetchBalances} = {fetchBalances: true}) => {
try {
const allDocs = await wallet.query({});
if (fetchBalances) {
Expand All @@ -147,7 +147,7 @@ export function _useWalletController() {
} catch (err) {
console.error(err);
}
}
}, [wallet, setDocuments]);

useEffect(() => {
if (!wallet) {
Expand Down Expand Up @@ -200,12 +200,12 @@ export function WalletSDKProvider({onError, customUri, children, onReady}) {

const {createWallet} = controller;

const handleReady = async () => {
const handleReady = useCallback(async () => {
await createWallet();
if (onReady) {
onReady();
}
};
}, [onReady, createWallet]);

const eventHandler: WebviewEventHandler = useMemo(
() =>
Expand Down

0 comments on commit 15518ad

Please sign in to comment.