mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Extracted promise-logic and created AutocompleteAddEntryToTableField
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import AsyncSelect from "react-select/lib/Async";
|
import AsyncSelect from "react-select/lib/Async";
|
||||||
|
import {LabelWithHelpIcon} from "@scm-manager/ui-components";
|
||||||
|
|
||||||
type SelectionResult = {
|
type SelectionResult = {
|
||||||
id: string,
|
id: string,
|
||||||
@@ -13,42 +14,39 @@ type SelectValue = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
url: string,
|
loadSuggestions: string => Promise<SelectionResult>,
|
||||||
loadOptions: string => Promise<SelectionResult>,
|
valueSelected: SelectionResult => void,
|
||||||
valueSelected: SelectionResult => void
|
label: string,
|
||||||
|
helpText?: string,
|
||||||
|
value?: any
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
value: SelectionResult
|
value: SelectionResult
|
||||||
};
|
};
|
||||||
|
|
||||||
const URL_QUERY_SUFFIX: string = "?q=";
|
|
||||||
|
|
||||||
class AsyncAutocomplete extends React.Component<Props, State> {
|
class AsyncAutocomplete extends React.Component<Props, State> {
|
||||||
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) => {
|
handleInputChange = (newValue: SelectValue) => {
|
||||||
this.setState({ value: newValue.value });
|
this.setState({ value: newValue.value });
|
||||||
return newValue.value;
|
this.props.valueSelected(newValue.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
const { label, helpText, value } = this.props;
|
||||||
|
const stringValue = value ? value.id : "";
|
||||||
return (
|
return (
|
||||||
<AsyncSelect
|
<div className="field">
|
||||||
cacheOptions
|
<LabelWithHelpIcon label={label} helpText={helpText} />
|
||||||
defaultOptions
|
<div className="control">
|
||||||
loadOptions={this.getOptions}
|
<AsyncSelect
|
||||||
onChange={this.handleInputChange}
|
cacheOptions
|
||||||
/>
|
defaultOptions
|
||||||
|
loadOptions={this.props.loadSuggestions}
|
||||||
|
onChange={this.handleInputChange}
|
||||||
|
value={stringValue}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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<Props, State> {
|
||||||
|
constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
this.state = {
|
||||||
|
entryToAdd: undefined
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { disabled, buttonLabel, fieldLabel, helpText } = this.props;
|
||||||
|
|
||||||
|
const { entryToAdd } = this.state;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="field">
|
||||||
|
<AsyncAutocomplete
|
||||||
|
label={fieldLabel}
|
||||||
|
loadSuggestions={this.props.loadSuggestions}
|
||||||
|
valueSelected={this.handleAddEntryChange}
|
||||||
|
helpText={helpText}
|
||||||
|
value={entryToAdd ? entryToAdd.id : ""}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<AddButton
|
||||||
|
label={buttonLabel}
|
||||||
|
action={this.addButtonClicked}
|
||||||
|
disabled={disabled}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -1,22 +1,19 @@
|
|||||||
//@flow
|
//@flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { translate } from "react-i18next";
|
import {translate} from "react-i18next";
|
||||||
import {
|
import {InputField, SubmitButton, Textarea} from "@scm-manager/ui-components";
|
||||||
InputField,
|
import type {Group} from "@scm-manager/ui-types";
|
||||||
SubmitButton,
|
|
||||||
Textarea,
|
|
||||||
AddEntryToTableField
|
|
||||||
} from "@scm-manager/ui-components";
|
|
||||||
import type { Group } from "@scm-manager/ui-types";
|
|
||||||
|
|
||||||
import * as validator from "./groupValidation";
|
import * as validator from "./groupValidation";
|
||||||
import MemberNameTable from "./MemberNameTable";
|
import MemberNameTable from "./MemberNameTable";
|
||||||
|
import AutocompleteAddEntryToTableField from "./AutocompleteAddEntryToTableField";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
t: string => string,
|
t: string => string,
|
||||||
submitForm: Group => void,
|
submitForm: Group => void,
|
||||||
loading?: boolean,
|
loading?: boolean,
|
||||||
group?: Group
|
group?: Group,
|
||||||
|
loadUserSuggestions: string => any
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
@@ -100,12 +97,14 @@ class GroupForm extends React.Component<Props, State> {
|
|||||||
members={this.state.group.members}
|
members={this.state.group.members}
|
||||||
memberListChanged={this.memberListChanged}
|
memberListChanged={this.memberListChanged}
|
||||||
/>
|
/>
|
||||||
<AddEntryToTableField
|
|
||||||
|
<AutocompleteAddEntryToTableField
|
||||||
addEntry={this.addMember}
|
addEntry={this.addMember}
|
||||||
disabled={false}
|
disabled={false}
|
||||||
buttonLabel={t("add-member-button.label")}
|
buttonLabel={t("add-member-button.label")}
|
||||||
fieldLabel={t("add-member-textfield.label")}
|
fieldLabel={t("add-member-textfield.label")}
|
||||||
errorMessage={t("add-member-textfield.error")}
|
errorMessage={t("add-member-textfield.error")}
|
||||||
|
loadSuggestions={this.props.loadUserSuggestions}
|
||||||
/>
|
/>
|
||||||
<SubmitButton
|
<SubmitButton
|
||||||
disabled={!this.isValid()}
|
disabled={!this.isValid()}
|
||||||
|
|||||||
@@ -1,17 +1,12 @@
|
|||||||
//@flow
|
//@flow
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { connect } from "react-redux";
|
import {connect} from "react-redux";
|
||||||
import GroupForm from "../components/GroupForm";
|
import GroupForm from "../components/GroupForm";
|
||||||
import {
|
import {getModifyGroupFailure, isModifyGroupPending, modifyGroup, modifyGroupReset} from "../modules/groups";
|
||||||
modifyGroup,
|
import type {History} from "history";
|
||||||
modifyGroupReset,
|
import {withRouter} from "react-router-dom";
|
||||||
isModifyGroupPending,
|
import type {Group} from "@scm-manager/ui-types";
|
||||||
getModifyGroupFailure
|
import {ErrorNotification} from "@scm-manager/ui-components";
|
||||||
} from "../modules/groups";
|
|
||||||
import type { History } from "history";
|
|
||||||
import { withRouter } from "react-router-dom";
|
|
||||||
import type { Group } from "@scm-manager/ui-types";
|
|
||||||
import { ErrorNotification } from "@scm-manager/ui-components";
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
group: Group,
|
group: Group,
|
||||||
@@ -37,6 +32,20 @@ class EditGroup extends React.Component<Props> {
|
|||||||
this.props.modifyGroup(group, this.groupModified(group));
|
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() {
|
render() {
|
||||||
const { group, loading, error } = this.props;
|
const { group, loading, error } = this.props;
|
||||||
return (
|
return (
|
||||||
@@ -48,6 +57,7 @@ class EditGroup extends React.Component<Props> {
|
|||||||
this.modifyGroup(group);
|
this.modifyGroup(group);
|
||||||
}}
|
}}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
|
loadUserSuggestions={this.loadUserAutocompletion}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user