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

181 lines
4.6 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,
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";
2018-07-25 13:21:49 +02:00
import EditUser from "./EditUser";
import type { User } from "@scm-manager/ui-types";
import type { History } from "history";
import {
2018-11-05 13:14:52 +01:00
fetchUserByName,
deleteUser,
getUserByName,
isFetchUserPending,
getFetchUserFailure,
isDeleteUserPending,
getDeleteUserFailure
} from "../modules/users";
2018-07-25 13:21:49 +02:00
2018-11-05 15:08:58 +01:00
import {
DeleteUserNavLink,
EditUserNavLink,
SetPasswordNavLink,
SetPermissionsNavLink
2018-11-05 15:08:58 +01:00
} 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";
2019-01-18 14:16:26 +01:00
import SetPermissions from "../../permissions/components/SetPermissions";
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 functions
deleteUser: (user: User, callback?: () => void) => void,
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
}
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() {
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
2018-07-26 10:19:26 +02:00
title={t("single-user.error-title")}
subtitle={t("single-user.error-subtitle")}
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}/edit`}
component={() => <EditUser user={user} />}
/>
2018-11-05 15:08:58 +01:00
<Route
path={`${url}/password`}
component={() => <SetUserPassword user={user} />}
/>
<Route
path={`${url}/permissions`}
2019-01-18 14:16:26 +01:00
component={() => (
<SetPermissions
selectedPermissionsLink={user._links.permissions}
/>
)}
/>
2018-07-25 13:21:49 +02:00
</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-11-05 15:08:58 +01:00
<SetPasswordNavLink
user={user}
passwordUrl={`${url}/password`}
/>
<SetPermissionsNavLink
user={user}
permissionsUrl={`${url}/permissions`}
/>
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;
const user = getUserByName(state, name);
const loading =
isFetchUserPending(state, name) || isDeleteUserPending(state, name);
const error =
getFetchUserFailure(state, name) || getDeleteUserFailure(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
},
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));