rewrite OwnersList with hooks

This commit is contained in:
Mikhail Mikheev 2019-06-26 15:55:08 +04:00
parent f8e9b4dfb3
commit ff9448c4db
1 changed files with 70 additions and 84 deletions

View File

@ -1,11 +1,11 @@
// @flow // @flow
import * as React from 'react' import React, { useState, useEffect } from 'react'
import Block from '~/components/layout/Block'
import { withStyles } from '@material-ui/core/styles' import { withStyles } from '@material-ui/core/styles'
import OpenInNew from '@material-ui/icons/OpenInNew'
import Block from '~/components/layout/Block'
import Field from '~/components/forms/Field' import Field from '~/components/forms/Field'
import { required } from '~/components/forms/validator' import { required } from '~/components/forms/validator'
import TextField from '~/components/forms/TextField' import TextField from '~/components/forms/TextField'
import OpenInNew from '@material-ui/icons/OpenInNew'
import Identicon from '~/components/Identicon' import Identicon from '~/components/Identicon'
import OpenPaper from '~/components/Stepper/OpenPaper' import OpenPaper from '~/components/Stepper/OpenPaper'
import Col from '~/components/layout/Col' import Col from '~/components/layout/Col'
@ -78,10 +78,6 @@ type Props = LayoutProps & {
updateInitialProps: (initialValues: Object) => void, updateInitialProps: (initialValues: Object) => void,
} }
type State = {
owners: Array<string>,
}
const calculateSafeValues = (owners: Array<string>, threshold: Number, values: Object) => { const calculateSafeValues = (owners: Array<string>, threshold: Number, values: Object) => {
const initialValues = { ...values } const initialValues = { ...values }
for (let i = 0; i < owners.length; i += 1) { for (let i = 0; i < owners.length; i += 1) {
@ -91,42 +87,35 @@ const calculateSafeValues = (owners: Array<string>, threshold: Number, values: O
return initialValues return initialValues
} }
class OwnerListComponent extends React.PureComponent<Props, State> { const OwnerListComponent = (props: Props) => {
static whyDidYouRender = true const [owners, setOwners] = useState<Array<string>>([])
const {
values, updateInitialProps, network, classes,
} = props
state = { useEffect(() => {
owners: [], let isCurrent = true
}
mounted = false const fetchSafe = async () => {
componentDidMount = async () => {
this.mounted = true
const { values, updateInitialProps } = this.props
const safeAddress = values[FIELD_LOAD_ADDRESS] const safeAddress = values[FIELD_LOAD_ADDRESS]
const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress) const gnosisSafe = await getGnosisSafeInstanceAt(safeAddress)
const owners = await gnosisSafe.getOwners() const safeOwners = await gnosisSafe.getOwners()
const threshold = await gnosisSafe.getThreshold() const threshold = await gnosisSafe.getThreshold()
if (!owners) { if (isCurrent && owners) {
return const sortedOwners = safeOwners.sort()
} const initialValues = calculateSafeValues(sortedOwners, threshold, values)
if (this.mounted) {
const initialValues = calculateSafeValues(owners.sort(), threshold, values)
updateInitialProps(initialValues) updateInitialProps(initialValues)
this.setState(() => ({ owners: owners.sort() })) setOwners(sortedOwners)
} }
} }
componentWillUnmount() { fetchSafe()
this.mounted = false
}
render() { return () => {
const { network, classes } = this.props isCurrent = false
const { owners } = this.state }
}, [])
return ( return (
<React.Fragment> <React.Fragment>
@ -173,7 +162,6 @@ class OwnerListComponent extends React.PureComponent<Props, State> {
</React.Fragment> </React.Fragment>
) )
} }
}
const OwnerListPage = withStyles(styles)(OwnerListComponent) const OwnerListPage = withStyles(styles)(OwnerListComponent)
@ -185,6 +173,4 @@ const OwnerList = ({ updateInitialProps }: Object, network: string) => (controls
</React.Fragment> </React.Fragment>
) )
OwnerList.whyDidYouRender = true
export default OwnerList export default OwnerList