Files
SCM-Manager/scm-ui/src/groups/components/GroupForm.js

162 lines
3.8 KiB
JavaScript
Raw Normal View History

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-08-01 15:54:32 +02:00
import * as validator from "./groupValidation";
import MemberNameTable from "./MemberNameTable";
import AutocompleteAddEntryToTableField from "./AutocompleteAddEntryToTableField";
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,
submitForm: Group => void,
2018-08-02 11:38:08 +02:00
loading?: boolean,
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);
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 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
}
}
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;
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();
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() {
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}
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 (
<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")}
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}
helpText={t("group-form.help.descriptionHelpText")}
2018-07-31 18:44:01 +02:00
/>
<MemberNameTable
members={group.members}
memberListChanged={this.memberListChanged}
2018-08-03 14:21:07 +02:00
/>
<AutocompleteAddEntryToTableField
addEntry={this.addMember}
disabled={false}
buttonLabel={t("add-member-button.label")}
fieldLabel={t("add-member-textfield.label")}
errorMessage={t("add-member-textfield.error")}
loadSuggestions={this.props.loadUserSuggestions}
/>
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
}
memberListChanged = membernames => {
2018-08-03 10:37:56 +02:00
this.setState({
...this.state,
group: {
...this.state.group,
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
addMember = (value: SelectValue) => {
if (this.isMember(value.value.id)) {
2018-08-03 10:37:56 +02:00
return;
}
this.setState({
...this.state,
group: {
...this.state.group,
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
isMember = (membername: string) => {
return this.state.group.members.includes(membername);
2018-08-03 14:21:07 +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);