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

130 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-07-25 13:21:49 +02:00
//@flow
import React from "react";
import { connect } from "react-redux";
2018-07-25 14:29:43 +02:00
import { Page } from "../../components/layout";
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";
2018-07-25 13:21:49 +02:00
import EditUser from "./EditUser";
import type { User } from "../types/User";
import type { UserEntry } from "../types/UserEntry";
import type { History } from "history";
2018-07-25 13:21:49 +02:00
import { fetchUser, deleteUser } from "../modules/users";
import Loading from "../../components/Loading";
2018-07-25 14:29:43 +02:00
import { Navigation, Section, NavLink } from "../../components/navigation";
2018-07-26 09:49:44 +02:00
import { DeleteUserNavLink, EditUserNavLink } from "./../components/navLinks";
2018-07-25 13:21:49 +02:00
import ErrorPage from "../../components/ErrorPage";
2018-07-26 10:19:26 +02:00
import { translate } from "react-i18next";
2018-07-25 13:21:49 +02:00
type Props = {
2018-07-26 10:19:26 +02:00
t: string => string,
2018-07-25 13:21:49 +02:00
name: string,
userEntry?: UserEntry,
match: any,
deleteUser: (user: User, callback?: () => void) => void,
fetchUser: string => void,
history: History
2018-07-25 13:21:49 +02:00
};
class SingleUser extends React.Component<Props> {
componentDidMount() {
this.props.fetchUser(this.props.name);
}
userDeleted = () => {
this.props.history.push("/users");
};
deleteUser = (user: User) => {
this.props.deleteUser(user, this.userDeleted);
};
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() {
2018-07-26 10:19:26 +02:00
const { t, userEntry } = this.props;
2018-07-25 13:21:49 +02:00
if (!userEntry || userEntry.loading) {
return <Loading />;
}
if (userEntry.error) {
return (
<ErrorPage
2018-07-26 10:19:26 +02:00
title={t("single-user.error-title")}
subtitle={t("single-user.error-subtitle")}
2018-07-25 13:21:49 +02:00
error={userEntry.error}
/>
);
}
const user = userEntry.entry;
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}/edit`}
component={() => <EditUser user={user} />}
/>
</div>
<div className="column">
<Navigation>
2018-07-26 10:19:26 +02:00
<Section label={t("single-user.navigation-label")}>
<NavLink to={`${url}`} label={t("single-user.information-label")} />
2018-07-26 09:49:44 +02:00
<EditUserNavLink user={user} editUrl={`${url}/edit`} />
2018-07-25 13:21:49 +02:00
</Section>
2018-07-26 10:19:26 +02:00
<Section label={t("single-user.actions-label")}>
2018-07-26 09:49:44 +02:00
<DeleteUserNavLink user={user} deleteUser={this.deleteUser} />
2018-07-26 10:19:26 +02:00
<NavLink to="/users" label={t("single-user.back-label")} />
2018-07-25 13:21:49 +02:00
</Section>
</Navigation>
</div>
</div>
</Page>
);
}
}
const mapStateToProps = (state, ownProps) => {
const name = ownProps.match.params.name;
let userEntry;
if (state.users && state.users.byNames) {
userEntry = state.users.byNames[name];
2018-07-25 13:21:49 +02:00
}
return {
name,
userEntry
};
};
const mapDispatchToProps = dispatch => {
return {
fetchUser: (name: string) => {
dispatch(fetchUser(name));
},
deleteUser: (user: User, callback?: () => void) => {
dispatch(deleteUser(user, callback));
2018-07-25 13:21:49 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2018-07-26 10:19:26 +02:00
)(translate("users")(SingleUser));