Farmbot-Web-App/frontend/routes.tsx

65 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2017-07-18 10:53:09 -06:00
import { store as _store } from "./redux/store";
2017-06-29 12:54:02 -06:00
import { history } from "./history";
import { Store } from "./redux/interfaces";
import { ready } from "./config/actions";
import { Session } from "./session";
2017-10-24 16:28:05 -06:00
import { attachToRoot } from "./util";
2018-04-02 09:23:09 -06:00
import { ErrorBoundary } from "./error_boundary";
2018-09-20 17:46:11 -06:00
import { Router } from "takeme";
import { UNBOUND_ROUTES } from "./route_config";
import { App } from "./app";
2019-01-28 09:07:26 -07:00
import { Provider } from "react-redux";
2018-03-02 10:32:09 -07:00
interface RootComponentProps { store: Store; }
2017-10-12 12:47:29 -06:00
2018-09-21 08:39:22 -06:00
export const attachAppToDom = () => {
2019-08-26 12:20:46 -06:00
attachToRoot(RootComponent, { store: _store });
// tslint:disable-next-line:no-any
_store.dispatch(ready() as any);
2017-10-12 12:47:29 -06:00
};
interface RootComponentState {
2019-08-26 12:20:46 -06:00
Route: React.ComponentType;
ChildRoute?: React.ComponentType;
}
2019-09-23 12:56:35 -06:00
export class RootComponent
extends React.Component<RootComponentProps, RootComponentState> {
2019-08-26 12:20:46 -06:00
state: RootComponentState = { Route: () => <div>Loading...</div> };
UNSAFE_componentWillMount() {
const notLoggedIn = !Session.fetchStoredToken();
const currentLocation = history.getCurrentLocation().pathname;
const restrictedArea = currentLocation.includes("/app");
(notLoggedIn && restrictedArea && Session.clear());
}
changeRoute =
(Route: React.ComponentType, ChildRoute?: React.ComponentType) => {
this.setState({ Route: Route, ChildRoute });
};
componentDidMount() {
const main_routes = UNBOUND_ROUTES.map(bindTo => bindTo(this.changeRoute));
new Router(main_routes).enableHtml5Routing("/app").init();
}
render() {
const { Route } = this.state;
const { ChildRoute } = this.state;
const props = ChildRoute ? { children: <ChildRoute /> } : {};
try {
return <ErrorBoundary>
<Provider store={_store}>
2019-09-19 13:09:00 -06:00
<App>
2019-08-26 12:20:46 -06:00
<Route {...props} />
</App>
</Provider>
</ErrorBoundary>;
} catch (error) {
return <p> Problem loading page.</p>;
2018-09-14 13:47:32 -06:00
}
2019-08-26 12:20:46 -06:00
}
2017-06-29 12:54:02 -06:00
}