Files
SCM-Manager/scm-ui/src/users/containers/SingleUser.js

150 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-07-25 13:21:49 +02:00
//@flow
import React from "react";
import { connect } from "react-redux";
import {
Page,
Loading,
Navigation,
2019-01-18 11:53:14 +01:00
SubNavigation,
Section,
NavLink,
ErrorPage
} from "@scm-manager/ui-components";
2018-07-25 13:21:49 +02:00
import { Route } from "react-router";
2018-07-25 15:04:19 +02:00
import { Details } from "./../components/table";
2019-01-23 09:17:53 +01:00
import GeneralUser from "./GeneralUser";
import type { User } from "@scm-manager/ui-types";
import type { History } from "history";
import {
2018-11-05 13:14:52 +01:00
fetchUserByName,
getUserByName,
isFetchUserPending,
getFetchUserFailure
} from "../modules/users";
2019-01-23 09:17:53 +01:00
import { GeneralUserNavLink, SetPasswordNavLink } from "./../components/navLinks";
2018-07-26 10:19:26 +02:00
import { translate } from "react-i18next";
2018-10-09 16:40:44 +02:00
import { getUsersLink } from "../../modules/indexResource";
2018-11-05 15:08:58 +01:00
import SetUserPassword from "../components/SetUserPassword";
2018-07-26 10:19:26 +02:00
2018-07-25 13:21:49 +02:00
type Props = {
name: string,
user: User,
loading: boolean,
error: Error,
2018-10-09 16:40:44 +02:00
usersLink: string,
// dispatcher function
2018-11-05 13:14:52 +01:00
fetchUserByName: (string, string) => void,
// context objects
t: string => string,
match: any,
history: History
2018-07-25 13:21:49 +02:00
};
class SingleUser extends React.Component<Props> {
componentDidMount() {
2018-11-05 13:14:52 +01:00
this.props.fetchUserByName(this.props.usersLink, this.props.name);
2018-07-25 13:21:49 +02:00
}
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
return url.substring(0, url.length - 2);
}
return url;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.match.url);
};
2018-07-25 13:21:49 +02:00
render() {
const { t, loading, error, user } = this.props;
2018-07-25 13:21:49 +02:00
if (error) {
2018-07-25 13:21:49 +02:00
return (
<ErrorPage
2019-01-23 11:14:30 +01:00
title={t("singleUser.errorTitle")}
subtitle={t("singleUser.errorSubtitle")}
error={error}
2018-07-25 13:21:49 +02:00
/>
);
}
if (!user || loading) {
return <Loading />;
}
const url = this.matchedUrl();
2018-07-25 13:21:49 +02:00
return (
<Page title={user.displayName}>
<div className="columns">
<div className="column is-three-quarters">
<Route path={url} exact component={() => <Details user={user} />} />
<Route
path={`${url}/settings/general`}
2019-01-23 09:17:53 +01:00
component={() => <GeneralUser user={user} />}
2018-07-25 13:21:49 +02:00
/>
2018-11-05 15:08:58 +01:00
<Route
2019-01-18 11:53:14 +01:00
path={`${url}/settings/password`}
2018-11-05 15:08:58 +01:00
component={() => <SetUserPassword user={user} />}
/>
2018-07-25 13:21:49 +02:00
</div>
<div className="column">
<Navigation>
2019-01-23 11:14:30 +01:00
<Section label={t("singleUser.menu.navigationLabel")}>
<NavLink
to={`${url}`}
2019-01-23 11:14:30 +01:00
label={t("singleUser.menu.informationNavLink")}
/>
2019-01-18 11:53:14 +01:00
<SubNavigation
to={`${url}/settings/general`}
2019-01-23 11:14:30 +01:00
label={t("singleUser.menu.settingsNavLink")}
2019-01-18 11:53:14 +01:00
>
2019-01-23 09:17:53 +01:00
<GeneralUserNavLink
user={user}
editUrl={`${url}/settings/general`}
/>
2019-01-18 11:53:14 +01:00
<SetPasswordNavLink
user={user}
passwordUrl={`${url}/settings/password`}
/>
</SubNavigation>
2018-07-25 13:21:49 +02:00
</Section>
</Navigation>
</div>
</div>
</Page>
);
}
}
const mapStateToProps = (state, ownProps) => {
const name = ownProps.match.params.name;
const user = getUserByName(state, name);
const loading = isFetchUserPending(state, name);
const error = getFetchUserFailure(state, name);
2018-10-09 16:40:44 +02:00
const usersLink = getUsersLink(state);
2018-07-25 13:21:49 +02:00
return {
2018-10-09 16:40:44 +02:00
usersLink,
2018-07-25 13:21:49 +02:00
name,
user,
loading,
error
2018-07-25 13:21:49 +02:00
};
};
const mapDispatchToProps = dispatch => {
return {
2018-11-05 13:14:52 +01:00
fetchUserByName: (link: string, name: string) => {
dispatch(fetchUserByName(link, name));
2018-07-25 13:21:49 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2018-07-26 10:19:26 +02:00
)(translate("users")(SingleUser));