Advertisement
ErolKZ

Untitled

Mar 2nd, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 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. console.log(result);
  54.  
  55. result.forEach(obj => {
  56.  
  57. if (obj.name === locationElement) {
  58.  
  59. let url2 = `http://localhost:3030/jsonstore/forecaster/today/${obj.code}`;
  60.  
  61. fetch(url2)
  62.  
  63. .then(response => response.json())
  64.  
  65. .then(result => {
  66.  
  67. function getConditionSymbol(condition) {
  68.  
  69. switch (condition) {
  70. case 'Sunny':
  71. return '&#x2600';
  72. case 'Partly sunny':
  73. return '&#x26C5';
  74. case 'Overcast':
  75. return '&#x2601';
  76. case 'Rain':
  77. return '&#x2614';
  78. }
  79. }
  80.  
  81.  
  82. let divCurrent = document.getElementById('current');
  83.  
  84. let divForecasts = document.createElement('div');
  85.  
  86. divForecasts.classList.add('forecasts');
  87.  
  88. let spanCondition = document.createElement('span');
  89.  
  90. spanCondition.classList.add('condition');
  91.  
  92. spanCondition.classList.add('symbol');
  93.  
  94. spanCondition.innerHTML = getConditionSymbol(result.forecast.condition);
  95.  
  96. divForecasts.appendChild(spanCondition);
  97.  
  98. divCurrent.appendChild(divForecasts);
  99.  
  100. let dataSpan = document.createElement('span');
  101.  
  102. let citySpan = document.createElement('span');
  103.  
  104. let tempSpan = document.createElement('span');
  105.  
  106. let conditionSpan = document.createElement('span');
  107.  
  108. dataSpan.classList.add('condition');
  109.  
  110. citySpan.classList.add('forecast-data');
  111.  
  112. tempSpan.classList.add('forecast-data');
  113.  
  114. conditionSpan.classList.add('forecast-data');
  115.  
  116. citySpan.textContent = result.name;
  117.  
  118. tempSpan.innerHTML = `${result.forecast.low}&#176/${result.forecast.high}&#176`;
  119.  
  120. conditionSpan.textContent = result.forecast.condition;
  121.  
  122. dataSpan.appendChild(citySpan);
  123.  
  124. dataSpan.appendChild(tempSpan);
  125.  
  126. dataSpan.appendChild(conditionSpan);
  127.  
  128. divForecasts.appendChild(dataSpan);
  129.  
  130. console.log(dataSpan);
  131.  
  132.  
  133.  
  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. // console.log(result);
  146.  
  147. divForecastElement.style.display = 'inline-block';
  148.  
  149. })
  150.  
  151. }
  152.  
  153. })
  154.  
  155.  
  156. })
  157.  
  158. .catch(error => {
  159.  
  160. let divContent = document.getElementById('content');
  161.  
  162. let errorElement = document.createElement('h1');
  163.  
  164. errorElement.classList.add('error');
  165.  
  166. errorElement.textContent = error;
  167.  
  168. divContent.appendChild(errorElement);
  169.  
  170. })
  171.  
  172. })
  173.  
  174.  
  175. }
  176.  
  177.  
  178.  
  179. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement