2018-07-31 14:44:52 +02:00
|
|
|
//@flow
|
2018-07-31 18:44:01 +02:00
|
|
|
import React from "react";
|
2018-11-19 10:31:01 +01:00
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import { InputField, SubmitButton, Textarea } from "@scm-manager/ui-components";
|
|
|
|
|
import type { Group } from "@scm-manager/ui-types";
|
2018-09-05 14:32:49 +02:00
|
|
|
|
2018-08-01 15:54:32 +02:00
|
|
|
import * as validator from "./groupValidation";
|
2018-08-07 15:51:02 +02:00
|
|
|
import MemberNameTable from "./MemberNameTable";
|
2018-11-15 16:58:40 +01:00
|
|
|
import AutocompleteAddEntryToTableField from "./AutocompleteAddEntryToTableField";
|
2018-11-21 18:08:21 +01:00
|
|
|
import type { SelectValue } from "../../containers/Autocomplete";
|
2018-07-31 14:44:52 +02:00
|
|
|
|
2018-08-01 13:40:54 +02:00
|
|
|
type Props = {
|
|
|
|
|
t: string => string,
|
2018-08-01 16:28:06 +02:00
|
|
|
submitForm: Group => void,
|
2018-08-02 11:38:08 +02:00
|
|
|
loading?: boolean,
|
2018-11-15 16:58:40 +01:00
|
|
|
group?: Group,
|
|
|
|
|
loadUserSuggestions: string => any
|
2018-08-01 15:54:32 +02:00
|
|
|
};
|
2018-07-31 14:44:52 +02:00
|
|
|
|
2018-08-01 13:40:54 +02:00
|
|
|
type State = {
|
|
|
|
|
group: Group,
|
2018-08-01 15:54:32 +02:00
|
|
|
nameValidationError: boolean
|
|
|
|
|
};
|
2018-07-31 14:44:52 +02:00
|
|
|
|
|
|
|
|
class GroupForm extends React.Component<Props, State> {
|
2018-07-31 18:44:01 +02:00
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
2018-07-31 19:05:06 +02:00
|
|
|
this.state = {
|
|
|
|
|
group: {
|
|
|
|
|
name: "",
|
|
|
|
|
description: "",
|
|
|
|
|
_embedded: {
|
|
|
|
|
members: []
|
|
|
|
|
},
|
|
|
|
|
_links: {},
|
|
|
|
|
members: [],
|
2018-08-01 15:54:32 +02:00
|
|
|
type: ""
|
2018-08-01 13:40:54 +02:00
|
|
|
},
|
2018-08-03 10:37:56 +02:00
|
|
|
nameValidationError: false
|
2018-07-31 19:05:06 +02:00
|
|
|
};
|
2018-07-31 18:44:01 +02:00
|
|
|
}
|
2018-08-01 13:40:54 +02:00
|
|
|
|
2018-08-02 11:38:08 +02:00
|
|
|
componentDidMount() {
|
2018-08-03 10:37:56 +02:00
|
|
|
const { group } = this.props;
|
2018-08-02 11:38:08 +02:00
|
|
|
if (group) {
|
2018-08-03 14:21:07 +02:00
|
|
|
this.setState({ ...this.state, group: { ...group } });
|
2018-08-02 11:38:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 12:06:30 +02:00
|
|
|
isFalsy(value) {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-07-31 14:44:52 +02:00
|
|
|
|
2018-07-31 16:39:07 +02:00
|
|
|
isValid = () => {
|
2018-08-01 13:40:54 +02:00
|
|
|
const group = this.state.group;
|
2018-08-08 13:44:14 +02:00
|
|
|
return !(this.state.nameValidationError || this.isFalsy(group.name));
|
2018-08-01 15:54:32 +02:00
|
|
|
};
|
2018-07-31 16:39:07 +02:00
|
|
|
|
|
|
|
|
submit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
2018-08-02 12:06:30 +02:00
|
|
|
if (this.isValid()) {
|
2018-08-01 15:54:32 +02:00
|
|
|
this.props.submitForm(this.state.group);
|
2018-08-01 13:40:54 +02:00
|
|
|
}
|
2018-08-01 15:54:32 +02:00
|
|
|
};
|
2018-07-31 16:39:07 +02:00
|
|
|
|
2018-08-03 10:37:56 +02:00
|
|
|
render() {
|
2018-08-01 16:28:06 +02:00
|
|
|
const { t, loading } = this.props;
|
2018-11-22 10:20:18 +01:00
|
|
|
const { group } = this.state;
|
2018-08-02 11:38:08 +02:00
|
|
|
let nameField = null;
|
|
|
|
|
if (!this.props.group) {
|
|
|
|
|
nameField = (
|
2018-07-31 18:44:01 +02:00
|
|
|
<InputField
|
2018-08-03 10:37:56 +02:00
|
|
|
label={t("group.name")}
|
|
|
|
|
errorMessage={t("group-form.name-error")}
|
|
|
|
|
onChange={this.handleGroupNameChange}
|
|
|
|
|
value={group.name}
|
|
|
|
|
validationError={this.state.nameValidationError}
|
2018-10-02 10:26:19 +02:00
|
|
|
helpText={t("group-form.help.nameHelpText")}
|
2018-08-03 10:37:56 +02:00
|
|
|
/>
|
2018-08-02 11:38:08 +02:00
|
|
|
);
|
|
|
|
|
}
|
2018-08-02 13:50:13 +02:00
|
|
|
|
2018-08-03 10:37:56 +02:00
|
|
|
return (
|
2018-08-02 12:06:30 +02:00
|
|
|
<form onSubmit={this.submit}>
|
2018-08-02 11:38:08 +02:00
|
|
|
{nameField}
|
2018-08-08 15:27:03 +02:00
|
|
|
<Textarea
|
2018-07-31 18:44:01 +02:00
|
|
|
label={t("group.description")}
|
2018-08-02 12:06:30 +02:00
|
|
|
errorMessage={t("group-form.description-error")}
|
2018-07-31 18:44:01 +02:00
|
|
|
onChange={this.handleDescriptionChange}
|
2018-08-02 11:38:08 +02:00
|
|
|
value={group.description}
|
2018-08-01 15:54:32 +02:00
|
|
|
validationError={false}
|
2018-10-02 10:26:19 +02:00
|
|
|
helpText={t("group-form.help.descriptionHelpText")}
|
2018-07-31 18:44:01 +02:00
|
|
|
/>
|
2018-08-07 15:51:02 +02:00
|
|
|
<MemberNameTable
|
2018-11-21 18:08:21 +01:00
|
|
|
members={group.members}
|
2018-08-07 15:51:02 +02:00
|
|
|
memberListChanged={this.memberListChanged}
|
2018-08-03 14:21:07 +02:00
|
|
|
/>
|
2018-11-15 16:58:40 +01:00
|
|
|
|
|
|
|
|
<AutocompleteAddEntryToTableField
|
2018-08-21 11:59:12 +02:00
|
|
|
addEntry={this.addMember}
|
|
|
|
|
disabled={false}
|
|
|
|
|
buttonLabel={t("add-member-button.label")}
|
|
|
|
|
fieldLabel={t("add-member-textfield.label")}
|
|
|
|
|
errorMessage={t("add-member-textfield.error")}
|
2018-11-15 16:58:40 +01:00
|
|
|
loadSuggestions={this.props.loadUserSuggestions}
|
2018-08-21 11:59:12 +02:00
|
|
|
/>
|
2018-08-03 10:37:56 +02:00
|
|
|
<SubmitButton
|
|
|
|
|
disabled={!this.isValid()}
|
|
|
|
|
label={t("group-form.submit")}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
2018-07-31 14:44:52 +02:00
|
|
|
</form>
|
2018-07-31 18:44:01 +02:00
|
|
|
);
|
2018-07-31 14:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-07 15:51:02 +02:00
|
|
|
memberListChanged = membernames => {
|
2018-08-03 10:37:56 +02:00
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
group: {
|
|
|
|
|
...this.state.group,
|
2018-08-07 15:51:02 +02:00
|
|
|
members: membernames
|
2018-08-03 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-06 13:55:54 +02:00
|
|
|
};
|
2018-08-03 14:21:07 +02:00
|
|
|
|
2018-11-21 18:08:21 +01:00
|
|
|
addMember = (value: SelectValue) => {
|
|
|
|
|
if (this.isMember(value.value.id)) {
|
2018-08-03 10:37:56 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2018-08-02 16:09:25 +02:00
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
group: {
|
|
|
|
|
...this.state.group,
|
2018-11-21 18:08:21 +01:00
|
|
|
members: [...this.state.group.members, value.value.id]
|
2018-08-03 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
});
|
2018-08-03 14:21:07 +02:00
|
|
|
};
|
2018-08-03 10:37:56 +02:00
|
|
|
|
2018-08-07 15:51:02 +02:00
|
|
|
isMember = (membername: string) => {
|
|
|
|
|
return this.state.group.members.includes(membername);
|
2018-08-03 14:21:07 +02:00
|
|
|
};
|
2018-08-02 16:09:25 +02:00
|
|
|
|
2018-07-31 18:44:01 +02:00
|
|
|
handleGroupNameChange = (name: string) => {
|
|
|
|
|
this.setState({
|
2018-08-01 13:40:54 +02:00
|
|
|
nameValidationError: !validator.isNameValid(name),
|
2018-08-01 15:54:32 +02:00
|
|
|
group: { ...this.state.group, name }
|
2018-07-31 18:44:01 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleDescriptionChange = (description: string) => {
|
|
|
|
|
this.setState({
|
2018-08-01 15:54:32 +02:00
|
|
|
group: { ...this.state.group, description }
|
2018-07-31 18:44:01 +02:00
|
|
|
});
|
|
|
|
|
};
|
2018-07-31 14:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 18:44:01 +02:00
|
|
|
export default translate("groups")(GroupForm);
|