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

148 lines
3.0 KiB
JavaScript
Raw Normal View History

2018-08-31 10:47:42 +02:00
// @flow
import React, { Component } from "react";
import Main from "./Main";
2018-07-09 11:38:13 +02:00
import { connect } from "react-redux";
import { translate } from "react-i18next";
import { withRouter } from "react-router-dom";
import {
fetchMe,
isAuthenticated,
getMe,
isFetchMePending,
getFetchMeFailure
} from "../modules/auth";
import {
PrimaryNavigation,
Loading,
ErrorPage,
Footer,
Header
} from "@scm-manager/ui-components";
import type { Me, Link } from "@scm-manager/ui-types";
2018-10-05 13:14:33 +02:00
import {
fetchIndexResources,
getConfigLink,
2018-10-05 13:14:33 +02:00
getFetchIndexResourcesFailure,
getGroupsLink,
getLogoutLink,
2018-10-11 09:54:12 +02:00
getMeLink,
getRepositoriesLink,
getUsersLink,
2018-10-05 13:14:33 +02:00
isFetchIndexResourcesPending
} from "../modules/indexResource";
2018-07-11 14:59:01 +02:00
type Props = {
2018-07-24 17:05:38 +02:00
me: Me,
authenticated: boolean,
2018-07-11 22:01:36 +02:00
error: Error,
loading: boolean,
2018-10-11 09:54:12 +02:00
repositoriesLink: string,
usersLink: string,
groupsLink: string,
configLink: string,
logoutLink: string,
meLink: string,
// dispatcher functions
2018-10-11 09:54:12 +02:00
fetchMe: (link: string) => void,
// context props
t: string => string
2018-07-05 16:48:56 +02:00
};
class App extends Component<Props> {
componentDidMount() {
2018-10-11 09:54:12 +02:00
if (this.props.meLink) this.props.fetchMe(this.props.meLink);
2018-07-09 11:38:13 +02:00
}
render() {
const {
me,
loading,
error,
authenticated,
t,
repositoriesLink,
usersLink,
groupsLink,
configLink,
logoutLink
} = this.props;
2018-07-11 14:59:01 +02:00
let content;
const navigation = authenticated ? (
<PrimaryNavigation
repositoriesLink={repositoriesLink}
usersLink={usersLink}
groupsLink={groupsLink}
configLink={configLink}
logoutLink={logoutLink}
/>
) : (
""
);
2018-07-11 14:59:01 +02:00
if (loading) {
content = <Loading />;
2018-07-11 22:01:36 +02:00
} else if (error) {
2018-07-12 12:57:23 +02:00
content = (
<ErrorPage
title={t("app.error.title")}
subtitle={t("app.error.subtitle")}
2018-07-12 12:57:23 +02:00
error={error}
/>
);
2018-07-05 16:48:56 +02:00
} else {
2018-07-13 10:57:11 +02:00
content = <Main authenticated={authenticated} />;
2018-07-05 16:48:56 +02:00
}
2018-07-11 14:59:01 +02:00
return (
<div className="App">
<Header>{navigation}</Header>
{content}
2018-07-24 17:05:38 +02:00
<Footer me={me} />
2018-07-11 14:59:01 +02:00
</div>
);
}
}
2018-07-13 10:57:11 +02:00
const mapDispatchToProps = (dispatch: any) => {
2018-07-09 11:38:13 +02:00
return {
2018-10-11 10:26:54 +02:00
fetchMe: (link: string) => dispatch(fetchMe(link))
2018-07-09 11:38:13 +02:00
};
};
const mapStateToProps = state => {
const authenticated = isAuthenticated(state);
const me = getMe(state);
2018-10-05 13:14:33 +02:00
const loading =
isFetchMePending(state) || isFetchIndexResourcesPending(state);
const error =
getFetchMeFailure(state) || getFetchIndexResourcesFailure(state);
const repositoriesLink = getRepositoriesLink(state);
const usersLink = getUsersLink(state);
const groupsLink = getGroupsLink(state);
const configLink = getConfigLink(state);
const logoutLink = getLogoutLink(state);
2018-10-11 09:54:12 +02:00
const meLink = getMeLink(state);
2018-07-24 13:02:50 +02:00
return {
authenticated,
me,
loading,
error,
repositoriesLink,
usersLink,
groupsLink,
configLink,
2018-10-11 09:54:12 +02:00
logoutLink,
meLink
2018-07-24 13:02:50 +02:00
};
2018-07-09 11:38:13 +02:00
};
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(translate("commons")(App))
2018-07-09 11:38:13 +02:00
);