Advertisement
GeorgiLukanov87

Forecast

Feb 28th, 2023
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const icons = {
  2.     'Sunny': '&#x2600', // ☀
  3.     'Partly sunny': '&#x26C5', // ⛅
  4.     'Overcast': '&#x2601', // ☁
  5.     'Rain': '&#x2614', // ☂
  6.     'Degrees': '&#176' // °
  7. }
  8.  
  9.  
  10. async function attachEvents() {
  11.     document.getElementById('submit').addEventListener('click', getWeatherInfo);
  12. }
  13.  
  14. async function getWeatherInfo() {
  15.     const inputLocation = document.getElementById('location').value;
  16.  
  17.     const response = await fetch(`http://localhost:3030/jsonstore/forecaster/locations`);
  18.     const result = await response.json();
  19.  
  20.     const info = result.find(x => x.name === inputLocation);
  21.     createForecaster(info.code);
  22.  
  23. }
  24.  
  25. async function createForecaster(code) {
  26.     const currentSection = document.getElementById('current')
  27.     const forecastContainer = document.getElementById('forecast')
  28.     const upcomingContainer = document.getElementById('upcoming')
  29.     const urlToday = `http://localhost:3030/jsonstore/forecaster/today/${code}`;
  30.     const urlUpcoming = `http://localhost:3030/jsonstore/forecaster/upcoming/${code}`;
  31.  
  32.     const responseToday = await fetch(urlToday);
  33.     const resultToday = await responseToday.json();
  34.  
  35.     const responseUpcoming = await fetch(urlUpcoming);
  36.     const resultUpcoming = await responseUpcoming.json();
  37.  
  38.     forecastContainer.style.display = 'block';
  39.     const todayHTMLTempl = createToday(resultToday);
  40.     currentSection.appendChild(todayHTMLTempl);
  41.  
  42.     const upcomingHTMLTemp = createUpcoming(resultUpcoming);
  43.     upcomingContainer.appendChild(upcomingHTMLTemp);
  44.  
  45. }
  46.  
  47. function createUpcoming(data) {
  48.     const container = document.createElement('div');
  49.     container.classList.add('forecast-info');
  50.  
  51.     data.forecast.forEach(data => {
  52.         const spanHolder = generateSpans(data);
  53.         container.appendChild(spanHolder);
  54.     });
  55.  
  56.     return container;
  57. }
  58.  
  59. function generateSpans(data) {
  60.     const { condition, high, low } = data;
  61.     const spanHolder = document.createElement('span');
  62.     spanHolder.classList.add('upcoming');
  63.  
  64.     const iconSpan = document.createElement('span');
  65.     iconSpan.classList.add('symbol');
  66.     iconSpan.innerHTML = icons[condition];
  67.  
  68.     const tempSpan = document.createElement('span');
  69.     tempSpan.classList.add('forecast-data');
  70.     tempSpan.innerHTML = `${low}${icons['Degrees']}/${high}${icons['Degrees']}`;
  71.  
  72.     const conditionSpan = document.createElement('span');
  73.     conditionSpan.classList.add('forecast-data');
  74.     conditionSpan.textContent = condition;
  75.  
  76.     spanHolder.appendChild(iconSpan);
  77.     spanHolder.appendChild(tempSpan);
  78.     spanHolder.appendChild(conditionSpan);
  79.  
  80.     return spanHolder;
  81.  
  82. }
  83.  
  84. function createToday(data) {
  85.     const { condition, high, low } = data.forecast;
  86.  
  87.     const conditionContainer = document.createElement('div');
  88.     conditionContainer.classList.add('forecasts');
  89.  
  90.     const conditionIconSpan = document.createElement('span');
  91.     conditionIconSpan.classList.add('condition', 'symbol');
  92.     conditionIconSpan.innerHTML = icons[condition];
  93.  
  94.     const conditionSpan = document.createElement('span');
  95.     conditionSpan.classList.add('condition');
  96.  
  97.     const nameSpan = document.createElement('span');
  98.     nameSpan.classList.add('forecast-data');
  99.     nameSpan.textContent = data.name;
  100.  
  101.     const tempSpan = document.createElement('span');
  102.     tempSpan.classList.add('forecast-data');
  103.     tempSpan.innerHTML = `${low}${icons['Degrees']}/${high}${icons['Degrees']}`;
  104.  
  105.     const conditionTxtSpan = document.createElement('span');
  106.     conditionTxtSpan.classList.add('forecast-data');
  107.     conditionTxtSpan.textContent = condition;
  108.  
  109.     conditionSpan.appendChild(nameSpan);
  110.     conditionSpan.appendChild(tempSpan);
  111.     conditionSpan.appendChild(conditionTxtSpan);
  112.  
  113.     conditionContainer.appendChild(conditionIconSpan);
  114.     conditionContainer.appendChild(conditionSpan);
  115.  
  116.     return conditionContainer;
  117. }
  118.  
  119. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement