2019-10-20 18:02:52 +02:00
|
|
|
import React, { Component } from "react";
|
|
|
|
|
import Main from "./Main";
|
|
|
|
|
import { connect } from "react-redux";
|
2020-01-08 13:40:07 +01:00
|
|
|
import { compose } from "redux";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { withRouter } from "react-router-dom";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { fetchMe, getFetchMeFailure, getMe, isAuthenticated, isFetchMePending } from "../modules/auth";
|
|
|
|
|
import { ErrorPage, Footer, Header, Loading, PrimaryNavigation } from "@scm-manager/ui-components";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { Links, Me } from "@scm-manager/ui-types";
|
2018-10-05 13:14:33 +02:00
|
|
|
import {
|
|
|
|
|
getFetchIndexResourcesFailure,
|
2019-01-02 11:53:51 +01:00
|
|
|
getLinks,
|
2018-10-11 09:54:12 +02:00
|
|
|
getMeLink,
|
2019-10-20 18:02:52 +02:00
|
|
|
isFetchIndexResourcesPending
|
|
|
|
|
} from "../modules/indexResource";
|
2018-07-11 14:59:01 +02:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
me: Me;
|
|
|
|
|
authenticated: boolean;
|
|
|
|
|
error: Error;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
links: Links;
|
|
|
|
|
meLink: string;
|
2018-07-30 15:29:23 +02:00
|
|
|
|
|
|
|
|
// dispatcher functions
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchMe: (link: string) => void;
|
2018-07-05 16:48:56 +02:00
|
|
|
};
|
2018-07-04 16:43:46 +02:00
|
|
|
|
2018-07-11 12:02:53 +02:00
|
|
|
class App extends Component<Props> {
|
|
|
|
|
componentDidMount() {
|
2018-10-18 08:25:24 +02:00
|
|
|
if (this.props.meLink) {
|
|
|
|
|
this.props.fetchMe(this.props.meLink);
|
|
|
|
|
}
|
2018-07-09 11:38:13 +02:00
|
|
|
}
|
2018-07-12 11:33:41 +02:00
|
|
|
|
2018-07-04 16:43:46 +02:00
|
|
|
render() {
|
2019-10-17 09:33:54 +02:00
|
|
|
const { me, loading, error, authenticated, links, t } = this.props;
|
2018-07-11 14:59:01 +02:00
|
|
|
|
2018-07-12 08:23:24 +02:00
|
|
|
let content;
|
2019-10-20 18:02:52 +02:00
|
|
|
const navigation = authenticated ? <PrimaryNavigation links={links} /> : "";
|
2018-07-11 14:59:01 +02:00
|
|
|
|
2018-07-11 12:02:53 +02:00
|
|
|
if (loading) {
|
2018-07-12 08:23:24 +02:00
|
|
|
content = <Loading />;
|
2018-07-11 22:01:36 +02:00
|
|
|
} else if (error) {
|
2019-10-21 10:57:56 +02:00
|
|
|
content = <ErrorPage title={t("app.error.title")} subtitle={t("app.error.subtitle")} error={error} />;
|
2018-07-05 16:48:56 +02:00
|
|
|
} else {
|
2019-01-11 13:10:15 +01:00
|
|
|
content = <Main authenticated={authenticated} links={links} />;
|
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-12 08:23:24 +02:00
|
|
|
{content}
|
2019-10-17 09:33:54 +02:00
|
|
|
{authenticated && <Footer me={me} />}
|
2018-07-11 14:59:01 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:57:11 +02:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2018-07-09 11:38:13 +02:00
|
|
|
return {
|
2019-10-20 18:02:52 +02:00
|
|
|
fetchMe: (link: string) => dispatch(fetchMe(link))
|
2018-07-09 11:38:13 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
const mapStateToProps = (state: any) => {
|
2018-07-30 15:29:23 +02:00
|
|
|
const authenticated = isAuthenticated(state);
|
|
|
|
|
const me = getMe(state);
|
2019-10-21 10:57:56 +02:00
|
|
|
const loading = isFetchMePending(state) || isFetchIndexResourcesPending(state);
|
|
|
|
|
const error = getFetchMeFailure(state) || getFetchIndexResourcesFailure(state);
|
2019-01-02 11:53:51 +01:00
|
|
|
const links = getLinks(state);
|
2018-10-11 09:54:12 +02:00
|
|
|
const meLink = getMeLink(state);
|
2018-07-24 13:02:50 +02:00
|
|
|
return {
|
2018-07-30 15:29:23 +02:00
|
|
|
authenticated,
|
|
|
|
|
me,
|
|
|
|
|
loading,
|
2018-10-05 13:48:21 +02:00
|
|
|
error,
|
2019-01-02 11:53:51 +01:00
|
|
|
links,
|
2019-10-20 18:02:52 +02:00
|
|
|
meLink
|
2018-07-24 13:02:50 +02:00
|
|
|
};
|
2018-07-09 11:38:13 +02:00
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
export default compose(withRouter, connect(mapStateToProps, mapDispatchToProps), withTranslation("commons"))(App);
|