diff --git a/scm-ui/src/containers/AsyncAutocomplete.js b/scm-ui/src/containers/AsyncAutocomplete.js index ee4ab86590..17701fbb9d 100644 --- a/scm-ui/src/containers/AsyncAutocomplete.js +++ b/scm-ui/src/containers/AsyncAutocomplete.js @@ -1,6 +1,7 @@ // @flow import React from "react"; import AsyncSelect from "react-select/lib/Async"; +import {LabelWithHelpIcon} from "@scm-manager/ui-components"; type SelectionResult = { id: string, @@ -13,42 +14,39 @@ type SelectValue = { }; type Props = { - url: string, - loadOptions: string => Promise, - valueSelected: SelectionResult => void + loadSuggestions: string => Promise, + valueSelected: SelectionResult => void, + label: string, + helpText?: string, + value?: any }; type State = { value: SelectionResult }; -const URL_QUERY_SUFFIX: string = "?q="; - class AsyncAutocomplete extends React.Component { - getOptions = (inputValue: string) => { - const { url } = this.props; - return fetch(url + URL_QUERY_SUFFIX + inputValue) - .then(response => response.json()) - .then(json => { - return json.map(element => { - return { value: element, label: element.displayName }; - }); - }); - }; - handleInputChange = (newValue: SelectValue) => { this.setState({ value: newValue.value }); - return newValue.value; + this.props.valueSelected(newValue.value); }; render() { + const { label, helpText, value } = this.props; + const stringValue = value ? value.id : ""; return ( - +
+ +
+ +
+
); } } diff --git a/scm-ui/src/groups/components/AutocompleteAddEntryToTableField.js b/scm-ui/src/groups/components/AutocompleteAddEntryToTableField.js new file mode 100644 index 0000000000..37cf11e32c --- /dev/null +++ b/scm-ui/src/groups/components/AutocompleteAddEntryToTableField.js @@ -0,0 +1,71 @@ +//@flow +import React from "react"; + +import { AddButton } from "@scm-manager/ui-components"; +import AsyncAutocomplete from "../../containers/AsyncAutocomplete"; + +type Props = { + addEntry: string => void, + disabled: boolean, + buttonLabel: string, + fieldLabel: string, + helpText?: string, + loadSuggestions: string => any //TODO: type +}; + +type State = { + entryToAdd: any // TODO: type +}; + +class AutocompleteAddEntryToTableField extends React.Component { + constructor(props: Props) { + super(props); + this.state = { + entryToAdd: undefined + }; + } + + render() { + const { disabled, buttonLabel, fieldLabel, helpText } = this.props; + + const { entryToAdd } = this.state; + + return ( +
+ + + +
+ ); + } + + addButtonClicked = (event: Event) => { + event.preventDefault(); + this.appendEntry(); + }; + + appendEntry = () => { + const { entryToAdd } = this.state; + this.props.addEntry(entryToAdd.id); + this.setState({ ...this.state, entryToAdd: undefined }); + }; + + handleAddEntryChange = (selection: any) => { + this.setState({ + ...this.state, + entryToAdd: selection + }); + }; +} + +export default AutocompleteAddEntryToTableField; diff --git a/scm-ui/src/groups/components/GroupForm.js b/scm-ui/src/groups/components/GroupForm.js index 4958fbf0fa..2ed374865d 100644 --- a/scm-ui/src/groups/components/GroupForm.js +++ b/scm-ui/src/groups/components/GroupForm.js @@ -1,22 +1,19 @@ //@flow import React from "react"; -import { translate } from "react-i18next"; -import { - InputField, - SubmitButton, - Textarea, - AddEntryToTableField -} from "@scm-manager/ui-components"; -import type { Group } from "@scm-manager/ui-types"; +import {translate} from "react-i18next"; +import {InputField, SubmitButton, Textarea} from "@scm-manager/ui-components"; +import type {Group} from "@scm-manager/ui-types"; import * as validator from "./groupValidation"; import MemberNameTable from "./MemberNameTable"; +import AutocompleteAddEntryToTableField from "./AutocompleteAddEntryToTableField"; type Props = { t: string => string, submitForm: Group => void, loading?: boolean, - group?: Group + group?: Group, + loadUserSuggestions: string => any }; type State = { @@ -100,12 +97,14 @@ class GroupForm extends React.Component { members={this.state.group.members} memberListChanged={this.memberListChanged} /> - { this.props.modifyGroup(group, this.groupModified(group)); }; + loadUserAutocompletion = (inputValue: string) => { + const url = "http://localhost:8081/scm/api/v2/autocomplete/users?q="; + return fetch(url + inputValue) + .then(response => response.json()) + .then(json => { + return json.map(element => { + return { + value: element, + label: `${element.displayName} (${element.id})` + }; + }); + }); + }; + render() { const { group, loading, error } = this.props; return ( @@ -48,6 +57,7 @@ class EditGroup extends React.Component { this.modifyGroup(group); }} loading={loading} + loadUserSuggestions={this.loadUserAutocompletion} /> );