Here I am sharing some overview of what I did in my stencilsjs project to achieve this scenario,
firstly I have created the separate xmlApi.ts
common file for my project and wrote following code in that
// common XMLHttpRequest for handling fetch request
// currently using this XMLHttpRequest in search component to fetch the data
let xmlApi
// Create the XHR request
const request = new XMLHttpRequest()
const fetchRequest = (url: string, params: any) => {
// Return it as a Promise
return new Promise((resolve, reject) => {
// Setup our listener to process compeleted requests
request.onreadystatechange = () => {
// Only run if the request is complete
if (request.readyState !== 4) { return }
// Process the response
if (request.status >= 200 && request.status < 300) {
// If successful
resolve(request)
} else {
// If failed
reject({
status: request.status,
statusText: request.statusText
})
}
}
// If error
request.onerror = () => {
reject({
status: request.status,
statusText: request.statusText
})
}
// Setup our HTTP request
request.open(params.method, url, true)
// Setup our HTTP request headers
if (params.headers) {
Object.keys(params.headers).forEach(key => {
request.setRequestHeader(key, params.headers[key])
})
}
// Send the request
request.send(params.body)
})
}
xmlApi = {
// exporting XMLHttpRequest object to use in search component to abort the previous fetch calls
request,
fetchRequest
}
export default xmlApi
Second I have passed the event object with onTextInput method to get the input value using event.target.value
HTMl:
<input id="search_box" type="text" placeholder="type to search..."
onInput={event => { this.onTextInput(event) }}/>
Sample HTML for suggestion :
Here based on the showSuggestionListFlag
,i have shown the search suggestionlist, also used css to align div and input tag properly
<div class={'search-result ' + (this.showSuggestionListFlag ? 'show' : '')}>
<ul class="dropdown-list">
{this.responseData && this.responseData.map((item, index) => (
<li class="list-element">{item} </li>
))}
</ul>
</div>
Third in my ts code, i imported the my xmlApi
Here i have just written some logic code from my code, I have also used async
and await
to handle promise/reject in my project code, according to your code you can handle your own promise/reject
code:
import xmlApi from './xmlApi'
onTextInput (event) { // first created bodydata here using `event.target.value`
const opts = {
method: 'POST',
body: bodyData,
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
}
}
try {
// aborting the previous xhr request call
if (xmlApi.request) {
xmlApi.request.abort()
}
const responseData = xmlApi.fetchRequest(endPointUrl, opts)
.then(data => {
consolep.log(`xhr request success`)
return JSON.parse(data['response'])
})
.catch(error => {
console.log.debug(`xhr request cancelled/failed : ${JSON.stringify(error)}`)
}) //use responseData
if(responseData){this.showSuggestionListFlag = true}
} catch (e) {
console.log(`fetch failed`, e)
}
}
This is my first answer on Stack Overflow.
Thanks!!