This commit is contained in:
Florian Scholdei
2019-05-15 02:04:54 +02:00
parent 3c526a8ac2
commit 6969003a4c
3 changed files with 65 additions and 64 deletions

View File

@@ -1,7 +1,9 @@
// @flow // @flow
import React from "react"; import React from "react";
import { InputField, SubmitButton } from "@scm-manager/ui-components"; import { connect } from "react-redux";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import type { Role } from "@scm-manager/ui-types";
import { InputField, SubmitButton } from "@scm-manager/ui-components";
import PermissionCheckbox from "../../repos/permissions/components/PermissionCheckbox"; import PermissionCheckbox from "../../repos/permissions/components/PermissionCheckbox";
import { import {
fetchAvailableVerbs, fetchAvailableVerbs,
@@ -10,15 +12,16 @@ import {
isFetchVerbsPending isFetchVerbsPending
} from "../modules/roles"; } from "../modules/roles";
import { getRepositoryVerbsLink } from "../../modules/indexResource"; import { getRepositoryVerbsLink } from "../../modules/indexResource";
import { connect } from "react-redux";
type Props = { type Props = {
submitForm: CustomRoleRequest => void, submitForm: CustomRoleRequest => void,
transmittedName?: string, role?: Role,
loading?: boolean, loading?: boolean,
availableVerbs: string[], availableVerbs: string[],
selectedVerbs: string[], selectedVerbs: string[],
verbsLink: string, verbsLink: string,
// context objects
t: string => string, t: string => string,
// dispatch functions // dispatch functions
@@ -26,7 +29,7 @@ type Props = {
}; };
type State = { type State = {
name?: string, role: Role,
verbs: string[] verbs: string[]
}; };
@@ -35,56 +38,56 @@ class GlobalPermissionRoleForm extends React.Component<Props, State> {
super(props); super(props);
this.state = { this.state = {
name: props.transmittedName ? props.transmittedName : "", role: {
name: "",
verbs: [],
system: false,
_links: {}
},
verbs: props.availableVerbs verbs: props.availableVerbs
}; };
} }
componentWillMount() {
const { fetchAvailableVerbs, verbsLink } = this.props;
fetchAvailableVerbs(verbsLink);
}
componentDidMount() { componentDidMount() {
const { const { fetchAvailableVerbs, verbsLink, role } = this.props;
fetchAvailableVerbs,
verbsLink,
} = this.props;
fetchAvailableVerbs(verbsLink); fetchAvailableVerbs(verbsLink);
if (role) {
this.setState({ role: { ...role } });
}
} }
isValid = () => {
const { name, verbs } = this.state;
return !(this.isFalsy(name) || this.isFalsy(verbs) || verbs.isEmpty());
};
isFalsy(value) { isFalsy(value) {
return !value; return !value;
} }
isValid = () => {
const { role } = this.state;
return !(
this.isFalsy(role) ||
this.isFalsy(role.name) ||
this.isFalsy(role.verbs)
);
};
handleNameChange = (name: string) => { handleNameChange = (name: string) => {
this.setState({ this.setState({
...this.state, role: {
name ...this.state.role,
name
}
}); });
}; };
handleVerbChange = (value: boolean, name: string) => { handleVerbChange = (value: boolean, name: string) => {
const { selectedVerbs } = this.state; const { selectedVerbs } = this.props;
const newVerbs = { ...selectedVerbs, [name]: value }; const newVerbs = { ...selectedVerbs, [name]: value };
this.setState({ selectedVerbs: newVerbs }); this.setState({ verbs: newVerbs });
}; };
render() { render() {
const { const { loading, availableVerbs, t } = this.props;
t, const { role, verbs } = this.state;
transmittedName,
loading,
disabled,
availableVerbs
} = this.props;
const { verbs } = this.state;
const verbSelectBoxes = const verbSelectBoxes =
!!availableVerbs && !!availableVerbs &&
@@ -99,31 +102,29 @@ class GlobalPermissionRoleForm extends React.Component<Props, State> {
)); ));
return ( return (
<div> <form onSubmit={this.submit}>
<form onSubmit={this.submit}> <div className="columns">
<div className="columns"> <div className="column">
<div className="column"> <InputField
<InputField name="name"
name="name" label={t("roles.create.name")}
label={t("roles.create.name")} onChange={this.handleNameChange}
onChange={this.handleNameChange} value={role.name ? role.name : ""}
value={name ? name : ""} disabled={!!role.name} // || disabled
disabled={!!transmittedName || disabled} />
/>
</div>
</div> </div>
<>{verbSelectBoxes}</> </div>
<div className="columns"> <>{verbSelectBoxes}</>
<div className="column"> <div className="columns">
<SubmitButton <div className="column">
disabled={disabled || !this.isValid()} <SubmitButton
loading={loading} loading={loading}
label={t("roles.create.submit")} label={t("roles.create.submit")}
/> //disabled={disabled || !this.isValid()}
</div> />
</div> </div>
</form> </div>
</div> </form>
); );
} }
} }

View File

@@ -5,14 +5,6 @@ import {withRouter} from "react-router-dom";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import type { History } from "history"; import type { History } from "history";
import type { Role, PagedCollection } from "@scm-manager/ui-types"; import type { Role, PagedCollection } from "@scm-manager/ui-types";
import {
fetchRolesByPage,
getRolesFromState,
selectListAsCollection,
isPermittedToCreateRoles,
isFetchRolesPending,
getFetchRolesFailure
} from "../modules/roles";
import { import {
Title, Title,
Loading, Loading,
@@ -21,6 +13,14 @@ import {
urls, urls,
CreateButton CreateButton
} from "@scm-manager/ui-components"; } from "@scm-manager/ui-components";
import {
fetchRolesByPage,
getRolesFromState,
selectListAsCollection,
isPermittedToCreateRoles,
isFetchRolesPending,
getFetchRolesFailure
} from "../modules/roles";
import PermissionRoleTable from "../components/table/PermissionRoleTable"; import PermissionRoleTable from "../components/table/PermissionRoleTable";
import { getRolesLink } from "../../modules/indexResource"; import { getRolesLink } from "../../modules/indexResource";
type Props = { type Props = {

View File

@@ -225,7 +225,7 @@ function verbReducer(state: any = {}, action: any = {}) {
return { return {
...state, ...state,
verbMap verbMap
}; };
default: default:
return state; return state;
} }
@@ -338,7 +338,7 @@ export function getRolesFromState(state: Object) {
} }
export function getVerbsFromState(state: Object) { export function getVerbsFromState(state: Object) {
return state.roles.verbs.verbs return state.roles.verbs.verbs;
} }
export function isFetchRolesPending(state: Object) { export function isFetchRolesPending(state: Object) {