Files
SCM-Manager/scm-ui/src/config/roles/containers/SingleRepositoryRole.js

139 lines
3.3 KiB
JavaScript
Raw Normal View History

//@flow
import React from "react";
import { connect } from "react-redux";
2019-05-15 17:30:42 +02:00
import { Loading, ErrorPage, Title } from "@scm-manager/ui-components";
import { Route } from "react-router";
import type { History } from "history";
2019-05-15 17:30:42 +02:00
import { EditRepositoryRoleNavLink } from "../../components/navLinks";
import { translate } from "react-i18next";
import type { Role } from "@scm-manager/ui-types";
2019-05-15 16:41:14 +02:00
import { getRepositoryRolesLink } from "../../../modules/indexResource";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
import {
fetchRoleByName,
getFetchRoleFailure,
getRoleByName,
isFetchRolePending
} from "../modules/roles";
import { withRouter } from "react-router-dom";
import PermissionRoleDetail from "../components/PermissionRoleDetails";
2019-05-15 17:30:42 +02:00
import EditRepositoryRole from "./EditRepositoryRole";
import Switch from "react-router-dom/es/Switch";
type Props = {
2019-05-15 15:21:43 +02:00
roleName: string,
role: Role,
loading: boolean,
error: Error,
repositoryRolesLink: string,
2019-05-15 14:48:12 +02:00
disabled: boolean,
// dispatcher function
fetchRoleByName: (string, string) => void,
// context objects
t: string => string,
match: any,
history: History
};
class SingleRepositoryRole extends React.Component<Props> {
componentDidMount() {
2019-05-15 16:41:14 +02:00
this.props.fetchRoleByName(
this.props.repositoryRolesLink,
this.props.roleName
);
console.log(this.props.match);
}
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
return url.substring(0, url.length - 2);
}
return url;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.match.url);
};
render() {
const { t, loading, error, role } = this.props;
if (error) {
return (
<ErrorPage
title={t("singleUser.errorTitle")}
subtitle={t("singleUser.errorSubtitle")}
error={error}
/>
);
}
if (!role || loading) {
return <Loading />;
}
const url = this.matchedUrl();
const extensionProps = {
role,
url
};
return (
2019-05-15 16:41:14 +02:00
<>
2019-05-15 17:30:42 +02:00
<Title title={t("repositoryRoles.title")} />
<div className="columns">
<div className="column is-three-quarters">
2019-05-15 17:30:42 +02:00
<Route
path={url}
component={() => <PermissionRoleDetail role={role} url={url} />}
/>
<Route
path={`${url}`}
exact
component={() => <EditRepositoryRole role={role} />}
/>
<ExtensionPoint
name="roles.route"
props={extensionProps}
renderAll={true}
/>
</div>
</div>
2019-05-15 16:41:14 +02:00
</>
);
}
}
const mapStateToProps = (state, ownProps) => {
2019-05-15 15:21:43 +02:00
const roleName = ownProps.match.params.role;
const role = getRoleByName(state, roleName);
const loading = isFetchRolePending(state, roleName);
const error = getFetchRoleFailure(state, roleName);
const repositoryRolesLink = getRepositoryRolesLink(state);
return {
repositoryRolesLink,
2019-05-15 15:21:43 +02:00
roleName,
role,
loading,
error
};
};
const mapDispatchToProps = dispatch => {
return {
fetchRoleByName: (link: string, name: string) => {
dispatch(fetchRoleByName(link, name));
}
};
};
2019-05-15 16:41:14 +02:00
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(translate("config")(SingleRepositoryRole))
);