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";
|
2018-08-02 16:09:25 +02:00
|
|
|
import { SubmitButton, Button } from "../../components/buttons";
|
2018-07-31 18:44:01 +02:00
|
|
|
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,
|
2018-08-02 11:38:08 +02:00
|
|
|
loading?: boolean,
|
|
|
|
|
group?: Group
|
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-02 16:09:25 +02:00
|
|
|
userToAdd: string,
|
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-02 16:09:25 +02:00
|
|
|
nameValidationError: false,
|
|
|
|
|
userToAdd: ""
|
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() {
|
|
|
|
|
const { group } = this.props
|
|
|
|
|
if (group) {
|
|
|
|
|
this.setState({group: {...group}})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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-02 12:06:30 +02:00
|
|
|
return !(
|
|
|
|
|
this.state.nameValidationError ||
|
|
|
|
|
this.isFalsy(group.name) ||
|
|
|
|
|
this.isFalsy(group.description)
|
|
|
|
|
);
|
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-02 16:09:25 +02:00
|
|
|
render() {
|
2018-08-01 16:28:06 +02:00
|
|
|
const { t, loading } = this.props;
|
2018-08-02 13:47:57 +02:00
|
|
|
const group = this.state.group
|
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-02 11:38:08 +02:00
|
|
|
label={t("group.name")}
|
2018-08-02 12:06:30 +02:00
|
|
|
errorMessage={t("group-form.name-error")}
|
2018-08-02 11:38:08 +02:00
|
|
|
onChange={this.handleGroupNameChange}
|
|
|
|
|
value={group.name}
|
|
|
|
|
validationError={this.state.nameValidationError}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-08-02 13:50:13 +02:00
|
|
|
|
2018-08-02 11:38:08 +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-07-31 18:44:01 +02:00
|
|
|
<InputField
|
|
|
|
|
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-07-31 18:44:01 +02:00
|
|
|
/>
|
2018-08-02 16:09:25 +02:00
|
|
|
<label className="label">{t("group.members")}</label>
|
|
|
|
|
<table className="table is-hoverable is-fullwidth">
|
|
|
|
|
<tbody>
|
|
|
|
|
{this.state.group.members.map((user, index) => {
|
|
|
|
|
return <tr key={user}>
|
|
|
|
|
<td key={user}>{user}</td>
|
|
|
|
|
<td><Button label="Remove" action={this.removeUser.bind(this, user)} key={user}/></td>
|
|
|
|
|
</tr>
|
|
|
|
|
})}
|
|
|
|
|
</tbody>
|
|
|
|
|
</table>
|
|
|
|
|
<InputField
|
|
|
|
|
label="Add user"
|
|
|
|
|
errorMessage="Error"
|
|
|
|
|
onChange={this.handleAddUserChange}
|
|
|
|
|
validationError={false}
|
|
|
|
|
value={this.state.userToAdd}/>
|
|
|
|
|
|
|
|
|
|
<Button label="Add user" action={this.addUserClick} />
|
|
|
|
|
|
2018-08-02 12:06:30 +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-02 16:09:25 +02:00
|
|
|
removeUser(user: string, event: Event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
let newMembers = this.state.group.members.filter(name => name !== user)
|
|
|
|
|
this.setState({...this.state, group: {
|
|
|
|
|
...this.state.group,
|
|
|
|
|
members: newMembers}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
addUserClick = (event: Event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
userToAdd: "",
|
|
|
|
|
group: {
|
|
|
|
|
...this.state.group,
|
|
|
|
|
members: [...this.state.group.members, this.state.userToAdd]}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
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-08-02 16:09:25 +02:00
|
|
|
|
|
|
|
|
handleAddUserChange = (username: string) => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
userToAdd: username
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-07-31 14:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 18:44:01 +02:00
|
|
|
export default translate("groups")(GroupForm);
|