georgiev955

03. Forecaster

Oct 30th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. async function attachEvents() {
  2. const location = document.getElementById('location');
  3. const getBtn = document.getElementById('submit');
  4.  
  5. const res = await fetch("http://localhost:3030/jsonstore/forecaster/locations");
  6. const data = await res.json();
  7.  
  8. getBtn.addEventListener('click', getData);
  9.  
  10. async function getData() {
  11.  
  12. try {
  13. const code = data.filter(x => x.name == location.value)[0].code;
  14. const currentCondit = await fetch(`http://localhost:3030/jsonstore/forecaster/today/${code}`);
  15. const currentWeather = await currentCondit.json();
  16.  
  17. const weatherData = {
  18. locationName: currentWeather.name,
  19. low: currentWeather.forecast.low,
  20. high: currentWeather.forecast.high,
  21. condition: currentWeather.forecast.condition
  22. }
  23.  
  24. const threeDayCondit = await fetch(`http://localhost:3030/jsonstore/forecaster/upcoming/${code}`);
  25. const threeDayWeather = await threeDayCondit.json();
  26.  
  27. const threeDayData = {
  28. locationName: currentWeather.name,
  29. firstDay: {
  30. low: threeDayWeather.forecast[0].low,
  31. high: threeDayWeather.forecast[0].high,
  32. condition: threeDayWeather.forecast[0].condition
  33. },
  34. secondDay: {
  35. low: threeDayWeather.forecast[1].low,
  36. high: threeDayWeather.forecast[1].high,
  37. condition: threeDayWeather.forecast[1].condition
  38. },
  39. thirdDay: {
  40. low: threeDayWeather.forecast[2].low,
  41. high: threeDayWeather.forecast[2].high,
  42. condition: threeDayWeather.forecast[2].condition
  43. },
  44. }
  45.  
  46. const weatherSymbol = {
  47. "Sunny": '&#x2600',
  48. "Partly sunny": '&#x26C5',
  49. "Overcast": '&#x2601',
  50. "Rain": '&#x2614'
  51. }
  52. document.getElementById('forecast').style.display = 'block';
  53.  
  54. const currentDayDiv = document.getElementById('current');
  55. const upcomingDaysDiv = document.getElementById('upcoming');
  56.  
  57. const singleDayDiv = document.createElement('div');
  58. singleDayDiv.classList.add('forecasts');
  59. singleDayDiv.innerHTML = `
  60. <span class="condition symbol">${weatherSymbol[weatherData.condition]}</span>
  61. <span class="condition">
  62. <span class="forecast-data">${weatherData.locationName}</span>
  63. <span class="forecast-data">${weatherData.low}\&#176/${weatherData.high}\&#176</span>
  64. <span class="forecast-data">${weatherData.condition}</span>
  65. </span>
  66. `
  67. currentDayDiv.appendChild(singleDayDiv);
  68.  
  69. const threeDayDiv = document.createElement('div');
  70. threeDayDiv.classList.add('forecast-info');
  71. threeDayDiv.innerHTML = `
  72. <span class="upcoming">
  73. <span class="symbol">${weatherSymbol[threeDayData.firstDay.condition]}</span>
  74. <span class="forecast-data">${threeDayData.firstDay.low}\&#176/${threeDayData.firstDay.high}\&#176</span>
  75. <span class="forecast-data">${threeDayData.firstDay.condition}</span>
  76. </span>
  77. <span class="upcoming">
  78. <span class="symbol">${weatherSymbol[threeDayData.secondDay.condition]}</span>
  79. <span class="forecast-data">${threeDayData.secondDay.low}\&#176/${threeDayData.secondDay.high}\&#176</span>
  80. <span class="forecast-data">${threeDayData.secondDay.condition}</span>
  81. </span>
  82. <span class="upcoming">
  83. <span class="symbol">${weatherSymbol[threeDayData.thirdDay.condition]}</span>
  84. <span class="forecast-data">${threeDayData.thirdDay.low}\&#176/${threeDayData.thirdDay.high}\&#176</span>
  85. <span class="forecast-data">${threeDayData.thirdDay.condition}</span>
  86. </span>
  87. `
  88. upcomingDaysDiv.appendChild(threeDayDiv);
  89. } catch (err) {
  90. document.getElementById('forecast').style.display = 'block';
  91. document.getElementById('forecast').innerHTML = 'Error';
  92. }
  93. }
  94. }
  95.  
  96. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment