mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 10:41:06 +01:00
Implemented Autocomplete timeout
This commit is contained in:
@@ -7,13 +7,16 @@ const getSuggestionValue = suggestion => suggestion.displayName;
|
|||||||
const renderSuggestion = suggestion => <div>{suggestion.displayName}</div>;
|
const renderSuggestion = suggestion => <div>{suggestion.displayName}</div>;
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
url: string
|
url: string,
|
||||||
|
placeholder: string,
|
||||||
|
timeoutMillis: number
|
||||||
};
|
};
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
suggestions: any[],
|
suggestions: any[],
|
||||||
value: any,
|
value: any,
|
||||||
isLoading: boolean
|
isLoading: boolean,
|
||||||
|
lastRequestId: any
|
||||||
};
|
};
|
||||||
|
|
||||||
class Autocomplete extends React.Component<Props, State> {
|
class Autocomplete extends React.Component<Props, State> {
|
||||||
@@ -23,8 +26,8 @@ class Autocomplete extends React.Component<Props, State> {
|
|||||||
this.state = {
|
this.state = {
|
||||||
suggestions: [],
|
suggestions: [],
|
||||||
value: "",
|
value: "",
|
||||||
users: [],
|
isLoading: false,
|
||||||
isLoading: false
|
lastRequestId: undefined
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,27 +36,42 @@ class Autocomplete extends React.Component<Props, State> {
|
|||||||
isLoading: true
|
isLoading: true
|
||||||
});
|
});
|
||||||
|
|
||||||
apiClient
|
if (this.state.lastRequestId) {
|
||||||
.get("http://localhost:8081/scm/api/v2/autocomplete/users?q=" + value) //TODO: Do not hardcode URL
|
clearTimeout(this.state.lastRequestId);
|
||||||
.then(response => {
|
}
|
||||||
return response.json();
|
|
||||||
})
|
const requestId = setTimeout(() => {
|
||||||
.then(json => {
|
this.setState({
|
||||||
this.setState({
|
isLoading: true
|
||||||
suggestions: [...this.state.suggestions, ...json]
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
apiClient
|
||||||
|
.get(this.props.url + value)
|
||||||
|
.then(response => {
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(json => {
|
||||||
|
this.setState({
|
||||||
|
isLoading: false,
|
||||||
|
suggestions: [...json]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, this.props.timeoutMillis);
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
lastRequestId: requestId
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
onChange = (event, { newValue }) => {
|
// TODO: Flow types
|
||||||
// TODO: Flow types
|
onChange = (event: SyntheticInputEvent<HTMLInputElement>, { newValue }) => {
|
||||||
this.setState({
|
this.setState({
|
||||||
value: newValue
|
value: newValue
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: Flow types
|
||||||
onSuggestionsFetchRequested = ({ value }) => {
|
onSuggestionsFetchRequested = ({ value }) => {
|
||||||
// TODO: Flow types
|
|
||||||
this.loadSuggestions(value);
|
this.loadSuggestions(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,12 +85,11 @@ class Autocomplete extends React.Component<Props, State> {
|
|||||||
const { value, suggestions } = this.state;
|
const { value, suggestions } = this.state;
|
||||||
|
|
||||||
const inputProps = {
|
const inputProps = {
|
||||||
placeholder: "placeholder", // TODO: i18n
|
placeholder: this.props.placeholder,
|
||||||
value,
|
value,
|
||||||
onChange: this.onChange
|
onChange: this.onChange
|
||||||
};
|
};
|
||||||
|
|
||||||
// Finally, render it!
|
|
||||||
return (
|
return (
|
||||||
<Autosuggest
|
<Autosuggest
|
||||||
suggestions={suggestions}
|
suggestions={suggestions}
|
||||||
|
|||||||
Reference in New Issue
Block a user