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

173 lines
4.3 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 { withRouter } from "react-router-dom";
import { translate } from "react-i18next";
import {
fetchPermissions,
getFetchPermissionsFailure,
isFetchPermissionsPending,
getPermissionsOfRepo,
2018-08-30 11:33:40 +02:00
hasCreatePermission,
createPermission,
isCreatePermissionPending
} from "../modules/permissions";
import Loading from "../../components/Loading";
import ErrorPage from "../../components/ErrorPage";
import type {
PermissionCollection,
PermissionEntry
} from "../types/Permissions";
2018-08-28 13:47:50 +02:00
import SinglePermission from "./SinglePermission";
import CreatePermissionForm from "../components/CreatePermissionForm";
import type { History } from "history";
2018-08-21 15:37:45 +02:00
type Props = {
namespace: string,
repoName: string,
loading: boolean,
error: Error,
2018-08-23 11:11:40 +02:00
permissions: PermissionCollection,
hasPermissionToCreate: boolean,
loadingCreatePermission: boolean,
//dispatch functions
fetchPermissions: (namespace: string, repoName: string) => void,
2018-08-30 11:33:40 +02:00
createPermission: (
permission: PermissionEntry,
2018-08-30 11:33:40 +02:00
namespace: string,
repoName: string,
callback?: () => void
2018-08-30 11:33:40 +02:00
) => void,
// context props
t: string => string,
match: any,
history: History
};
2018-08-21 15:37:45 +02:00
class Permissions extends React.Component<Props> {
componentDidMount() {
const { fetchPermissions, namespace, repoName } = this.props;
fetchPermissions(namespace, repoName);
}
permissionCreated = () => {
const { history, namespace, repoName } = this.props;
history.push(`/repo/${namespace}/${repoName}/permissions`);
};
2018-08-21 15:37:45 +02:00
render() {
const {
loading,
error,
permissions,
t,
namespace,
repoName,
loadingCreatePermission,
hasPermissionToCreate
} = this.props;
console.log(permissions);
if (error) {
return (
<ErrorPage
title={t("permissions.error-title")}
subtitle={t("permissions.error-subtitle")}
error={error}
/>
);
}
if (loading || !permissions) {
return <Loading />;
}
const createPermissionForm = hasPermissionToCreate ? (
2018-08-30 11:33:40 +02:00
<CreatePermissionForm
createPermission={permission =>
this.props.createPermission(
permission,
namespace,
repoName,
this.permissionCreated()
)
2018-08-30 11:33:40 +02:00
}
loading={loadingCreatePermission}
2018-08-30 11:33:40 +02:00
/>
) : null;
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 => {
return (
<SinglePermission
key={permission.name}
namespace={namespace}
repoName={repoName}
permission={permission}
/>
);
})}
</tbody>
</table>
{createPermissionForm}
</div>
);
2018-08-21 15:37:45 +02:00
}
}
const mapStateToProps = (state, ownProps) => {
const namespace = ownProps.namespace;
const repoName = ownProps.repoName;
const error = getFetchPermissionsFailure(state, namespace, repoName);
const loading = isFetchPermissionsPending(state, namespace, repoName);
const permissions = getPermissionsOfRepo(state, namespace, repoName);
const loadingCreatePermission = isCreatePermissionPending(
state,
namespace,
repoName
);
const hasPermissionToCreate = hasCreatePermission(state, namespace, repoName);
return {
namespace,
repoName,
error,
loading,
permissions,
hasPermissionToCreate,
loadingCreatePermission
};
};
const mapDispatchToProps = dispatch => {
return {
fetchPermissions: (namespace: string, repoName: string) => {
dispatch(fetchPermissions(namespace, repoName));
2018-08-30 11:33:40 +02:00
},
createPermission: (
permission: PermissionEntry,
2018-08-30 11:33:40 +02:00
namespace: string,
repoName: string,
callback?: () => void
2018-08-30 11:33:40 +02:00
) => {
dispatch(createPermission(permission, namespace, repoName, callback));
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(translate("permissions")(withRouter(Permissions)));