From 59d1c52cad492e97a2af2edbcd47859233508f90 Mon Sep 17 00:00:00 2001 From: shubhammantri1 Date: Sun, 27 Oct 2019 19:24:11 +0530 Subject: [PATCH] adding search.js for sending request --- index.html | 1 + js/search.js | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 js/search.js diff --git a/index.html b/index.html index b5911d9..b043b5e 100644 --- a/index.html +++ b/index.html @@ -97,6 +97,7 @@
Scroll down to see what is trending around the globe.
+ diff --git a/js/search.js b/js/search.js new file mode 100644 index 0000000..8ef4cac --- /dev/null +++ b/js/search.js @@ -0,0 +1,41 @@ +const searchInput = document.querySelector('.searchtext'); +const searchBtn = document.querySelector('btn-link'); + +/* Not present yet */ +const api = "#"; + +searchBtn.addEventListener('click', ($event) => { + $event.preventDefault(); + const inputUrl = { + Url : searchInput.value + }; + submitFormData(inputUrl); +}); + +function makeRequest(data){ + return new Promise((resolve, reject)=>{ + let request = new XMLHttpRequest(); + request.open('POST', api + '/search'); + request.onreadystatechange = () => { + if(request.readyState === 4){ + if(request.status === 201){ + resolve(JSON.parse(request.response)); + }else{ + reject(JSON.parse(request.response)); + } + } + } + request.setRequestHeader('Content-Type', 'application/json'); + request.send(JSON.stringify(data)); + }); +} + +async function submitFormData(inputUrl){ + try{ + const requestPromise = makeRequest(inputUrl); + const response = await requestPromise; + } catch(errorResponse) { + // If there is place where we can show error message; + /*responseMessage.textContent = errorResponse.error;*/ + } +}