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