Skip to content

Commit

Permalink
Merge 16bc54e into 6feca77
Browse files Browse the repository at this point in the history
  • Loading branch information
dhivyada committed Apr 6, 2020
2 parents 6feca77 + 16bc54e commit 6a99f4b
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/components/Autocomplete.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

<script type="text/babel">
import debounce from 'lodash/debounce'
const NETWORK_RESPONSE_ERROR = 'Network response was not ok.'
export default {
props: {
/**
Expand Down Expand Up @@ -241,6 +242,13 @@ export default {
}
return this.resourceSearch(this.source + this.display)
case this.source.constructor.name === 'AsyncFunction':
// No resource search with no input
if (!this.display || this.display.length < 1) {
return
}
// When the function is async and returns a promise
return this.handleCallback(this.source(this.display))
case typeof this.source === 'function':
// No resource search with no input
if (!this.display || this.display.length < 1) {
Expand Down Expand Up @@ -279,13 +287,34 @@ export default {
headers: this.getHeaders()
})
return this.handleFetchRequest(promise)
},
handleFetchResponse (response) {
if (response.ok) {
this.error = null
return response.json()
}
throw new Error(NETWORK_RESPONSE_ERROR)
},
handlePromiseResolution (response) {
if (response.status === 200) {
this.error = null
return response
}
throw new Error(NETWORK_RESPONSE_ERROR)
},
handleFetchRequest: function (promise) {
return this.handlePromise(promise, (response) => this.handleFetchResponse(response))
},
handleCallback (promise) {
this.loading = true
return this.handlePromise(promise, (response) => this.handlePromiseResolution(response))
},
handlePromise (promise, responseHandler) {
return promise
.then(response => {
if (response.ok) {
this.error = null
return response.json()
}
throw new Error('Network response was not ok.')
return responseHandler(response)
})
.then(response => {
this.results = this.setResults(response)
Expand All @@ -297,7 +326,6 @@ export default {
this.loading = false
})
},
/**
* Set some default headers and apply user supplied headers
*/
Expand Down

0 comments on commit 6a99f4b

Please sign in to comment.