|
| 1 | +const form = document.querySelector('form'); |
| 2 | + |
1 | 3 | // this function accepts an {object} and returns the contents displayed to the page |
2 | 4 | function displayResults(jsonData) { |
| 5 | + // receiving array of objects here |
| 6 | + |
3 | 7 | // results is an array of n items |
4 | | - let results = jsonData['results']; |
| 8 | + let results = jsonData; |
5 | 9 |
|
6 | 10 | results.forEach((element) => { |
7 | 11 | let item = JSON.stringify(element); |
8 | 12 | // you can now access element.body, element.lecture, element.timestamp, element.week |
9 | | - document.getElementById('results-area').innerText = element.body; |
10 | 13 |
|
11 | 14 | // create a new div area for each element |
12 | 15 | // insert into that div, elements for each body/lecture/timestamp/week |
| 16 | + // create a DOM table |
| 17 | + var newTable = document.createElement('table'); |
| 18 | + newTable.setAttribute('style', 'border-spacing:50px;'); |
| 19 | + // add the DOM table to the landing-area |
| 20 | + document.getElementById('landing-area').appendChild(newTable); |
| 21 | + var headerRow = |
| 22 | + '<tr> <td> <b> Body </b> </td> <td> <b> Lecture </b> </td> <td> <b> Timestamp </b> </td> <td> <b> Week </b> </td></tr>'; |
| 23 | + var tr = '<tr>'; |
| 24 | + tr += |
| 25 | + '<td> ' + |
| 26 | + element.body + |
| 27 | + '</td>' + |
| 28 | + '<td> ' + |
| 29 | + element.lecture + |
| 30 | + '</td>' + |
| 31 | + '<td>' + |
| 32 | + element.timestamp + |
| 33 | + '</td>' + |
| 34 | + '<td>' + |
| 35 | + element.week + |
| 36 | + '</td>'; |
| 37 | + |
| 38 | + document.getElementById('landing-area').lastChild.innerHTML += headerRow; |
| 39 | + document.getElementById('landing-area').lastChild.innerHTML += tr; |
13 | 40 | }); |
14 | 41 | } |
15 | 42 |
|
16 | 43 | function clearResults() { |
17 | | - document.getElementById('results-area').innerText = ''; |
| 44 | + document.getElementById('landing-area').innerText = ''; |
18 | 45 | } |
19 | 46 |
|
20 | | -function sendQuery() { |
21 | | - fetch('http://localhost:5000/test') |
| 47 | +function sendQuery(formData) { |
| 48 | + // clear search results area before starting a new search |
| 49 | + clearResults(); |
| 50 | + |
| 51 | + fetch('http://localhost:5000/search', { |
| 52 | + method: 'post', |
| 53 | + body: formData, |
| 54 | + }) |
22 | 55 | .then((response) => { |
23 | | - return response.json(); |
24 | | - }) |
25 | | - .then((jsonResponse) => { |
26 | | - displayResults(jsonResponse).catch((error) => { |
27 | | - console.log('Error: ' + error); |
| 56 | + // this returns a promise |
| 57 | + response.json().then((data) => { |
| 58 | + // this is an object containing an array of results, which are themselves objects |
| 59 | + var resultsData = data.results; |
| 60 | + console.log(resultsData); |
| 61 | + displayResults(data.results); |
28 | 62 | }); |
| 63 | + }) |
| 64 | + .catch((error) => { |
| 65 | + console.log('Error: ' + error); |
29 | 66 | }); |
30 | 67 | } |
31 | 68 |
|
32 | | -console.log('made it to tabs'); |
33 | | -// test hitting our local server |
34 | | -fetch('http://localhost:5000/test') |
35 | | - .then(function (response) { |
36 | | - return response.json(); |
37 | | - }) |
38 | | - .then(function (jsonResponse) { |
39 | | - // displayResults(jsonResponse); |
40 | | - console.log('do nothing'); |
41 | | - }) |
42 | | - .catch(function (error) { |
43 | | - console.log('Error: ' + error); |
44 | | - }); |
| 69 | +function handleSubmit(event) { |
| 70 | + // Prevent the default submission |
| 71 | + event.preventDefault(); |
| 72 | + |
| 73 | + // Log the serialized data |
| 74 | + console.log(serializeObject(this)); |
45 | 75 |
|
46 | | -// add event listener for Search button |
47 | | -document.getElementById('search-button').addEventListener('click', sendQuery); |
| 76 | + // Alert the user |
| 77 | + alert('Check the console.'); |
| 78 | +} |
| 79 | + |
| 80 | +// add event listener for Clear Results button |
48 | 81 | document |
49 | 82 | .getElementById('clear-results') |
50 | 83 | .addEventListener('click', clearResults); |
| 84 | + |
| 85 | +// add event listener for query form |
| 86 | +document.getElementById('form').addEventListener('submit', function (e) { |
| 87 | + e.preventDefault(); |
| 88 | + |
| 89 | + // create formData object |
| 90 | + const formData = new FormData(this); |
| 91 | + |
| 92 | + // jsonify the query to send to the server |
| 93 | + var object = {}; |
| 94 | + formData.forEach(function (value, key) { |
| 95 | + object[key] = value; |
| 96 | + }); |
| 97 | + var json = JSON.stringify(object); |
| 98 | + |
| 99 | + sendQuery(json); |
| 100 | +}); |
0 commit comments