2018-07-31 14:44:52 +02:00
|
|
|
//@flow
|
2018-07-31 18:44:01 +02:00
|
|
|
import React from "react";
|
|
|
|
|
|
|
|
|
|
import InputField from "../../components/forms/InputField";
|
|
|
|
|
import { SubmitButton } from "../../components/buttons";
|
|
|
|
|
import { translate } from "react-i18next";
|
|
|
|
|
import type { Group } from "../types/Group";
|
2018-08-01 15:54:32 +02:00
|
|
|
import * as validator from "./groupValidation";
|
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,
|
|
|
|
|
loading?: boolean
|
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-01 15:54:32 +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-07-31 18:44:01 +02:00
|
|
|
onSubmit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.props.submitForm(this.state.group);
|
|
|
|
|
};
|
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-01 15:54:32 +02:00
|
|
|
return !(this.state.nameValidationError || group.name);
|
|
|
|
|
};
|
2018-07-31 16:39:07 +02:00
|
|
|
|
|
|
|
|
submit = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
2018-08-01 13:40:54 +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-07-31 14:44:52 +02:00
|
|
|
render() {
|
2018-08-01 16:28:06 +02:00
|
|
|
const { t, loading } = this.props;
|
2018-07-31 14:44:52 +02:00
|
|
|
return (
|
2018-07-31 18:44:01 +02:00
|
|
|
<form onSubmit={this.onSubmit}>
|
|
|
|
|
<InputField
|
|
|
|
|
label={t("group.name")}
|
2018-08-01 13:40:54 +02:00
|
|
|
errorMessage="group name invalid"
|
2018-07-31 18:44:01 +02:00
|
|
|
onChange={this.handleGroupNameChange}
|
2018-08-01 13:40:54 +02:00
|
|
|
validationError={this.state.nameValidationError}
|
2018-07-31 16:39:07 +02:00
|
|
|
/>
|
2018-07-31 18:44:01 +02:00
|
|
|
<InputField
|
|
|
|
|
label={t("group.description")}
|
|
|
|
|
errorMessage=""
|
|
|
|
|
onChange={this.handleDescriptionChange}
|
2018-08-01 15:54:32 +02:00
|
|
|
validationError={false}
|
2018-07-31 18:44:01 +02:00
|
|
|
/>
|
2018-08-01 16:28:06 +02:00
|
|
|
<SubmitButton 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-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);
|