Files
SCM-Manager/scm-ui/src/admin/roles/containers/CreateRepositoryRole.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-05-15 14:48:12 +02:00
// @flow
import React from "react";
import RepositoryRoleForm from "./RepositoryRoleForm";
import { connect } from "react-redux";
import { translate } from "react-i18next";
2019-05-17 15:47:20 +02:00
import {ErrorNotification, Subtitle, Title} from "@scm-manager/ui-components";
2019-05-15 14:48:12 +02:00
import {
createRole,
getCreateRoleFailure,
2019-05-15 14:48:12 +02:00
getFetchVerbsFailure,
isFetchVerbsPending
} from "../modules/roles";
2019-05-16 13:36:11 +02:00
import type { RepositoryRole } from "@scm-manager/ui-types";
2019-05-15 14:48:12 +02:00
import {
getRepositoryRolesLink,
getRepositoryVerbsLink
} from "../../../modules/indexResource";
2019-05-17 15:43:48 +02:00
import type {History} from "history";
2019-05-15 14:48:12 +02:00
type Props = {
repositoryRolesLink: string,
error?: Error,
2019-05-17 15:43:48 +02:00
history: History,
2019-05-15 14:48:12 +02:00
//dispatch function
2019-05-16 13:36:11 +02:00
addRole: (link: string, role: RepositoryRole, callback?: () => void) => void,
2019-05-16 10:10:10 +02:00
// context objects
t: string => string
2019-05-15 14:48:12 +02:00
};
class CreateRepositoryRole extends React.Component<Props> {
2019-05-16 13:36:11 +02:00
repositoryRoleCreated = (role: RepositoryRole) => {
2019-05-15 14:48:12 +02:00
const { history } = this.props;
history.push("/config/role/" + role.name + "/info");
2019-05-15 14:48:12 +02:00
};
2019-05-16 13:36:11 +02:00
createRepositoryRole = (role: RepositoryRole) => {
2019-05-15 14:48:12 +02:00
this.props.addRole(this.props.repositoryRolesLink, role, () =>
this.repositoryRoleCreated(role)
);
};
render() {
const { t, error } = this.props;
if (error) {
return <ErrorNotification error={error} />;
}
2019-05-15 14:48:12 +02:00
return (
<>
2019-05-16 10:10:10 +02:00
<Title title={t("repositoryRole.title")} />
2019-05-17 15:47:20 +02:00
<Subtitle subtitle={t("repositoryRole.createSubtitle")} />
2019-05-15 14:48:12 +02:00
<RepositoryRoleForm
submitForm={role => this.createRepositoryRole(role)}
/>
</>
);
}
}
const mapStateToProps = (state, ownProps) => {
const loading = isFetchVerbsPending(state);
const error = getFetchVerbsFailure(state) || getCreateRoleFailure(state);
2019-05-15 14:48:12 +02:00
const verbsLink = getRepositoryVerbsLink(state);
const repositoryRolesLink = getRepositoryRolesLink(state);
return {
loading,
error,
verbsLink,
repositoryRolesLink
};
};
const mapDispatchToProps = dispatch => {
return {
2019-05-16 13:36:11 +02:00
addRole: (link: string, role: RepositoryRole, callback?: () => void) => {
dispatch(createRole(link, role, callback));
2019-05-15 14:48:12 +02:00
}
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
2019-06-19 09:58:17 +02:00
)(translate("admin")(CreateRepositoryRole));