Implementation of removing owner logic indepentdently the order

This commit is contained in:
apanizo 2018-10-03 10:51:25 +02:00
parent ef17ca59a5
commit 9e6c97aea8
2 changed files with 73 additions and 7 deletions

View File

@ -23,7 +23,8 @@ type Props = {
classes: Object, classes: Object,
otherAccounts: string[], otherAccounts: string[],
errors: Object, errors: Object,
// updateInitialProps: () => void, values: Object,
updateInitialProps: (initialValues: Object) => void,
} }
type State = { type State = {
@ -64,18 +65,32 @@ const getAddressValidators = (addresses: string[], position: number) => {
const noErrorsOn = (name: string, errors: Object) => errors[name] === undefined const noErrorsOn = (name: string, errors: Object) => errors[name] === undefined
export const calculateValuesAfterRemoving = (index: number, notRemovedOwners: number, values: Object) => {
const initialValues = { ...values }
const numOwnersAfterRemoving = notRemovedOwners - 1
// muevo indices
for (let i = index; i < numOwnersAfterRemoving; i += 1) {
initialValues[getOwnerNameBy(i)] = values[getOwnerNameBy(i + 1)]
initialValues[getOwnerAddressBy(i)] = values[getOwnerAddressBy(i + 1)]
}
delete initialValues[getOwnerNameBy(numOwnersAfterRemoving)]
delete initialValues[getOwnerAddressBy(numOwnersAfterRemoving)]
return initialValues
}
class SafeOwners extends React.Component<Props, State> { class SafeOwners extends React.Component<Props, State> {
state = { state = {
numOwners: 3, numOwners: 3,
} }
// eslint-disable-next-line
onRemoveRow = (index: number) => () => { onRemoveRow = (index: number) => () => {
/* const { values } = this.props
this.props.updateInitialProps({ const { numOwners } = this.state
owner0Address: 'moeFeo', const initialValues = calculateValuesAfterRemoving(index, numOwners, values)
}) this.props.updateInitialProps(initialValues)
*/
this.setState(state => ({ this.setState(state => ({
numOwners: state.numOwners - 1, numOwners: state.numOwners - 1,
@ -161,6 +176,7 @@ const SafeOwnersPage = ({ updateInitialProps }: Object) => (controls: React$Node
otherAccounts={getAccountsFrom(values)} otherAccounts={getAccountsFrom(values)}
errors={errors} errors={errors}
updateInitialProps={updateInitialProps} updateInitialProps={updateInitialProps}
values={values}
/> />
</OpenPaper> </OpenPaper>
</React.Fragment> </React.Fragment>

View File

@ -13,8 +13,10 @@ import fetchSafe from '~/routes/safe/store/actions/fetchSafe'
import { removeOwner, shouldDecrease, initialValuesFrom } from '~/routes/safe/component/RemoveOwner' import { removeOwner, shouldDecrease, initialValuesFrom } from '~/routes/safe/component/RemoveOwner'
import { DECREASE_PARAM } from '~/routes/safe/component/RemoveOwner/RemoveOwnerForm' import { DECREASE_PARAM } from '~/routes/safe/component/RemoveOwner/RemoveOwnerForm'
import { getSafeFrom } from '~/test/utils/safeHelper' import { getSafeFrom } from '~/test/utils/safeHelper'
import { getOwnerNameBy, getOwnerAddressBy } from '~/routes/open/components/fields'
import { processTransaction } from '~/logic/safe/safeFrontendOperations' import { processTransaction } from '~/logic/safe/safeFrontendOperations'
import { allowedRemoveSenderInTxHistoryService } from '~/config' import { allowedRemoveSenderInTxHistoryService } from '~/config'
import { calculateValuesAfterRemoving } from '~/routes/open/components/SafeOwnersForm'
describe('React DOM TESTS > Add and remove owners', () => { describe('React DOM TESTS > Add and remove owners', () => {
const processOwnerModification = async (store, safeAddress, executor, threshold) => { const processOwnerModification = async (store, safeAddress, executor, threshold) => {
@ -46,6 +48,54 @@ describe('React DOM TESTS > Add and remove owners', () => {
const getAddressesFrom = (safe: Safe) => safe.get('owners').map(owner => owner.get('address')) const getAddressesFrom = (safe: Safe) => safe.get('owners').map(owner => owner.get('address'))
it.only('creates initialValues removing last owner', () => {
const numOwners = 3
const values = {
moe: 'Bart',
[getOwnerNameBy(0)]: 'Foo',
[getOwnerAddressBy(0)]: '0x1',
[getOwnerNameBy(1)]: 'Bar',
[getOwnerAddressBy(1)]: '0x2',
[getOwnerNameBy(2)]: 'Baz',
[getOwnerAddressBy(2)]: '0x3',
}
const indexToRemove = 2
const initialValues = calculateValuesAfterRemoving(indexToRemove, numOwners, values)
expect(initialValues).toEqual({
moe: 'Bart',
[getOwnerNameBy(0)]: 'Foo',
[getOwnerAddressBy(0)]: '0x1',
[getOwnerNameBy(1)]: 'Bar',
[getOwnerAddressBy(1)]: '0x2',
})
})
it.only('creates initialValues removing middle owner', () => {
const numOwners = 3
const values = {
moe: 'Bart',
[getOwnerNameBy(0)]: 'Foo',
[getOwnerAddressBy(0)]: '0x1',
[getOwnerNameBy(1)]: 'Bar',
[getOwnerAddressBy(1)]: '0x2',
[getOwnerNameBy(2)]: 'Baz',
[getOwnerAddressBy(2)]: '0x3',
}
const indexToRemove = 1
const initialValues = calculateValuesAfterRemoving(indexToRemove, numOwners, values)
expect(initialValues).toEqual({
moe: 'Bart',
[getOwnerNameBy(0)]: 'Foo',
[getOwnerAddressBy(0)]: '0x1',
[getOwnerNameBy(1)]: 'Baz',
[getOwnerAddressBy(1)]: '0x3',
})
})
it('adds owner without increasing the threshold', async () => { it('adds owner without increasing the threshold', async () => {
// GIVEN // GIVEN
const numOwners = 2 const numOwners = 2