From 6685b129bbdfc4c625cd26e14007d913d948b552 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Wed, 4 May 2022 16:21:29 +0100 Subject: [PATCH] refactor to use accounts context --- .../src/components/Accounts/AccountItem.tsx | 80 +++++++------- .../src/components/Accounts/Accounts.tsx | 85 +++------------ .../src/components/Accounts/AccountsModal.tsx | 102 ++++++++---------- .../components/Accounts/AddAccountModal.tsx | 39 +++---- .../components/Accounts/EditAccountModal.tsx | 33 +++--- .../Accounts/ImportAccountModal.tsx | 28 +++-- nym-wallet/src/context/accounts.tsx | 4 +- nym-wallet/src/context/index.tsx | 1 + .../pages/sign-in/pages/signin-mnemonic.tsx | 2 +- 9 files changed, 143 insertions(+), 231 deletions(-) diff --git a/nym-wallet/src/components/Accounts/AccountItem.tsx b/nym-wallet/src/components/Accounts/AccountItem.tsx index a1fdb2f1cd..940da46285 100644 --- a/nym-wallet/src/components/Accounts/AccountItem.tsx +++ b/nym-wallet/src/components/Accounts/AccountItem.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { Box, IconButton, @@ -10,48 +10,44 @@ import { Typography, } from '@mui/material'; import { Edit } from '@mui/icons-material'; +import { AccountsContext } from 'src/context'; import { AccountAvatar } from './AccountAvatar'; import { ShowMnemonic } from './ShowMnemonic'; -export const AccountItem = ({ - name, - address, - isSelected, - onSelect, - onEdit, -}: { - name: string; - address: string; - isSelected: boolean; - onSelect: () => void; - onEdit: () => void; -}) => ( - - - - - - - {address} - - +export const AccountItem = ({ name, address }: { name: string; address: string }) => { + const { selectedAccount, handleSelectAccount, handleAccountToEdit } = useContext(AccountsContext); + return ( + + handleSelectAccount(name)}> + + + + + {address} + + + - - } - /> - - { - e.stopPropagation(); - onEdit(); - }} - > - - - - - -); + } + /> + + { + e.stopPropagation(); + handleAccountToEdit(name); + }} + > + + + + + + ); +}; diff --git a/nym-wallet/src/components/Accounts/Accounts.tsx b/nym-wallet/src/components/Accounts/Accounts.tsx index 04d27f0dfb..2dd50102d4 100644 --- a/nym-wallet/src/components/Accounts/Accounts.tsx +++ b/nym-wallet/src/components/Accounts/Accounts.tsx @@ -1,90 +1,29 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { Button } from '@mui/material'; -import { v4 as uuidv4 } from 'uuid'; -import { AccountEntry } from 'src/types'; +import { AccountsContext } from 'src/context'; import { EditAccountModal } from './EditAccountModal'; import { AddAccountModal } from './AddAccountModal'; import { AccountsModal } from './AccountsModal'; import { ImportAccountModal } from './ImportAccountModal'; import { AccountAvatar } from './AccountAvatar'; -import { TDialog } from './types'; -export const Accounts = ({ - accounts, - selectedAccount, - accountToEdit, - dialogToDisplay, - addAccount, - editAccount, - setAccountToEdit, - importAccount, - setSelectedAccount, - setDialogToDisplay, -}: { - accounts?: AccountEntry[]; - selectedAccount?: AccountEntry; - accountToEdit?: AccountEntry; - dialogToDisplay?: TDialog; - addAccount: (acc: { accountName: string; mnemonic: string; password: string }) => Promise; - editAccount: (acc: AccountEntry) => void; - importAccount: (acc: AccountEntry & { mnemonic: string }) => void; - setAccountToEdit: (accountName: string) => void; - setSelectedAccount: (accountName: string) => void; - setDialogToDisplay: (dialog: TDialog | undefined) => void; -}) => - accounts && selectedAccount ? ( +export const Accounts = () => { + const { accounts, selectedAccount, setDialogToDisplay } = useContext(AccountsContext); + + return accounts && selectedAccount ? ( <> - setDialogToDisplay(undefined)} - accounts={accounts} - onAccountSelect={(accountName) => setSelectedAccount(accountName)} - selectedAccount={selectedAccount.id} - onAdd={() => { - setDialogToDisplay('Add'); - }} - onEdit={(accountName) => { - setAccountToEdit(accountName); - setDialogToDisplay('Edit'); - }} - onImport={() => setDialogToDisplay('Import')} - /> - { - setDialogToDisplay('Accounts'); - }} - onAdd={async (data) => { - addAccount(data); - setDialogToDisplay('Accounts'); - }} - /> - { - setDialogToDisplay('Accounts'); - }} - onEdit={(account) => { - editAccount(account); - setDialogToDisplay('Accounts'); - }} - /> - setDialogToDisplay('Accounts')} - onImport={(mnemonic) => { - importAccount({ id: 'New Account', address: uuidv4(), mnemonic }); - setDialogToDisplay('Accounts'); - }} - /> + + + + ) : null; +}; diff --git a/nym-wallet/src/components/Accounts/AccountsModal.tsx b/nym-wallet/src/components/Accounts/AccountsModal.tsx index bc2de38243..8210b3c562 100644 --- a/nym-wallet/src/components/Accounts/AccountsModal.tsx +++ b/nym-wallet/src/components/Accounts/AccountsModal.tsx @@ -1,62 +1,48 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { Box, Button, Dialog, DialogActions, DialogContent, DialogTitle, IconButton, Typography } from '@mui/material'; import { Add, ArrowDownwardSharp, Close } from '@mui/icons-material'; -import { AccountEntry } from 'src/types'; +import { AccountsContext } from 'src/context'; import { AccountItem } from './AccountItem'; -export const AccountsModal = ({ - show, - accounts, - selectedAccount, - onClose, - onAccountSelect, - onAdd, - onEdit, - onImport, -}: { - show: boolean; - accounts: AccountEntry[]; - selectedAccount: AccountEntry['id']; - onClose: () => void; - onAccountSelect: (accountName: string) => void; - onAdd: () => void; - onEdit: (accoutnName: string) => void; - onImport: () => void; -}) => ( - - - - Accounts - - - - - - Switch between accounts - - - - {accounts.map(({ id, address }) => ( - { - onAccountSelect(id); - onClose(); - }} - onEdit={() => onEdit(id)} - isSelected={selectedAccount === id} - key={address} - /> - ))} - - - - - - -); +export const AccountsModal = ({ onClose }: { onClose?: () => void }) => { + const { accounts, dialogToDisplay, setDialogToDisplay } = useContext(AccountsContext); + + const handleClose = () => { + setDialogToDisplay(undefined); + onClose?.(); + }; + + return ( + + + + Accounts + + + + + + Switch between accounts + + + + {accounts?.map(({ id, address }) => ( + + ))} + + + + + + + ); +}; diff --git a/nym-wallet/src/components/Accounts/AddAccountModal.tsx b/nym-wallet/src/components/Accounts/AddAccountModal.tsx index 6a45c6213f..4745dd4590 100644 --- a/nym-wallet/src/components/Accounts/AddAccountModal.tsx +++ b/nym-wallet/src/components/Accounts/AddAccountModal.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import { Alert, Box, @@ -15,6 +15,7 @@ import { import { Check, Close, ContentCopySharp } from '@mui/icons-material'; import { useClipboard } from 'use-clipboard-copy'; import { createMnemonic } from 'src/requests'; +import { AccountsContext } from 'src/context'; const createAccountSteps = ['Save and copy mnemonic for your new account', 'Name your new account', 'Confirm password']; @@ -135,42 +136,36 @@ const ConfirmPassword = ({ onConfirm }: { onConfirm: (password: string) => void ); }; -export const AddAccountModal = ({ - show, - withoutPassword, - onClose, - onAdd, -}: { - show: boolean; - withoutPassword?: boolean; - onClose: () => void; - onAdd: (data: { accountName: string; mnemonic: string; password: string }) => void; -}) => { +export const AddAccountModal = ({ withoutPassword }: { withoutPassword?: boolean }) => { const [step, setStep] = useState(0); const [data, setData] = useState<{ mnemonic?: string; accountName?: string }>({ mnemonic: undefined, accountName: undefined, }); + const { dialogToDisplay, setDialogToDisplay, handleAddAccount } = useContext(AccountsContext); + const generateMnemonic = async () => { const mnemon = await createMnemonic(); setData((d) => ({ ...d, mnemonic: mnemon })); }; + const handleClose = () => { + setDialogToDisplay('Accounts'); + setData({ mnemonic: undefined, accountName: undefined }); + setStep(0); + }; + useEffect(() => { - if (show) generateMnemonic(); - else { - setData({ mnemonic: undefined, accountName: undefined }); - setStep(0); - } - }, [show]); + if (dialogToDisplay === 'Accounts') generateMnemonic(); + }, [dialogToDisplay]); return ( - + Add new account - + @@ -181,7 +176,7 @@ export const AddAccountModal = ({ )} {createAccountSteps[step]} - {withoutPassword && } + {withoutPassword && } {!withoutPassword && data.mnemonic && (() => { @@ -202,7 +197,7 @@ export const AddAccountModal = ({ { if (data.accountName && data.mnemonic) { - onAdd({ accountName: data.accountName, mnemonic: data.mnemonic, password }); + handleAddAccount({ accountName: data.accountName, mnemonic: data.mnemonic, password }); } }} /> diff --git a/nym-wallet/src/components/Accounts/EditAccountModal.tsx b/nym-wallet/src/components/Accounts/EditAccountModal.tsx index 4ab69ea75f..c5fec1d28b 100644 --- a/nym-wallet/src/components/Accounts/EditAccountModal.tsx +++ b/nym-wallet/src/components/Accounts/EditAccountModal.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useContext, useEffect, useState } from 'react'; import { Box, Button, @@ -11,31 +11,23 @@ import { Typography, } from '@mui/material'; import { Close } from '@mui/icons-material'; -import { AccountEntry } from 'src/types'; +import { AccountsContext } from 'src/context'; -export const EditAccountModal = ({ - account, - show, - onClose, - onEdit, -}: { - account?: AccountEntry; - show: boolean; - onClose: () => void; - onEdit: (account: AccountEntry) => void; -}) => { +export const EditAccountModal = () => { const [accountName, setAccountName] = useState(''); + const { accountToEdit, dialogToDisplay, setDialogToDisplay, handleEditAccount } = useContext(AccountsContext); + useEffect(() => { - setAccountName(account ? account?.id : ''); - }, [account]); + setAccountName(accountToEdit ? accountToEdit?.id : ''); + }, [accountToEdit]); return ( - + setDialogToDisplay('Accounts')} fullWidth hideBackdrop> Edit account name - + setDialogToDisplay('Accounts')}> @@ -60,7 +52,12 @@ export const EditAccountModal = ({ disableElevation variant="contained" size="large" - onClick={() => account && onEdit({ ...account, id: accountName })} + onClick={() => { + if (accountToEdit) { + handleEditAccount({ ...accountToEdit, id: accountName }); + setDialogToDisplay('Accounts'); + } + }} disabled={!accountName?.length} > Edit diff --git a/nym-wallet/src/components/Accounts/ImportAccountModal.tsx b/nym-wallet/src/components/Accounts/ImportAccountModal.tsx index 8222f330bf..2e61066f71 100644 --- a/nym-wallet/src/components/Accounts/ImportAccountModal.tsx +++ b/nym-wallet/src/components/Accounts/ImportAccountModal.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useContext, useState } from 'react'; import { Box, Button, @@ -11,28 +11,24 @@ import { Typography, } from '@mui/material'; import { Close } from '@mui/icons-material'; +import { AccountsContext } from 'src/context'; -export const ImportAccountModal = ({ - show, - onClose, - onImport, -}: { - show: boolean; - onClose: () => void; - onImport: (mnemonic: string) => void; -}) => { +export const ImportAccountModal = () => { const [mnemonic, setMnemonic] = useState(''); - useEffect(() => { - if (!show) setMnemonic(''); - }, [show]); + const { dialogToDisplay, setDialogToDisplay, handleImportAccount } = useContext(AccountsContext); + + const handleClose = () => { + setMnemonic(''); + setDialogToDisplay('Accounts'); + }; return ( - + Import account - + @@ -59,7 +55,7 @@ export const ImportAccountModal = ({ disableElevation variant="contained" size="large" - onClick={() => onImport(mnemonic)} + onClick={() => handleImportAccount({ id: '', address: '' })} disabled={!mnemonic.length} > Import account diff --git a/nym-wallet/src/context/accounts.tsx b/nym-wallet/src/context/accounts.tsx index a97b3ae0a4..ef3bec09d5 100644 --- a/nym-wallet/src/context/accounts.tsx +++ b/nym-wallet/src/context/accounts.tsx @@ -1,5 +1,5 @@ import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; -import { Account, AccountEntry, TAccountsDialog } from 'src/types'; +import { AccountEntry } from 'src/types'; import { addAccount as addAccountRequest } from 'src/requests'; import { ClientContext } from './main'; @@ -16,6 +16,8 @@ type TAccounts = { handleImportAccount: (account: AccountEntry) => void; }; +export type TAccountsDialog = 'Accounts' | 'Add' | 'Edit' | 'Import'; + export const AccountsContext = createContext({} as TAccounts); export const AccountsProvider: React.FC = ({ children }) => { diff --git a/nym-wallet/src/context/index.tsx b/nym-wallet/src/context/index.tsx index fe68268d78..e6b2ab0a8e 100644 --- a/nym-wallet/src/context/index.tsx +++ b/nym-wallet/src/context/index.tsx @@ -1,2 +1,3 @@ export * from './main'; export * from './sign-in'; +export * from './accounts'; diff --git a/nym-wallet/src/pages/sign-in/pages/signin-mnemonic.tsx b/nym-wallet/src/pages/sign-in/pages/signin-mnemonic.tsx index 1b81179917..51e4377bb4 100644 --- a/nym-wallet/src/pages/sign-in/pages/signin-mnemonic.tsx +++ b/nym-wallet/src/pages/sign-in/pages/signin-mnemonic.tsx @@ -1,9 +1,9 @@ import React, { useContext, useState, useEffect } from 'react'; import { useHistory } from 'react-router-dom'; import { Box, Button, FormControl, Stack } from '@mui/material'; +import { ClientContext } from 'src/context'; import { isPasswordCreated } from 'src/requests'; import { MnemonicInput, Subtitle } from '../components'; -import { ClientContext } from 'src/context'; export const SignInMnemonic = () => { const [mnemonic, setMnemonic] = useState('');