4.2 - Quicksearch
Introduction
How it works
input.addEventListener(
'input',
debounce((event) => {
// saves the value of what the user is typing in userInput
const userInput = event.target.value;
// intercepts the url the form is pointing at
const url = document.getElementById(formID).getAttribute('action');
// shapes the url with the query state
history.replaceState({}, '', `?searchValue=${userInput}&token=${token}`);
// then a fetch to our own server is made with the id and token passed, these are used on the server to fetch the right playlist the Spotify api
fetch(`${url}?query=${userInput}&async=true&token=${token}`)
.then((res) => res.text())
.then((html) => {
// the server than renders the component, with the right data and sends this back to the client
const resultComponent = document.querySelector('.search-results');
resultComponent.innerHTML = html;
});
});
})Last updated


