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 * as React from "react";
import { List as ImmutableList } from "immutable"; import { List as ImmutableList } from "immutable";
import { connect } from "react-redux"; import { connect, useDispatch } from "react-redux";
import { withRouter } from "react-router"; import { useHistory } from "react-router";
import { BrowserRouter } from "react-router-dom"; import { BrowserRouter } from "react-router-dom";
import { MuiThemeProvider as ThemeProvider, createMuiTheme } from "@material-ui/core/styles"; // v1.x import { MuiThemeProvider as ThemeProvider, createMuiTheme } from "@material-ui/core/styles"; // v1.x
import amber from "@material-ui/core/colors/amber"; import amber from "@material-ui/core/colors/amber";
@ -38,8 +38,6 @@ import * as actions from "./store/actions";
import { credentialsSelector } from "./login"; import { credentialsSelector } from "./login";
import { History } from "history";
export const routeResolver = new RouteResolver({ export const routeResolver = new RouteResolver({
home: "", home: "",
pim: { pim: {
@ -95,205 +93,178 @@ export const routeResolver = new RouteResolver({
}, },
}); });
const AppBarWitHistory = withRouter( interface AppBarPropsType {
class extends React.PureComponent { toggleDrawerIcon: any;
public props: { iconElementRight: any;
title: string; }
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);
}
public render() {
const {
staticContext,
toggleDrawerIcon,
history,
iconElementRight,
...props
} = this.props;
return (
<AppBar
position="static"
{...props}
>
<Toolbar>
<div style={{ marginLeft: -12, marginRight: 20 }}>
{!this.canGoBack() ?
toggleDrawerIcon :
<IconButton onClick={this.goBack}><NavigationBack /></IconButton>
}
</div>
<div style={{ flexGrow: 1, fontSize: "1.25em" }} id="appbar-title" /> function AppBarWitHistory(props: AppBarPropsType) {
const history = useHistory();
<div style={{ marginRight: -12 }} id="appbar-buttons"> function canGoBack() {
{iconElementRight} return (
</div> (history!.length > 1) &&
</Toolbar> (history!.location.pathname !== routeResolver.getRoute("pim")) &&
</AppBar> (history!.location.pathname !== routeResolver.getRoute("home"))
); );
} }
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() { function goBack() {
this.props.history!.goBack(); history!.goBack();
}
} }
); const {
toggleDrawerIcon,
iconElementRight,
...rest
} = props;
return (
<AppBar
position="static"
{...rest}
>
<Toolbar>
<div style={{ marginLeft: -12, marginRight: 20 }}>
{!canGoBack() ?
toggleDrawerIcon :
<IconButton onClick={goBack}><NavigationBack /></IconButton>
}
</div>
<div style={{ flexGrow: 1, fontSize: "1.25em" }} id="appbar-title" />
<div style={{ marginRight: -12 }} id="appbar-buttons">
{iconElementRight}
</div>
</Toolbar>
</AppBar>
);
}
const IconRefreshWithSpin = withSpin(NavigationRefresh); const IconRefreshWithSpin = withSpin(NavigationRefresh);
class App extends React.PureComponent { interface PropsType {
public state: { credentials: store.CredentialsData;
drawerOpen: boolean; entries: store.EntriesData;
errorsDialog: boolean; fetchCount: number;
}; darkMode: boolean;
errors: ImmutableList<Error>;
public props: { }
credentials: store.CredentialsData;
entries: store.EntriesData;
fetchCount: number;
darkMode: boolean;
errors: ImmutableList<Error>;
};
constructor(props: any) { function App(props: PropsType) {
super(props); const [drawerOpen, setDrawerOpen] = React.useState(false);
this.state = { drawerOpen: false, errorsDialog: false }; const [errorsDialog, setErrorsDialog] = React.useState(false);
const dispatch = useDispatch();
this.toggleDrawer = this.toggleDrawer.bind(this); function refresh() {
this.closeDrawer = this.closeDrawer.bind(this); dispatch(actions.fetchAll(props.credentials, props.entries));
this.refresh = this.refresh.bind(this);
this.autoRefresh = this.autoRefresh.bind(this);
} }
public render() { function autoRefresh() {
const credentials = this.props.credentials ?? null; if (navigator.onLine && props.credentials &&
const { darkMode } = this.props; !(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) {
refresh();
const errors = this.props.errors; }
const fetching = this.props.fetchCount > 0;
const muiTheme = createMuiTheme({
palette: {
type: darkMode ? "dark" : undefined,
primary: amber,
secondary: {
light: lightBlue.A200,
main: lightBlue.A400,
dark: lightBlue.A700,
contrastText: "#fff",
},
},
});
const styles = {
main: {
backgroundColor: muiTheme.palette.background.default,
color: muiTheme.palette.text.primary,
flexGrow: 1,
},
};
return (
<ThemeProvider theme={muiTheme}>
<BrowserRouter>
<div style={styles.main} className={darkMode ? "theme-dark" : "theme-light"}>
<AppBarWitHistory
toggleDrawerIcon={<IconButton onClick={this.toggleDrawer}><NavigationMenu /></IconButton>}
iconElementRight={
<>
{(errors.size > 0) && (
<IconButton onClick={() => this.setState({ errorsDialog: true })} title="Parse Errors">
<Badge badgeContent={errors.size} color="error">
<ErrorsIcon />
</Badge>
</IconButton>
)}
<IconButton disabled={!credentials || fetching} onClick={this.refresh} title="Refresh">
<IconRefreshWithSpin spin={fetching} />
</IconButton>
</>
}
/>
<ConfirmationDialog
title="Parse Errors"
open={this.state.errorsDialog}
labelOk="OK"
onCancel={() => this.setState({ errorsDialog: false })}
onOk={() => this.setState({ errorsDialog: false })}
>
<h4>
This should not happen, please contact developers!
</h4>
<List>
{errors.map((error, index) => (
<ListItem
key={index}
style={{ height: "unset" }}
onClick={() => (window as any).navigator.clipboard.writeText(`${error.message}\n\n${error.stack}`)}
>
<PrettyError error={error} />
</ListItem>
))}
</List>
</ConfirmationDialog>
<Drawer
open={this.state.drawerOpen}
onClose={this.toggleDrawer}
>
<SideMenu etesync={credentials} onCloseDrawerRequest={this.closeDrawer} />
</Drawer>
<ErrorBoundary>
<LoginGate />
</ErrorBoundary>
</div>
</BrowserRouter>
</ThemeProvider>
);
} }
private toggleDrawer() { React.useEffect(() => {
this.setState({ drawerOpen: !this.state.drawerOpen }); const interval = 60 * 1000;
} const id = setInterval(autoRefresh, interval);
return () => clearInterval(id);
});
private closeDrawer() { function toggleDrawer() {
this.setState({ drawerOpen: false }); setDrawerOpen(!drawerOpen);
} }
private refresh() { function closeDrawer() {
store.store.dispatch<any>(actions.fetchAll(this.props.credentials, this.props.entries)); setDrawerOpen(false);
} }
private autoRefresh() { const credentials = props.credentials ?? null;
if (navigator.onLine && this.props.credentials && const { darkMode } = props;
!(window.location.pathname.match(/.*\/(new|edit|duplicate)$/))) {
this.refresh(); const errors = props.errors;
} const fetching = props.fetchCount > 0;
const muiTheme = createMuiTheme({
palette: {
type: darkMode ? "dark" : undefined,
primary: amber,
secondary: {
light: lightBlue.A200,
main: lightBlue.A400,
dark: lightBlue.A700,
contrastText: "#fff",
},
},
});
} const styles = {
main: {
backgroundColor: muiTheme.palette.background.default,
color: muiTheme.palette.text.primary,
flexGrow: 1,
},
};
componentDidMount() { return (
const interval = 60 * 1000; <ThemeProvider theme={muiTheme}>
setInterval(this.autoRefresh, interval); <BrowserRouter>
} <div style={styles.main} className={darkMode ? "theme-dark" : "theme-light"}>
<AppBarWitHistory
toggleDrawerIcon={<IconButton onClick={toggleDrawer}><NavigationMenu /></IconButton>}
iconElementRight={
<>
{(errors.size > 0) && (
<IconButton onClick={() => setErrorsDialog(true)} title="Parse Errors">
<Badge badgeContent={errors.size} color="error">
<ErrorsIcon />
</Badge>
</IconButton>
)}
<IconButton disabled={!credentials || fetching} onClick={refresh} title="Refresh">
<IconRefreshWithSpin spin={fetching} />
</IconButton>
</>
}
/>
<ConfirmationDialog
title="Parse Errors"
open={errorsDialog}
labelOk="OK"
onCancel={() => setErrorsDialog(false)}
onOk={() => setErrorsDialog(false)}
>
<h4>
This should not happen, please contact developers!
</h4>
<List>
{errors.map((error, index) => (
<ListItem
key={index}
style={{ height: "unset" }}
onClick={() => (window as any).navigator.clipboard.writeText(`${error.message}\n\n${error.stack}`)}
>
<PrettyError error={error} />
</ListItem>
))}
</List>
</ConfirmationDialog>
<Drawer
open={drawerOpen}
onClose={toggleDrawer}
>
<SideMenu etesync={credentials} onCloseDrawerRequest={closeDrawer} />
</Drawer>
<ErrorBoundary>
<LoginGate />
</ErrorBoundary>
</div>
</BrowserRouter>
</ThemeProvider>
);
} }
const mapStateToProps = (state: store.StoreState) => { const mapStateToProps = (state: store.StoreState) => {

Loading…
Cancel
Save