Advertisement
ErolKZ

Untitled

Mar 2nd, 2022
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.49 KB | None | 0 0
  1.  
  2. function attachEvents() {
  3.  
  4.  
  5. let url = `http://localhost:3030/jsonstore/forecaster/locations`;
  6.  
  7. let inputBtnElement = document.getElementById('submit');
  8.  
  9. let divForecastElement = document.getElementById('forecast');
  10.  
  11. inputBtnElement.addEventListener('click', (e) => {
  12.  
  13. divForecastElement.style.display = 'none';
  14.  
  15. let divContent = document.getElementById('content');
  16.  
  17. let divForecasts = document.querySelectorAll('.forecasts');
  18.  
  19. if (divContent.lastChild.textContent === 'Error: Invalid input!') {
  20.  
  21. let h1Element = document.querySelector('.error');
  22.  
  23. h1Element.remove();
  24.  
  25. } else if (divForecasts.length !== 0) {
  26.  
  27. divForecasts[0].remove();
  28.  
  29. }
  30.  
  31. let locationElement = document.getElementById('location').value;
  32.  
  33. fetch(url)
  34.  
  35. .then((response) => {
  36.  
  37. let arrOfCities = ['London', 'New York', 'Barcelona'];
  38.  
  39. if (arrOfCities.includes(locationElement)) {
  40.  
  41. return response.json();
  42.  
  43. } else {
  44.  
  45. throw new Error(`Invalid input!`);
  46.  
  47. }
  48.  
  49. })
  50.  
  51. .then(result => {
  52.  
  53. result.forEach(obj => {
  54.  
  55. if (obj.name === locationElement) {
  56.  
  57. let url2 = `http://localhost:3030/jsonstore/forecaster/today/${obj.code}`;
  58.  
  59. fetch(url2)
  60.  
  61. .then(response => response.json())
  62.  
  63. .then(result => {
  64.  
  65. console.log(result);
  66.  
  67. function getConditionSymbol(condition) {
  68.  
  69. switch (condition) {
  70. case 'Sunny':
  71. return '&#x2600';
  72. break;
  73. case 'Partly sunny':
  74. return '&#x26C5';
  75. break;
  76. case 'Overcast':
  77. return '&#x2601';
  78. break;
  79. case 'Rain':
  80. return '&#x2614';
  81. break;
  82. }
  83. }
  84.  
  85.  
  86. let divCurrent = document.getElementById('current');
  87.  
  88. let divForecasts = document.createElement('div');
  89.  
  90. divForecasts.classList.add('forecasts');
  91.  
  92. let spanCondition = document.createElement('span');
  93.  
  94. spanCondition.classList.add('condition');
  95.  
  96. spanCondition.classList.add('symbol');
  97.  
  98. spanCondition.innerHTML = getConditionSymbol(result.forecast.condition);
  99.  
  100. divForecasts.appendChild(spanCondition);
  101.  
  102. divCurrent.appendChild(divForecasts);
  103.  
  104. let dataSpan = document.createElement('span');
  105.  
  106. let citySpan = document.createElement('span');
  107.  
  108. let tempSpan = document.createElement('span');
  109.  
  110. let conditionSpan = document.createElement('span');
  111.  
  112. dataSpan.classList.add('condition');
  113.  
  114. citySpan.classList.add('forecast-data');
  115.  
  116. tempSpan.classList.add('forecast-data');
  117.  
  118. conditionSpan.classList.add('forecast-data');
  119.  
  120. if (locationElement === 'London') {
  121.  
  122. citySpan.textContent = 'London, UK';
  123.  
  124. tempSpan.textContent = `${result.forecast.low}/${result.forecast.high}`;
  125.  
  126. conditionSpan.textContent = result.forecast.condition;
  127.  
  128. dataSpan.appendChild(citySpan);
  129.  
  130. dataSpan.appendChild(tempSpan);
  131.  
  132. dataSpan.appendChild(conditionSpan);
  133.  
  134. divForecasts.appendChild(dataSpan);
  135.  
  136. console.log(dataSpan);
  137.  
  138. }
  139.  
  140.  
  141. })
  142.  
  143. let url3 = `http://localhost:3030/jsonstore/forecaster/upcoming/${obj.code}`;
  144.  
  145. fetch(url3)
  146.  
  147. .then(response => response.json())
  148.  
  149. .then(result => {
  150.  
  151. // console.log(result);
  152.  
  153. divForecastElement.style.display = 'inline-block';
  154.  
  155. })
  156.  
  157. }
  158.  
  159. })
  160.  
  161.  
  162. })
  163.  
  164. .catch(error => {
  165.  
  166. let divContent = document.getElementById('content');
  167.  
  168. let errorElement = document.createElement('h1');
  169.  
  170. errorElement.classList.add('error');
  171.  
  172. errorElement.textContent = error;
  173.  
  174. divContent.appendChild(errorElement);
  175.  
  176. })
  177.  
  178. })
  179.  
  180.  
  181. }
  182.  
  183.  
  184.  
  185. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement