App: change to be a function component.

master
Tom Hacohen 4 years ago
parent 9ec5d2a708
commit c1b57eecfd

@ -3,8 +3,8 @@
import * as React from "react";
import { List as ImmutableList } from "immutable";
import { connect } from "react-redux";
import { withRouter } from "react-router";
import { connect, useDispatch } from "react-redux";
import { useHistory } from "react-router";
import { BrowserRouter } from "react-router-dom";
import { MuiThemeProvider as ThemeProvider, createMuiTheme } from "@material-ui/core/styles"; // v1.x
import amber from "@material-ui/core/colors/amber";
@ -38,8 +38,6 @@ import * as actions from "./store/actions";
import { credentialsSelector } from "./login";
import { History } from "history";
export const routeResolver = new RouteResolver({
home: "",
pim: {
@ -95,40 +93,40 @@ export const routeResolver = new RouteResolver({
},
});
const AppBarWitHistory = withRouter(
class extends React.PureComponent {
public props: {
title: string;
interface AppBarPropsType {
toggleDrawerIcon: any;
history?: History;
staticContext?: any;
iconElementRight: any;
};
}
constructor(props: any) {
super(props);
this.goBack = this.goBack.bind(this);
this.canGoBack = this.canGoBack.bind(this);
function AppBarWitHistory(props: AppBarPropsType) {
const history = useHistory();
function canGoBack() {
return (
(history!.length > 1) &&
(history!.location.pathname !== routeResolver.getRoute("pim")) &&
(history!.location.pathname !== routeResolver.getRoute("home"))
);
}
public render() {
function goBack() {
history!.goBack();
}
const {
staticContext,
toggleDrawerIcon,
history,
iconElementRight,
...props
} = this.props;
...rest
} = props;
return (
<AppBar
position="static"
{...props}
{...rest}
>
<Toolbar>
<div style={{ marginLeft: -12, marginRight: 20 }}>
{!this.canGoBack() ?
{!canGoBack() ?
toggleDrawerIcon :
<IconButton onClick={this.goBack}><NavigationBack /></IconButton>
<IconButton onClick={goBack}><NavigationBack /></IconButton>
}
</div>
@ -140,54 +138,53 @@ const AppBarWitHistory = withRouter(
</Toolbar>
</AppBar>
);
}
private canGoBack() {
return (
(this.props.history!.length > 1) &&
(this.props.history!.location.pathname !== routeResolver.getRoute("pim")) &&
(this.props.history!.location.pathname !== routeResolver.getRoute("home"))
);
}
private goBack() {
this.props.history!.goBack();
}
}
);
}
const IconRefreshWithSpin = withSpin(NavigationRefresh);
class App extends React.PureComponent {
public state: {
drawerOpen: boolean;
errorsDialog: boolean;
};
public props: {
interface PropsType {
credentials: store.CredentialsData;
entries: store.EntriesData;
fetchCount: number;
darkMode: boolean;
errors: ImmutableList<Error>;
};
}
function App(props: PropsType) {
const [drawerOpen, setDrawerOpen] = React.useState(false);
const [errorsDialog, setErrorsDialog] = React.useState(false);
const dispatch = useDispatch();
function refresh() {
dispatch(actions.fetchAll(props.credentials, props.entries));
}
constructor(props: any) {
super(props);
this.state = { drawerOpen: false, errorsDialog: false };
function autoRefresh() {
if (navigator.onLine && props.credentials &&
!(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) {
refresh();
}
}
React.useEffect(() => {
const interval = 60 * 1000;
const id = setInterval(autoRefresh, interval);
return () => clearInterval(id);
});
this.toggleDrawer = this.toggleDrawer.bind(this);
this.closeDrawer = this.closeDrawer.bind(this);
this.refresh = this.refresh.bind(this);
this.autoRefresh = this.autoRefresh.bind(this);
function toggleDrawer() {
setDrawerOpen(!drawerOpen);
}
public render() {
const credentials = this.props.credentials ?? null;
const { darkMode } = this.props;
function closeDrawer() {
setDrawerOpen(false);
}
const credentials = props.credentials ?? null;
const { darkMode } = props;
const errors = this.props.errors;
const fetching = this.props.fetchCount > 0;
const errors = props.errors;
const fetching = props.fetchCount > 0;
const muiTheme = createMuiTheme({
palette: {
@ -215,17 +212,17 @@ class App extends React.PureComponent {
<BrowserRouter>
<div style={styles.main} className={darkMode ? "theme-dark" : "theme-light"}>
<AppBarWitHistory
toggleDrawerIcon={<IconButton onClick={this.toggleDrawer}><NavigationMenu /></IconButton>}
toggleDrawerIcon={<IconButton onClick={toggleDrawer}><NavigationMenu /></IconButton>}
iconElementRight={
<>
{(errors.size > 0) && (
<IconButton onClick={() => this.setState({ errorsDialog: true })} title="Parse Errors">
<IconButton onClick={() => setErrorsDialog(true)} title="Parse Errors">
<Badge badgeContent={errors.size} color="error">
<ErrorsIcon />
</Badge>
</IconButton>
)}
<IconButton disabled={!credentials || fetching} onClick={this.refresh} title="Refresh">
<IconButton disabled={!credentials || fetching} onClick={refresh} title="Refresh">
<IconRefreshWithSpin spin={fetching} />
</IconButton>
</>
@ -233,10 +230,10 @@ class App extends React.PureComponent {
/>
<ConfirmationDialog
title="Parse Errors"
open={this.state.errorsDialog}
open={errorsDialog}
labelOk="OK"
onCancel={() => this.setState({ errorsDialog: false })}
onOk={() => this.setState({ errorsDialog: false })}
onCancel={() => setErrorsDialog(false)}
onOk={() => setErrorsDialog(false)}
>
<h4>
This should not happen, please contact developers!
@ -255,10 +252,10 @@ class App extends React.PureComponent {
</ConfirmationDialog>
<Drawer
open={this.state.drawerOpen}
onClose={this.toggleDrawer}
open={drawerOpen}
onClose={toggleDrawer}
>
<SideMenu etesync={credentials} onCloseDrawerRequest={this.closeDrawer} />
<SideMenu etesync={credentials} onCloseDrawerRequest={closeDrawer} />
</Drawer>
<ErrorBoundary>
@ -268,32 +265,6 @@ class App extends React.PureComponent {
</BrowserRouter>
</ThemeProvider>
);
}
private toggleDrawer() {
this.setState({ drawerOpen: !this.state.drawerOpen });
}
private closeDrawer() {
this.setState({ drawerOpen: false });
}
private refresh() {
store.store.dispatch<any>(actions.fetchAll(this.props.credentials, this.props.entries));
}
private autoRefresh() {
if (navigator.onLine && this.props.credentials &&
!(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) {
this.refresh();
}
}
componentDidMount() {
const interval = 60 * 1000;
setInterval(this.autoRefresh, interval);
}
}
const mapStateToProps = (state: store.StoreState) => {

Loading…
Cancel
Save