refactor main container for hooks

This commit is contained in:
Barry Gitarts 2019-08-26 20:10:10 -04:00 committed by Barry G
parent 16fe253f41
commit f85e610ebc
1 changed files with 124 additions and 127 deletions

View File

@ -1,9 +1,9 @@
import React,{ Suspense, lazy } from 'react';
import React,{ Suspense, lazy, useState, useEffect } from 'react';
import { Route, Link, Switch, withRouter } from 'react-router-dom'
import PropTypes from 'prop-types';
import classNames from 'classnames';
import queryString from 'query-string'
import { withStyles } from '@material-ui/core/styles'
import { makeStyles } from '@material-ui/core/styles'
import { Hook, Console, Decode } from 'console-feed'
import Drawer from '@material-ui/core/Drawer';
import CssBaseline from '@material-ui/core/CssBaseline';
@ -16,7 +16,6 @@ import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import AddIcon from '@material-ui/icons/Add';
import ChevronLeftIcon from '@material-ui/icons/ChevronLeft';
import ChevronRightIcon from '@material-ui/icons/ChevronRight';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
@ -45,7 +44,7 @@ const formatAccount = account => {
return `${start}...${end}`
}
const styles = theme => ({
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
},
@ -112,7 +111,7 @@ const styles = theme => ({
},
content: {
flexGrow: 1,
padding: theme.spacing.unit * 3,
padding: theme.spacing(3),
transition: theme.transitions.create('margin', {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
@ -129,7 +128,7 @@ const styles = theme => ({
link: {
textDecoration: 'none'
}
})
}))
const MenuItem = ({to, name, className, icon}) => (
<Link to={to} className={className}>
@ -140,37 +139,39 @@ const MenuItem = ({to, name, className, icon}) => (
</Link>
)
class PersistentDrawerLeft extends React.Component {
state = {
open: false,
queryParams: {},
logs: []
};
MenuItem.propTypes = {
to: PropTypes.string,
name: PropTypes.string,
className: PropTypes.string,
icon: PropTypes.object
}
componentDidMount() {
const { location: { search } } = this.props
function PersistentDrawerLeft({ loading, account, children, enableEthereum, location: { pathname, search } }) {
const [open, setOpen] = useState(false)
const [queryParams, setQueryParams] = useState({})
const [logs, setLogs] = useState([])
useEffect(() => {
const queryParams = queryString.parse(search)
if (queryParams.logs) this.enableLogs()
this.setState({ queryParams })
}
if (queryParams.logs) enableLogs()
setQueryParams({ queryParams })
}, [search])
enableLogs = () => {
const enableLogs = () => {
Hook(window.console, log => {
this.setState(({ logs }) => ({ logs: [Decode(log), ...logs] }))
setLogs(({ logs }) => ({ logs: [Decode(log), ...logs] }))
})
}
handleDrawerOpen = () => {
this.setState({ open: true })
const handleDrawerOpen = () => {
setOpen({ open: true })
};
handleDrawerClose = () => {
this.setState({ open: false })
const handleDrawerClose = () => {
setOpen({ open: false })
};
render() {
const { classes, theme, loading, account, enableEthereum, location: { pathname } } = this.props
const { open, logs, queryParams } = this.state
const classes = useStyles()
const isHome = pathname === "/"
return (
@ -186,16 +187,14 @@ class PersistentDrawerLeft extends React.Component {
{queryParams.menu && <IconButton
color="inherit"
aria-label="Open drawer"
onClick={this.handleDrawerOpen}
className={classNames(classes.menuButton, open && !loading && classes.hide)}
>
onClick={handleDrawerOpen}
className={classNames(classes.menuButton, open && !loading && classes.hide)}>
{loading && <ScaleLoader
sizeUnit={'px'}
height={20}
width={2}
margin="3px"
color={'#FFFFFF'}
/>}
color={'#FFFFFF'} />}
{!loading && <MenuIcon/>}
</IconButton>}
{!isHome && <Typography variant="h6" noWrap>
@ -220,8 +219,8 @@ class PersistentDrawerLeft extends React.Component {
}}
>
<div className={classes.drawerHeader}>
<IconButton onClick={this.handleDrawerClose}>
{theme.direction === 'ltr' ? <ChevronLeftIcon /> : <ChevronRightIcon />}
<IconButton onClick={handleDrawerClose}>
<ChevronLeftIcon />
</IconButton>
</div>
<Divider/>
@ -263,22 +262,20 @@ class PersistentDrawerLeft extends React.Component {
<Route path="/console" render={() => <Console logs={logs} variant="dark" />} />
</Switch>
</Suspense>
{this.props.children}
{children}
</div>
</main>
</div>
);
}
}
PersistentDrawerLeft.propTypes = {
classes: PropTypes.object.isRequired,
theme: PropTypes.object.isRequired,
loading: PropTypes.bool.isRequired,
account: PropTypes.string,
enableEthereum: PropTypes.func.isRequired,
location: PropTypes.object
location: PropTypes.object,
children: PropTypes.node
};
const DrawerWithRouter = withRouter(PersistentDrawerLeft)
export default withStyles(styles, { withTheme: true })(DrawerWithRouter)
export default DrawerWithRouter