Files
SCM-Manager/scm-ui/src/permissions/containers/Permissions.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-08-21 15:37:45 +02:00
//@flow
import React from "react";
import connect from "react-redux/es/connect/connect";
import { translate } from "react-i18next";
import {
fetchPermissions,
getFetchPermissionsFailure,
isFetchPermissionsPending,
getPermissionsOfRepo,
hasCreatePermission
} from "../modules/permissions";
import Loading from "../../components/Loading";
import ErrorPage from "../../components/ErrorPage";
2018-08-23 11:49:10 +02:00
import type { PermissionCollection } from "../types/Permissions";
2018-08-28 13:47:50 +02:00
import SinglePermission from "./SinglePermission";
import CreatePermissionForm from "../components/CreatePermissionForm";
2018-08-21 15:37:45 +02:00
type Props = {
namespace: string,
name: string,
loading: boolean,
error: Error,
2018-08-23 11:11:40 +02:00
permissions: PermissionCollection,
createPermission: boolean,
//dispatch functions
fetchPermissions: (namespace: string, name: string) => void,
// context props
t: string => string,
match: any
};
2018-08-21 15:37:45 +02:00
class Permissions extends React.Component<Props> {
componentDidMount() {
const { fetchPermissions, namespace, name } = this.props;
fetchPermissions(namespace, name);
}
2018-08-21 15:37:45 +02:00
render() {
const {
loading,
error,
permissions,
t,
namespace,
name,
createPermission
} = this.props;
if (error) {
return (
<ErrorPage
title={t("permissions.error-title")}
subtitle={t("permissions.error-subtitle")}
error={error}
/>
);
}
if (loading || !permissions) {
return <Loading />;
}
const createPermissionForm = createPermission ? (
<CreatePermissionForm />
) : null;
2018-08-23 11:49:10 +02:00
if (permissions.length > 0)
2018-08-28 13:39:32 +02:00
return (
<div>
<table className="table is-hoverable is-fullwidth">
<thead>
<tr>
<th>{t("permission.name")}</th>
<th className="is-hidden-mobile">{t("permission.type")}</th>
<th>{t("permission.group-permission")}</th>
</tr>
</thead>
<tbody>
{permissions.map((permission, index) => {
return (
<SinglePermission
key={index}
namespace={namespace}
name={name}
permission={permission}
/>
);
})}
</tbody>
</table>
{createPermissionForm}
</div>
2018-08-28 13:39:32 +02:00
);
2018-08-23 11:49:10 +02:00
return <div />;
2018-08-21 15:37:45 +02:00
}
}
const mapStateToProps = (state, ownProps) => {
const namespace = ownProps.namespace;
const name = ownProps.name;
const error = getFetchPermissionsFailure(state, namespace, name);
const loading = isFetchPermissionsPending(state, namespace, name);
const permissions = getPermissionsOfRepo(state, namespace, name);
const createPermission = hasCreatePermission(state, namespace, name);
return {
namespace,
name,
error,
loading,
permissions,
createPermission
};
};
const mapDispatchToProps = dispatch => {
return {
fetchPermissions: (namespace: string, name: string) => {
dispatch(fetchPermissions(namespace, name));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("permissions")(Permissions));