1- import { React , useEffect , useState } from "react" ;
1+ import { React , useEffect , useRef , useState } from "react" ;
22import PropTypes from "prop-types" ;
33import { useTranslation } from "react-i18next" ;
44import { Row , Spinner } from "react-bootstrap" ;
55import AsyncSelect from "react-select/async" ;
66import Col from "react-bootstrap/Col" ;
7- import { formatDate , getHeaders , loadDropdownOptions } from "./poster-helper" ;
7+ import { formatDate , getHeaders , loadDropdownOptions , loadDropdownOptionsPromise } from "./poster-helper" ;
88
99/**
1010 * @param {object } props Props.
@@ -133,6 +133,28 @@ function PosterSubscription({
133133 subscriptionNumberValue ,
134134 ] ) ;
135135
136+ const timeoutRef = useRef ( null ) ;
137+
138+ const debounceOptions = ( inputValue , type ) => {
139+ // Debounce promise.
140+ return new Promise ( ( resolve , reject ) => {
141+ if ( timeoutRef . current ) {
142+ clearTimeout ( timeoutRef . current ) ;
143+ }
144+
145+ timeoutRef . current = setTimeout ( ( ) => {
146+ loadDropdownOptionsPromise (
147+ searchEndpoint ,
148+ getHeaders ( ) ,
149+ inputValue ,
150+ type
151+ )
152+ . then ( ( data ) => resolve ( data ) )
153+ . catch ( ( reason ) => reject ( reason ) ) ;
154+ } , 500 ) ;
155+ } ) ;
156+ } ;
157+
136158 return (
137159 < >
138160 < Row >
@@ -153,14 +175,8 @@ function PosterSubscription({
153175 isSearchable
154176 defaultOptions
155177 isMulti
156- loadOptions = { ( inputValue , callback ) =>
157- loadDropdownOptions (
158- searchEndpoint ,
159- getHeaders ( ) ,
160- inputValue ,
161- callback ,
162- "locations"
163- )
178+ loadOptions = { ( inputValue ) =>
179+ debounceOptions ( inputValue , "locations" )
164180 }
165181 value = { subscriptionPlaceValue }
166182 onChange = { ( newValue ) => {
@@ -181,14 +197,8 @@ function PosterSubscription({
181197 isSearchable
182198 defaultOptions
183199 isMulti
184- loadOptions = { ( inputValue , callback ) =>
185- loadDropdownOptions (
186- searchEndpoint ,
187- getHeaders ( ) ,
188- inputValue ,
189- callback ,
190- "organizations"
191- )
200+ loadOptions = { ( inputValue ) =>
201+ debounceOptions ( inputValue , "organizations" )
192202 }
193203 value = { subscriptionOrganizerValue }
194204 onChange = { ( newValue ) => {
@@ -209,14 +219,8 @@ function PosterSubscription({
209219 isSearchable
210220 defaultOptions
211221 isMulti
212- loadOptions = { ( inputValue , callback ) =>
213- loadDropdownOptions (
214- searchEndpoint ,
215- getHeaders ( ) ,
216- inputValue ,
217- callback ,
218- "tags"
219- )
222+ loadOptions = { ( inputValue ) =>
223+ debounceOptions ( inputValue , "tags" )
220224 }
221225 value = { subscriptionTagValue }
222226 onChange = { ( newValue ) => {
0 commit comments