Refactor owner management components

This commit is contained in:
Germán Martínez 2019-07-12 16:59:24 +02:00
parent d8bc944fe9
commit 9564f568c1
7 changed files with 58 additions and 86 deletions

View File

@ -4,9 +4,8 @@ import { List } from 'immutable'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import { SharedSnackbarConsumer } from '~/components/SharedSnackBar' import { SharedSnackbarConsumer } from '~/components/SharedSnackBar'
import Modal from '~/components/Modal' import Modal from '~/components/Modal'
import { type Owner, makeOwner } from '~/routes/safe/store/models/owner' import { type Owner } from '~/routes/safe/store/models/owner'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { getOwners } from '~/logic/safe/utils'
import OwnerForm from './screens/OwnerForm' import OwnerForm from './screens/OwnerForm'
import ThresholdForm from './screens/ThresholdForm' import ThresholdForm from './screens/ThresholdForm'
import ReviewAddOwner from './screens/Review' import ReviewAddOwner from './screens/Review'
@ -28,7 +27,7 @@ type Props = {
owners: List<Owner>, owners: List<Owner>,
threshold: number, threshold: number,
network: string, network: string,
updateSafe: Function, addSafeOwner: Function,
createTransaction: Function, createTransaction: Function,
} }
type ActiveScreen = 'selectOwner' | 'selectThreshold' | 'reviewAddOwner' type ActiveScreen = 'selectOwner' | 'selectThreshold' | 'reviewAddOwner'
@ -39,29 +38,15 @@ export const sendAddOwner = async (
ownersOld: List<Owner>, ownersOld: List<Owner>,
openSnackbar: Function, openSnackbar: Function,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, addSafeOwner: Function,
) => { ) => {
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const txData = gnosisSafe.contract.methods.addOwnerWithThreshold(values.ownerAddress, values.threshold).encodeABI() const txData = gnosisSafe.contract.methods.addOwnerWithThreshold(values.ownerAddress, values.threshold).encodeABI()
const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar) const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar)
const owners = []
const storedOwners = await getOwners(safeAddress)
storedOwners.forEach((value, key) => owners.push(makeOwner({
address: key,
name: (values.ownerAddress.toLowerCase() === key.toLowerCase()) ? values.ownerName : value,
})))
const newOwnerIndex = List(owners).findIndex(o => o.address.toLowerCase() === values.ownerAddress.toLowerCase())
if (newOwnerIndex < 0) {
owners.push(makeOwner({ address: values.ownerAddress, name: values.ownerName }))
}
if (txHash) { if (txHash) {
updateSafe({ addSafeOwner({ safeAddress, ownerName: values.ownerName, ownerAddress: values.ownerAddress })
address: safeAddress,
owners: List(owners),
})
} }
} }
@ -75,7 +60,7 @@ const AddOwner = ({
threshold, threshold,
network, network,
createTransaction, createTransaction,
updateSafe, addSafeOwner,
}: Props) => { }: Props) => {
const [activeScreen, setActiveScreen] = useState<ActiveScreen>('selectOwner') const [activeScreen, setActiveScreen] = useState<ActiveScreen>('selectOwner')
const [values, setValues] = useState<Object>({}) const [values, setValues] = useState<Object>({})
@ -120,7 +105,7 @@ const AddOwner = ({
const onAddOwner = async () => { const onAddOwner = async () => {
onClose() onClose()
try { try {
sendAddOwner(values, safeAddress, owners, openSnackbar, createTransaction, updateSafe) sendAddOwner(values, safeAddress, owners, openSnackbar, createTransaction, addSafeOwner)
} catch (error) { } catch (error) {
// eslint-disable-next-line // eslint-disable-next-line
console.log('Error while removing an owner ' + error) console.log('Error while removing an owner ' + error)

View File

@ -1,6 +1,5 @@
// @flow // @flow
import React from 'react' import React from 'react'
import { List } from 'immutable'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import Close from '@material-ui/icons/Close' import Close from '@material-ui/icons/Close'
import OpenInNew from '@material-ui/icons/OpenInNew' import OpenInNew from '@material-ui/icons/OpenInNew'
@ -16,7 +15,6 @@ import TextField from '~/components/forms/TextField'
import Paragraph from '~/components/layout/Paragraph' import Paragraph from '~/components/layout/Paragraph'
import Identicon from '~/components/Identicon' import Identicon from '~/components/Identicon'
import { getEtherScanLink } from '~/logic/wallets/getWeb3' import { getEtherScanLink } from '~/logic/wallets/getWeb3'
import type { Owner } from '~/routes/safe/store/models/owner'
import { composeValidators, required, minMaxLength } from '~/components/forms/validator' import { composeValidators, required, minMaxLength } from '~/components/forms/validator'
import Modal from '~/components/Modal' import Modal from '~/components/Modal'
import { styles } from './style' import { styles } from './style'
@ -30,23 +28,15 @@ const openIconStyle = {
color: secondary, color: secondary,
} }
const stylesModal = () => ({
smallerModalWindow: {
height: 'auto',
position: 'static',
},
})
type Props = { type Props = {
onClose: () => void, onClose: () => void,
classes: Object, classes: Object,
isOpen: boolean, isOpen: boolean,
safeAddress: string, safeAddress: string,
ownerAddress: string, ownerAddress: string,
owners: List<Owner>,
network: string, network: string,
selectedOwnerName: string, selectedOwnerName: string,
updateSafe: Function, editSafeOwner: Function,
} }
const EditOwnerComponent = ({ const EditOwnerComponent = ({
@ -56,15 +46,11 @@ const EditOwnerComponent = ({
safeAddress, safeAddress,
ownerAddress, ownerAddress,
selectedOwnerName, selectedOwnerName,
updateSafe, editSafeOwner,
owners,
network, network,
}: Props) => { }: Props) => {
const handleSubmit = (values) => { const handleSubmit = (values) => {
const ownerToUpdateIndex = owners.findIndex(o => o.address === ownerAddress) editSafeOwner({ safeAddress, ownerAddress, ownerName: values.ownerName })
const updatedOwners = owners.update(ownerToUpdateIndex, owner => owner.set('name', values.ownerName))
updateSafe({ address: safeAddress, owners: updatedOwners })
onClose() onClose()
} }
@ -74,7 +60,7 @@ const EditOwnerComponent = ({
description="Edit owner from Safe" description="Edit owner from Safe"
handleClose={onClose} handleClose={onClose}
open={isOpen} open={isOpen}
paperClassName={stylesModal.smallerModalWindow} paperClassName={classes.smallerModalWindow}
> >
<Row align="center" grow className={classes.heading}> <Row align="center" grow className={classes.heading}>
<Paragraph className={classes.manage} noMargin weight="bolder"> <Paragraph className={classes.manage} noMargin weight="bolder">

View File

@ -1,9 +1,9 @@
// @flow // @flow
import { import {
lg, md, sm, error, background, lg, md, sm, error,
} from '~/theme/variables' } from '~/theme/variables'
export const styles = (theme: Object) => ({ export const styles = () => ({
heading: { heading: {
padding: `${sm} ${lg}`, padding: `${sm} ${lg}`,
justifyContent: 'space-between', justifyContent: 'space-between',
@ -36,4 +36,8 @@ export const styles = (theme: Object) => ({
cursor: 'pointer', cursor: 'pointer',
}, },
}, },
smallerModalWindow: {
height: 'auto',
position: 'static',
},
}) })

View File

@ -4,9 +4,8 @@ import { List } from 'immutable'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import { SharedSnackbarConsumer } from '~/components/SharedSnackBar' import { SharedSnackbarConsumer } from '~/components/SharedSnackBar'
import Modal from '~/components/Modal' import Modal from '~/components/Modal'
import { type Owner, makeOwner } from '~/routes/safe/store/models/owner' import { type Owner } from '~/routes/safe/store/models/owner'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import { getOwners } from '~/logic/safe/utils'
import CheckOwner from './screens/CheckOwner' import CheckOwner from './screens/CheckOwner'
import ThresholdForm from './screens/ThresholdForm' import ThresholdForm from './screens/ThresholdForm'
import ReviewRemoveOwner from './screens/Review' import ReviewRemoveOwner from './screens/Review'
@ -31,7 +30,7 @@ type Props = {
threshold: number, threshold: number,
network: string, network: string,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, removeSafeOwner: Function,
} }
type ActiveScreen = 'checkOwner' | 'selectThreshold' | 'reviewRemoveOwner' type ActiveScreen = 'checkOwner' | 'selectThreshold' | 'reviewRemoveOwner'
@ -45,7 +44,7 @@ export const sendRemoveOwner = async (
ownersOld: List<Owner>, ownersOld: List<Owner>,
openSnackbar: Function, openSnackbar: Function,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, removeSafeOwner: Function,
) => { ) => {
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const safeOwners = await gnosisSafe.getOwners() const safeOwners = await gnosisSafe.getOwners()
@ -57,16 +56,8 @@ export const sendRemoveOwner = async (
const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar) const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar)
let owners = []
const storedOwners = await getOwners(safeAddress)
storedOwners.forEach((value, key) => owners.push(makeOwner({ address: key, name: value })))
owners = List(owners).filter(o => o.address.toLowerCase() !== ownerAddressToRemove.toLowerCase())
if (txHash) { if (txHash) {
updateSafe({ removeSafeOwner({ safeAddress, ownerAddress: ownerAddressToRemove })
address: safeAddress,
owners,
})
} }
} }
@ -82,7 +73,7 @@ const RemoveOwner = ({
threshold, threshold,
network, network,
createTransaction, createTransaction,
updateSafe, removeSafeOwner,
}: Props) => { }: Props) => {
const [activeScreen, setActiveScreen] = useState<ActiveScreen>('checkOwner') const [activeScreen, setActiveScreen] = useState<ActiveScreen>('checkOwner')
const [values, setValues] = useState<Object>({}) const [values, setValues] = useState<Object>({})
@ -128,7 +119,7 @@ const RemoveOwner = ({
owners, owners,
openSnackbar, openSnackbar,
createTransaction, createTransaction,
updateSafe, removeSafeOwner,
) )
} catch (error) { } catch (error) {
// eslint-disable-next-line // eslint-disable-next-line

View File

@ -4,8 +4,6 @@ import { List } from 'immutable'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import { SharedSnackbarConsumer } from '~/components/SharedSnackBar' import { SharedSnackbarConsumer } from '~/components/SharedSnackBar'
import Modal from '~/components/Modal' import Modal from '~/components/Modal'
import { type Owner, makeOwner } from '~/routes/safe/store/models/owner'
import { getOwners } from '~/logic/safe/utils'
import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts'
import OwnerForm from './screens/OwnerForm' import OwnerForm from './screens/OwnerForm'
import ReviewReplaceOwner from './screens/Review' import ReviewReplaceOwner from './screens/Review'
@ -30,7 +28,7 @@ type Props = {
network: string, network: string,
threshold: string, threshold: string,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, replaceSafeOwner: Function,
} }
type ActiveScreen = 'checkOwner' | 'reviewReplaceOwner' type ActiveScreen = 'checkOwner' | 'reviewReplaceOwner'
@ -44,7 +42,7 @@ export const sendReplaceOwner = async (
ownersOld: List<Owner>, ownersOld: List<Owner>,
openSnackbar: Function, openSnackbar: Function,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, replaceSafeOwner: Function,
) => { ) => {
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const safeOwners = await gnosisSafe.getOwners() const safeOwners = await gnosisSafe.getOwners()
@ -56,22 +54,12 @@ export const sendReplaceOwner = async (
const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar) const txHash = await createTransaction(safeAddress, safeAddress, 0, txData, openSnackbar)
let owners = []
const storedOwners = await getOwners(safeAddress)
storedOwners.forEach((value, key) => owners.push(makeOwner({
address: key,
name: (values.ownerAddress.toLowerCase() === key.toLowerCase()) ? values.ownerName : value,
})))
const newOwnerIndex = List(owners).findIndex(o => o.address.toLowerCase() === values.ownerAddress.toLowerCase())
if (newOwnerIndex < 0) {
owners.push(makeOwner({ address: values.ownerAddress, name: values.ownerName }))
}
owners = List(owners).filter(o => o.address.toLowerCase() !== ownerAddressToRemove.toLowerCase())
if (txHash) { if (txHash) {
updateSafe({ replaceSafeOwner({
address: safeAddress, safeAddress,
owners, oldOwnerAddress: ownerAddressToRemove,
ownerAddress: values.ownerAddress,
ownerName: values.ownerName,
}) })
} }
} }
@ -88,7 +76,7 @@ const ReplaceOwner = ({
network, network,
threshold, threshold,
createTransaction, createTransaction,
updateSafe, replaceSafeOwner,
}: Props) => { }: Props) => {
const [activeScreen, setActiveScreen] = useState<ActiveScreen>('checkOwner') const [activeScreen, setActiveScreen] = useState<ActiveScreen>('checkOwner')
const [values, setValues] = useState<Object>({}) const [values, setValues] = useState<Object>({})
@ -125,7 +113,7 @@ const ReplaceOwner = ({
owners, owners,
openSnackbar, openSnackbar,
createTransaction, createTransaction,
updateSafe, replaceSafeOwner,
) )
} catch (error) { } catch (error) {
// eslint-disable-next-line // eslint-disable-next-line

View File

@ -57,7 +57,10 @@ type Props = {
threshold: number, threshold: number,
userAddress: string, userAddress: string,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, addSafeOwner: Function,
removeSafeOwner: Function,
replaceSafeOwner: Function,
editSafeOwner: Function,
granted: boolean, granted: boolean,
} }
@ -108,7 +111,10 @@ class ManageOwners extends React.Component<Props, State> {
network, network,
userAddress, userAddress,
createTransaction, createTransaction,
updateSafe, addSafeOwner,
removeSafeOwner,
replaceSafeOwner,
editSafeOwner,
granted, granted,
} = this.props } = this.props
const { const {
@ -211,7 +217,7 @@ class ManageOwners extends React.Component<Props, State> {
network={network} network={network}
userAddress={userAddress} userAddress={userAddress}
createTransaction={createTransaction} createTransaction={createTransaction}
updateSafe={updateSafe} addSafeOwner={addSafeOwner}
/> />
<RemoveOwnerModal <RemoveOwnerModal
onClose={this.onHide('RemoveOwner')} onClose={this.onHide('RemoveOwner')}
@ -225,7 +231,7 @@ class ManageOwners extends React.Component<Props, State> {
network={network} network={network}
userAddress={userAddress} userAddress={userAddress}
createTransaction={createTransaction} createTransaction={createTransaction}
updateSafe={updateSafe} removeSafeOwner={removeSafeOwner}
/> />
<ReplaceOwnerModal <ReplaceOwnerModal
onClose={this.onHide('ReplaceOwner')} onClose={this.onHide('ReplaceOwner')}
@ -239,7 +245,7 @@ class ManageOwners extends React.Component<Props, State> {
threshold={threshold} threshold={threshold}
userAddress={userAddress} userAddress={userAddress}
createTransaction={createTransaction} createTransaction={createTransaction}
updateSafe={updateSafe} replaceSafeOwner={replaceSafeOwner}
/> />
<EditOwnerModal <EditOwnerModal
onClose={this.onHide('EditOwner')} onClose={this.onHide('EditOwner')}
@ -249,7 +255,7 @@ class ManageOwners extends React.Component<Props, State> {
selectedOwnerName={selectedOwnerName} selectedOwnerName={selectedOwnerName}
owners={owners} owners={owners}
network={network} network={network}
updateSafe={updateSafe} editSafeOwner={editSafeOwner}
/> />
</React.Fragment> </React.Fragment>
) )

View File

@ -37,7 +37,10 @@ type Props = Actions & {
threshold: number, threshold: number,
network: string, network: string,
createTransaction: Function, createTransaction: Function,
updateSafe: Function, addSafeOwner: Function,
removeSafeOwner: Function,
replaceSafeOwner: Function,
editSafeOwner: Function,
userAddress: string, userAddress: string,
} }
@ -75,6 +78,10 @@ class Settings extends React.Component<Props, State> {
userAddress, userAddress,
createTransaction, createTransaction,
updateSafe, updateSafe,
addSafeOwner,
removeSafeOwner,
replaceSafeOwner,
editSafeOwner,
} = this.props } = this.props
return ( return (
@ -116,7 +123,9 @@ class Settings extends React.Component<Props, State> {
onClick={this.handleChange(2)} onClick={this.handleChange(2)}
testId={OWNERS_SETTINGS_TAB_TESTID} testId={OWNERS_SETTINGS_TAB_TESTID}
> >
Owners ({owners.size}) Owners (
{owners.size}
)
</Row> </Row>
<Hairline /> <Hairline />
{granted && ( {granted && (
@ -146,7 +155,10 @@ class Settings extends React.Component<Props, State> {
network={network} network={network}
createTransaction={createTransaction} createTransaction={createTransaction}
userAddress={userAddress} userAddress={userAddress}
updateSafe={updateSafe} addSafeOwner={addSafeOwner}
removeSafeOwner={removeSafeOwner}
replaceSafeOwner={replaceSafeOwner}
editSafeOwner={editSafeOwner}
granted={granted} granted={granted}
/> />
)} )}