Skip to content

Commit

Permalink
Fix/remove async methods (#1034)
Browse files Browse the repository at this point in the history
* fix(wallet): removing async read methods from the wallet context
  • Loading branch information
VERZUOL1 authored May 5, 2022
1 parent 2bfa733 commit b328761
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 237 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback } from 'react';
import { MyAccountButton } from 'astro_2.0/components/AppHeader/components/AccountDropdown/components/MyAccount';
import { WalletType } from 'types/config';
import { DisconnectButton } from 'astro_2.0/components/AppHeader/components/AccountDropdown/components/DisconnectButton';
Expand All @@ -16,22 +16,12 @@ export const WalletsList: React.FC<WalletsListProps> = ({
}) => {
const {
currentWallet,
getAvailableAccounts,
availableAccounts,
availableWallets,
switchAccount,
switchWallet,
} = useWalletContext();

const [availableAccounts, setAvailableAccounts] = useState<string[]>([]);

useEffect(() => {
if (!getAvailableAccounts) {
return;
}

getAvailableAccounts().then(setAvailableAccounts);
}, [getAvailableAccounts]);

const switchAccountHandler = useCallback(
(account: string) => () => {
switchAccount(WalletType.NEAR, account);
Expand Down
10 changes: 4 additions & 6 deletions astro_2.0/features/Notifications/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type UpdateSettingsConfig = {
export function useNotificationsSettings(): {
updateSettings: (config: UpdateSettingsConfig) => void;
} {
const { accountId, getPublicKeyAndSignature } = useWalletContext();
const { accountId, pkAndSignature } = useWalletContext();

async function getPrevConfig(
accId: string,
Expand Down Expand Up @@ -65,13 +65,11 @@ export function useNotificationsSettings(): {
const updateSettings = useCallback(
async (config: UpdateSettingsConfig) => {
try {
const result = await getPublicKeyAndSignature();

if (!result) {
if (!pkAndSignature) {
return;
}

const { publicKey, signature } = result;
const { publicKey, signature } = pkAndSignature;
const prevConfig = await getPrevConfig(accountId, config.daoId);

if (publicKey && signature) {
Expand All @@ -97,7 +95,7 @@ export function useNotificationsSettings(): {
});
}
},
[accountId, getPublicKeyAndSignature]
[accountId, pkAndSignature]
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const WalletIdCard: VFC<WalletIdCardProps> = props => {
isPhoneVerified,
} = contactsConfig;

const { accountId, getPublicKeyAndSignature } = useWalletContext();
const { accountId, pkAndSignature } = useWalletContext();

const { t } = useTranslation('common');

Expand All @@ -43,10 +43,10 @@ export const WalletIdCard: VFC<WalletIdCardProps> = props => {
isEmail,
setConfig,
accountId,
getPublicKeyAndSignature,
pkAndSignature,
});
},
[setConfig, showModal, accountId, getPublicKeyAndSignature]
[setConfig, showModal, accountId, pkAndSignature]
);

const openAddEmailModal = useCallback(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { yupResolver } from '@hookform/resolvers/yup';
import React, { VFC, useState, useCallback } from 'react';
import { useForm, FormProvider, FieldError } from 'react-hook-form';

import { PkAndSignature } from 'context/WalletContext';
import { UserContacts } from 'services/NotificationsService/types';

import { NotificationsService } from 'services/NotificationsService';
Expand All @@ -14,6 +13,7 @@ import { Input } from 'components/inputs/Input';
import { InputFormWrapper } from 'components/inputs/InputFormWrapper';
import { UsaOnly } from 'astro_2.0/features/pages/myAccount/cards/WalletIdCard/components/UsaOnly';

import { PkAndSignature } from 'context/WalletContextHooks';
import { ContactForm } from './types';

import { useValidationSchema } from './useValidationSchema';
Expand All @@ -30,7 +30,7 @@ export interface AddUserInfoModalProps {
isEmail: boolean;
accountId: string;
setConfig: (config: UserContacts) => void;
getPublicKeyAndSignature: () => Promise<PkAndSignature | null>;
pkAndSignature: PkAndSignature | null;
}

const ONE_MINUTE_IN_MS = 60000;
Expand All @@ -43,7 +43,7 @@ export const AddUserInfoModal: VFC<AddUserInfoModalProps> = props => {
isEmail,
setConfig,
accountId,
getPublicKeyAndSignature,
pkAndSignature,
} = props;

const tBase = 'myAccountPage.popup';
Expand Down Expand Up @@ -73,11 +73,11 @@ export const AddUserInfoModal: VFC<AddUserInfoModalProps> = props => {
NotificationsService.sendContact(
accountId,
contact,
getPublicKeyAndSignature,
pkAndSignature,
isEmail
);
},
[start, accountId, isEmail, getPublicKeyAndSignature]
[start, accountId, isEmail, pkAndSignature]
);

const verifyContact = useCallback(
Expand All @@ -87,7 +87,7 @@ export const AddUserInfoModal: VFC<AddUserInfoModalProps> = props => {
const successful = await NotificationsService.verifyContact(
accountId,
code,
getPublicKeyAndSignature,
pkAndSignature,
isEmail
);

Expand All @@ -103,7 +103,7 @@ export const AddUserInfoModal: VFC<AddUserInfoModalProps> = props => {
setCodeValid(false);
}
},
[onClose, isEmail, setConfig, accountId, getPublicKeyAndSignature]
[onClose, isEmail, setConfig, accountId, pkAndSignature]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('AddUserInfoModal', () => {
onClose={() => 0}
accountId={accountId}
setConfig={() => 0}
getPublicKeyAndSignature={() => Promise.resolve({})}
pkAndSignature={null}
{...props}
/>
);
Expand Down Expand Up @@ -74,12 +74,7 @@ describe('AddUserInfoModal', () => {

await new Promise(resolve => setTimeout(resolve, 0));

expect(sendContact).toBeCalledWith(
accountId,
email,
expect.anything(),
true
);
expect(sendContact).toBeCalledWith(accountId, email, null, true);
});

it('Should verify code', async () => {
Expand Down Expand Up @@ -108,11 +103,6 @@ describe('AddUserInfoModal', () => {

await new Promise(resolve => setTimeout(resolve, 0));

expect(verifyContact).toBeCalledWith(
accountId,
code,
expect.anything(),
true
);
expect(verifyContact).toBeCalledWith(accountId, code, null, true);
});
});
Loading

0 comments on commit b328761

Please sign in to comment.