Files
SCM-Manager/scm-ui/src/containers/App.js

73 lines
1.6 KiB
JavaScript
Raw Normal View History

import React, { Component } from "react";
import Main from "./Main";
import Login from "./Login";
2018-07-09 11:38:13 +02:00
import { connect } from "react-redux";
import { withRouter } from "react-router-dom";
2018-07-11 17:02:38 +02:00
import { ThunkDispatch } from "redux-thunk";
2018-07-11 21:01:29 +02:00
import { fetchMe } from "../modules/me";
2018-07-11 14:59:01 +02:00
import "./App.css";
import Header from "../components/Header";
import PrimaryNavigation from "../components/PrimaryNavigation";
2018-07-11 21:01:29 +02:00
import Loading from "../components/Loading";
import Notification from "../components/Notification";
import Footer from "../components/Footer";
2018-07-11 22:01:36 +02:00
import ErrorNotification from "../components/ErrorNotification";
2018-07-11 14:59:01 +02:00
type Props = {
2018-07-11 21:01:29 +02:00
me: any,
2018-07-11 22:01:36 +02:00
error: Error,
loading: boolean,
fetchMe: () => void
2018-07-05 16:48:56 +02:00
};
class App extends Component<Props> {
componentDidMount() {
2018-07-11 21:01:29 +02:00
this.props.fetchMe();
2018-07-09 11:38:13 +02:00
}
render() {
2018-07-11 22:01:36 +02:00
const { me, loading, error } = this.props;
2018-07-11 14:59:01 +02:00
2018-07-11 21:01:29 +02:00
let content = [];
2018-07-11 14:59:01 +02:00
let navigation;
if (loading) {
2018-07-11 21:01:29 +02:00
content.push(<Loading />);
2018-07-11 22:01:36 +02:00
} else if (error) {
// TODO add error page instead of plain notification
content.push(<ErrorNotification error={error} />);
2018-07-11 21:01:29 +02:00
} else if (!me) {
content.push(<Login />);
2018-07-05 16:48:56 +02:00
} else {
2018-07-11 21:01:29 +02:00
content.push(<Main />, <Footer me={me} />);
2018-07-11 14:59:01 +02:00
navigation = <PrimaryNavigation />;
2018-07-05 16:48:56 +02:00
}
2018-07-11 14:59:01 +02:00
return (
<div className="App">
<Header>{navigation}</Header>
2018-07-11 21:01:29 +02:00
{content.map(c => {
return c;
})}
2018-07-11 14:59:01 +02:00
</div>
);
}
}
2018-07-11 17:02:38 +02:00
const mapDispatchToProps = (dispatch: ThunkDispatch) => {
2018-07-09 11:38:13 +02:00
return {
2018-07-11 21:01:29 +02:00
fetchMe: () => dispatch(fetchMe())
2018-07-09 11:38:13 +02:00
};
};
const mapStateToProps = state => {
2018-07-11 21:01:29 +02:00
return state.me || {};
2018-07-09 11:38:13 +02:00
};
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(App)
);