Files
SCM-Manager/scm-ui/ui-webapp/src/admin/roles/containers/SingleRepositoryRole.tsx

133 lines
3.1 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { connect } from 'react-redux';
import { Loading, ErrorPage, Title } from '@scm-manager/ui-components';
import { Route } from 'react-router-dom';
import { History } from 'history';
import { translate } from 'react-i18next';
import { RepositoryRole } from '@scm-manager/ui-types';
import { getRepositoryRolesLink } from '../../../modules/indexResource';
import { ExtensionPoint } from '@scm-manager/ui-extensions';
2019-05-15 16:41:14 +02:00
import {
fetchRoleByName,
getFetchRoleFailure,
getRoleByName,
isFetchRolePending,
} from '../modules/roles';
import { withRouter } from 'react-router-dom';
import PermissionRoleDetail from '../components/PermissionRoleDetails';
import EditRepositoryRole from './EditRepositoryRole';
type Props = {
roleName: string;
role: RepositoryRole;
loading: boolean;
error: Error;
repositoryRolesLink: string;
disabled: boolean;
// dispatcher function
fetchRoleByName: (p1: string, p2: string) => void;
// context objects
t: (p: 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,
2019-05-15 16:41:14 +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);
};
render() {
const { t, loading, error, role } = this.props;
if (error) {
return (
<ErrorPage
title={t('repositoryRole.errorTitle')}
subtitle={t('repositoryRole.errorSubtitle')}
error={error}
/>
2019-05-16 10:01:33 +02:00
);
}
if (!role || loading) {
return <Loading />;
}
const url = this.matchedUrl();
const extensionProps = {
role,
url,
};
return (
2019-05-15 16:41:14 +02:00
<>
<Title title={t('repositoryRole.title')} />
2019-05-17 15:51:55 +02:00
<Route
path={`${url}/info`}
component={() => <PermissionRoleDetail role={role} url={url} />}
/>
<Route
path={`${url}/edit`}
exact
component={() => (
<EditRepositoryRole role={role} history={this.props.history} />
)}
/>
<ExtensionPoint
name="roles.route"
props={extensionProps}
renderAll={true}
/>
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('admin')(SingleRepositoryRole)),
2019-05-15 16:41:14 +02:00
);