Skip to content

Commit

Permalink
Refactor ContactAccountForm props
Browse files Browse the repository at this point in the history
  • Loading branch information
buberdds committed Sep 11, 2023
1 parent ca31d38 commit 2d08132
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
8 changes: 7 additions & 1 deletion src/app/components/Toolbar/Features/Contacts/AddContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ export const AddContact = ({ setLayerVisibility }: AddContactProps) => {
height={{ min: isMobile ? 'auto' : layerOverlayMinHeight }}

Check warning on line 40 in src/app/components/Toolbar/Features/Contacts/AddContact.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/AddContact.tsx#L40

Added line #L40 was not covered by tests
pad={{ vertical: 'medium' }}
>
<ContactAccountForm setLayerVisibility={setLayerVisibility} submitHandler={submitHandler} />
<ContactAccountForm
onCancel={() => setLayerVisibility(false)}
onSave={contract => {
submitHandler(contract)
setLayerVisibility(false)

Check warning on line 47 in src/app/components/Toolbar/Features/Contacts/AddContact.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/AddContact.tsx#L44-L47

Added lines #L44 - L47 were not covered by tests
}}
/>
</Box>
</Tab>
</Tabs>
Expand Down
12 changes: 9 additions & 3 deletions src/app/components/Toolbar/Features/Contacts/ContactAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ export const ContactAccount = ({ contact }: ContactAccountProps) => {
>
<ContactAccountForm
contact={contact}
deleteHandler={deleteHandler}
setLayerVisibility={setLayerVisibility}
submitHandler={submitHandler}
onDelete={address => {
deleteHandler(address)
setLayerVisibility(false)

Check warning on line 64 in src/app/components/Toolbar/Features/Contacts/ContactAccount.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccount.tsx#L62-L64

Added lines #L62 - L64 were not covered by tests
}}
onCancel={() => setLayerVisibility(false)}
onSave={contract => {
submitHandler(contract)
setLayerVisibility(false)

Check warning on line 69 in src/app/components/Toolbar/Features/Contacts/ContactAccount.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccount.tsx#L66-L69

Added lines #L66 - L69 were not covered by tests
}}
/>
</Box>
</Tab>
Expand Down
29 changes: 11 additions & 18 deletions src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@ import { TextInput } from 'grommet/es6/components/TextInput'
import { TextArea } from 'grommet/es6/components/TextArea'
import { selectContactsList } from 'app/state/contacts/selectors'
import { isValidAddress } from 'app/lib/helpers'
import { contactsActions } from 'app/state/contacts'
import { Contact } from 'app/state/contacts/types'
import { DeleteContact } from './DeleteContact'

interface ContactAccountFormProps {
contact?: Contact
deleteHandler?: (address: string) => ReturnType<typeof contactsActions.delete>
setLayerVisibility: (isVisible: boolean) => void
submitHandler: (contact: Contact) => ReturnType<typeof contactsActions.add | typeof contactsActions.update>
onDelete?: (address: string) => void
onCancel: () => void
onSave: (contact: Contact) => void
}

interface FormValue {
address: string
name: string
}

export const ContactAccountForm = ({
contact,
deleteHandler,
setLayerVisibility,
submitHandler,
}: ContactAccountFormProps) => {
export const ContactAccountForm = ({ contact, onDelete, onCancel, onSave }: ContactAccountFormProps) => {
const { t } = useTranslation()
const [deleteLayerVisibility, setDeleteLayerVisibility] = useState(false)
const [value, setValue] = useState({ name: contact?.name || '', address: contact?.address || '' })
Expand All @@ -41,10 +35,9 @@ export const ContactAccountForm = ({
style={{ display: 'flex', flex: 1, flexDirection: 'column' }}
messages={{ required: t('toolbar.contacts.validation.required', 'Field is required') }}
onChange={nextValue => setValue(nextValue)}

Check warning on line 37 in src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx#L37

Added line #L37 was not covered by tests
onSubmit={({ value }) => {
submitHandler({ address: value.address.replaceAll(' ', ''), name: value.name.trim() })
setLayerVisibility(false)
}}
onSubmit={({ value }) =>
onSave({ address: value.address.replaceAll(' ', ''), name: value.name.trim() })

Check warning on line 39 in src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx#L39

Added line #L39 was not covered by tests
}
value={value}
>
<Box flex="grow">
Expand Down Expand Up @@ -90,7 +83,7 @@ export const ContactAccountForm = ({
placeholder={t('toolbar.contacts.address', 'Address')}
/>
</FormField>
{contact && deleteHandler && (
{contact && onDelete && (
<Box align="end">

Check warning on line 87 in src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx#L86-L87

Added lines #L86 - L87 were not covered by tests
<Button
style={{ fontSize: '14px', fontWeight: 600 }}
Expand All @@ -102,15 +95,15 @@ export const ContactAccountForm = ({
></Button>
{deleteLayerVisibility && (
<DeleteContact
deleteHandler={() => deleteHandler(contact.address)}
setLayerVisibility={setDeleteLayerVisibility}
onDelete={() => onDelete(contact.address)}
onCancel={() => setDeleteLayerVisibility(false)}

Check warning on line 99 in src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/ContactAccountForm.tsx#L96-L99

Added lines #L96 - L99 were not covered by tests
/>
)}
</Box>
)}
</Box>
<Box direction="row" align="center" justify="between" pad={{ top: 'medium' }}>
<Button label={t('toolbar.contacts.cancel', 'Cancel')} onClick={() => setLayerVisibility(false)} />
<Button label={t('toolbar.contacts.cancel', 'Cancel')} onClick={onCancel} />
<Button type="submit" label={t('toolbar.contacts.save', 'Save')} primary />
</Box>
</Form>
Expand Down
14 changes: 7 additions & 7 deletions src/app/components/Toolbar/Features/Contacts/DeleteContact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import { Text } from 'grommet/es6/components/Text'
import { ResponsiveLayer } from '../../../ResponsiveLayer'

interface DeleteContactProps {
deleteHandler: () => void
setLayerVisibility: (isVisible: boolean) => void
onDelete: () => void
onCancel: () => void
}

export const DeleteContact = ({ deleteHandler, setLayerVisibility }: DeleteContactProps) => {
export const DeleteContact = ({ onCancel, onDelete }: DeleteContactProps) => {
const { t } = useTranslation()
const isMobile = useContext(ResponsiveContext) === 'small'

Check warning on line 16 in src/app/components/Toolbar/Features/Contacts/DeleteContact.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/DeleteContact.tsx#L15-L16

Added lines #L15 - L16 were not covered by tests

return (

Check warning on line 18 in src/app/components/Toolbar/Features/Contacts/DeleteContact.tsx

View check run for this annotation

Codecov / codecov/patch

src/app/components/Toolbar/Features/Contacts/DeleteContact.tsx#L18

Added line #L18 was not covered by tests
<ResponsiveLayer
onClickOutside={() => setLayerVisibility(false)}
onEsc={() => setLayerVisibility(false)}
onClickOutside={onCancel}
onEsc={onCancel}
animation="none"
background="background-front"
modal
Expand All @@ -34,9 +34,9 @@ export const DeleteContact = ({ deleteHandler, setLayerVisibility }: DeleteConta
</Text>
</Box>
<Box direction="row" align="center" justify="between" pad={{ top: 'medium' }}>
<Button label={t('toolbar.contacts.cancel', 'Cancel')} onClick={() => setLayerVisibility(false)} />
<Button label={t('toolbar.contacts.cancel', 'Cancel')} onClick={onCancel} />
<Button
onClick={deleteHandler}
onClick={onDelete}
label={t('toolbar.contacts.delete.confirm', 'Yes, delete')}
color="status-error"
primary
Expand Down

0 comments on commit 2d08132

Please sign in to comment.