Advertisement
ErolKZ

Untitled

Mar 2nd, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 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 divUpcoming = document.getElementById('upcoming');
  16.  
  17.  
  18. let divContent = document.getElementById('content');
  19.  
  20. let divForecasts = document.querySelectorAll('.forecasts');
  21.  
  22. if (divContent.lastChild.textContent === 'Error: Invalid input!') {
  23.  
  24. let h1Element = document.querySelector('.error');
  25.  
  26. h1Element.remove();
  27.  
  28. } else if (divForecasts.length !== 0) {
  29.  
  30. divForecasts[0].remove();
  31.  
  32. }
  33.  
  34. let locationElement = document.getElementById('location').value;
  35.  
  36. fetch(url)
  37.  
  38. .then((response) => {
  39.  
  40. console.log(response);
  41.  
  42. let arrOfCities = ['London', 'New York', 'Barcelona'];
  43.  
  44. if (arrOfCities.includes(locationElement)) {
  45.  
  46. return response.json();
  47.  
  48. } else {
  49.  
  50. throw new Error(`Invalid input!`);
  51.  
  52. }
  53.  
  54. })
  55.  
  56. .then(result => {
  57.  
  58. console.log(result);
  59.  
  60. result.forEach(obj => {
  61.  
  62. if (obj.name === locationElement) {
  63.  
  64. let url2 = `http://localhost:3030/jsonstore/forecaster/today/${obj.code}`;
  65.  
  66. fetch(url2)
  67.  
  68. .then(response => response.json())
  69.  
  70. .then(result => {
  71.  
  72. function getConditionSymbol(condition) {
  73.  
  74. switch (condition) {
  75. case 'Sunny':
  76. return '&#x2600';
  77. case 'Partly sunny':
  78. return '&#x26C5';
  79. case 'Overcast':
  80. return '&#x2601';
  81. case 'Rain':
  82. return '&#x2614';
  83. }
  84. }
  85.  
  86.  
  87. let divCurrent = document.getElementById('current');
  88.  
  89. let divForecasts = document.createElement('div');
  90.  
  91. divForecasts.classList.add('forecasts');
  92.  
  93. let spanCondition = document.createElement('span');
  94.  
  95. spanCondition.classList.add('condition');
  96.  
  97. spanCondition.classList.add('symbol');
  98.  
  99. spanCondition.innerHTML = getConditionSymbol(result.forecast.condition);
  100.  
  101. divForecasts.appendChild(spanCondition);
  102.  
  103. divCurrent.appendChild(divForecasts);
  104.  
  105. let dataSpan = document.createElement('span');
  106.  
  107. let citySpan = document.createElement('span');
  108.  
  109. let tempSpan = document.createElement('span');
  110.  
  111. let conditionSpan = document.createElement('span');
  112.  
  113. dataSpan.classList.add('condition');
  114.  
  115. citySpan.classList.add('forecast-data');
  116.  
  117. tempSpan.classList.add('forecast-data');
  118.  
  119. conditionSpan.classList.add('forecast-data');
  120.  
  121. citySpan.textContent = result.name;
  122.  
  123. tempSpan.innerHTML = `${result.forecast.low}&#176/${result.forecast.high}&#176`;
  124.  
  125. conditionSpan.textContent = result.forecast.condition;
  126.  
  127. dataSpan.appendChild(citySpan);
  128.  
  129. dataSpan.appendChild(tempSpan);
  130.  
  131. dataSpan.appendChild(conditionSpan);
  132.  
  133. divForecasts.appendChild(dataSpan);
  134.  
  135. })
  136.  
  137. let url3 = `http://localhost:3030/jsonstore/forecaster/upcoming/${obj.code}`;
  138.  
  139. fetch(url3)
  140.  
  141. .then(response => response.json())
  142.  
  143. .then(result => {
  144.  
  145. function getConditionSymbol(condition) {
  146.  
  147. switch (condition) {
  148. case 'Sunny':
  149. return '&#x2600';
  150. case 'Partly sunny':
  151. return '&#x26C5';
  152. case 'Overcast':
  153. return '&#x2601';
  154. case 'Rain':
  155. return '&#x2614';
  156. }
  157. }
  158.  
  159.  
  160. let divForecasts = document.createElement('div');
  161.  
  162. divForecasts.classList.add('forecasts-info');
  163.  
  164. let divUpcoming = document.getElementById('upcoming');
  165.  
  166. for (let obj of result.forecast) {
  167.  
  168. console.log(obj);
  169.  
  170. let spanUpcoming = document.createElement('span');
  171.  
  172. spanUpcoming.classList.add('upcoming');
  173.  
  174. let spanSymbol = document.createElement('span');
  175.  
  176. spanSymbol.classList.add('symbol');
  177.  
  178. spanSymbol.innerHTML = getConditionSymbol(obj.condition);
  179.  
  180. let tempSpan = document.createElement('span');
  181.  
  182. let conditionSpan = document.createElement('span');
  183.  
  184.  
  185. tempSpan.classList.add('forecast-data');
  186.  
  187. conditionSpan.classList.add('forecast-data');
  188.  
  189. tempSpan.innerHTML = `${obj.low}&#176/${obj.high}&#176`;
  190.  
  191. conditionSpan.textContent = obj.condition;
  192.  
  193. spanUpcoming.appendChild(spanSymbol);
  194.  
  195. spanUpcoming.appendChild(tempSpan);
  196.  
  197. spanUpcoming.appendChild(conditionSpan);
  198.  
  199. divForecasts.appendChild(spanUpcoming);
  200.  
  201. }
  202.  
  203. divUpcoming.appendChild(divForecasts);
  204.  
  205. divForecastElement.style.display = 'inline-block';
  206.  
  207. })
  208.  
  209. }
  210.  
  211. })
  212.  
  213.  
  214. })
  215.  
  216. .catch(error => {
  217.  
  218. let divContent = document.getElementById('content');
  219.  
  220. let errorElement = document.createElement('h1');
  221.  
  222. errorElement.classList.add('error');
  223.  
  224. errorElement.textContent = error;
  225.  
  226. divContent.appendChild(errorElement);
  227.  
  228. })
  229.  
  230. })
  231.  
  232. }
  233.  
  234.  
  235.  
  236. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement