rework account selection functions

This commit is contained in:
fmtabbara
2022-05-17 15:43:22 +01:00
parent 1e3c8ed3e0
commit 20828d0d28
2 changed files with 15 additions and 11 deletions
+11 -7
View File
@@ -16,11 +16,11 @@ type TAccounts = {
setAccountMnemonic: Dispatch<SetStateAction<TAccountMnemonic>>;
handleAddAccount: (data: { accountName: string; mnemonic: string; password: string }) => void;
setDialogToDisplay: (dialog?: TAccountsDialog) => void;
handleSelectAccount: (accountId: string) => void;
handleSelectAccount: (data: { accountName: string; password: string }) => Promise<boolean>;
handleAccountToEdit: (accountId: string) => void;
handleEditAccount: (account: AccountEntry) => void;
handleImportAccount: (account: AccountEntry) => void;
handleGetAcccountMnemonic: (data: { password: string; accountName: string }) => void;
handleGetAccountMnemonic: (data: { password: string; accountName: string }) => void;
};
export type TAccountsDialog = 'Accounts' | 'Add' | 'Edit' | 'Import' | 'Mnemonic';
@@ -75,15 +75,19 @@ export const AccountsProvider: React.FC = ({ children }) => {
const handleAccountToEdit = (accountName: string) =>
setAccountToEdit(accounts?.find((acc) => acc.id === accountName));
const handleSelectAccount = async (accountName: string) => {
if (accountName !== selectedAccount?.id) {
await onAccountChange(accountName);
const handleSelectAccount = async ({ accountName, password }: { accountName: string; password: string }) => {
try {
await onAccountChange({ accountId: accountName, password });
const match = accounts?.find((acc) => acc.id === accountName);
setSelectedAccount(match);
return true;
} catch (e) {
setError('Error switching account. Please check your password');
return false;
}
};
const handleGetAcccountMnemonic = async ({ password, accountName }: { password: string; accountName: string }) => {
const handleGetAccountMnemonic = async ({ password, accountName }: { password: string; accountName: string }) => {
try {
setIsLoading(true);
const mnemonic = await showMnemonicForAccount({ password, accountName });
@@ -124,7 +128,7 @@ export const AccountsProvider: React.FC = ({ children }) => {
handleAccountToEdit,
handleSelectAccount,
handleImportAccount,
handleGetAcccountMnemonic,
handleGetAccountMnemonic,
}),
[accounts, selectedAccount, accountToEdit, dialogToDisplay, isLoading, error, accountMnemonic],
)}
+4 -4
View File
@@ -55,7 +55,7 @@ type TAppContext = {
handleShowTerminal: () => void;
signInWithPassword: (password: string) => void;
logOut: () => void;
onAccountChange: (accountId: string) => void;
onAccountChange: ({ accountId, password }: { accountId: string; password: string }) => void;
};
export const AppContext = createContext({} as TAppContext);
@@ -165,15 +165,15 @@ export const AppProvider = ({ children }: { children: React.ReactNode }) => {
enqueueSnackbar('Successfully logged out', { variant: 'success' });
};
const onAccountChange = async (accountId: string) => {
const onAccountChange = async ({ accountId, password }: { accountId: string; password: string }) => {
if (network) {
setIsLoading(true);
try {
await switchAccount(accountId);
await switchAccount({ accountId, password });
await loadAccount(network);
enqueueSnackbar('Account switch success', { variant: 'success', preventDuplicate: true });
} catch (e) {
enqueueSnackbar(`Error swtiching account: ${e}`, { variant: 'error' });
throw new Error(`Error swtiching account: ${e}`);
} finally {
setIsLoading(false);
}