Advertisement
Guest User

Untitled

a guest
Dec 21st, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     let conditions = {
  3.         'Sunny': '☀',
  4.         'Partly sunny': '⛅',
  5.         'Overcast': '☁',
  6.         'Rain': '☂',
  7.     };
  8.  
  9.     const location = document.getElementById('location');
  10.     const forecast = document.getElementById('forecast');
  11.  
  12.     document.getElementById('submit').addEventListener('click', getWeather);
  13.  
  14.     async function getWeather() {
  15.         try {
  16.             const url = `http://localhost:3030/jsonstore/forecaster/locations`;
  17.  
  18.             const response = await fetch(url);
  19.             const data = await response.json();
  20.  
  21.             if (!response.ok) {
  22.                 throw new Error(`${response.status} (${response.statusText})`);
  23.             }
  24.  
  25.             const town = data.find(x => x.name.toLowerCase() == location.value.toLowerCase());
  26.  
  27.             if (!town) {
  28.                 throw new Error('Error (Invalid town name)!')
  29.             }
  30.  
  31.             forecast.style.display = "block";
  32.  
  33.             if (town) {
  34.                 getWeatherToday(town.code);
  35.                 getWeatherUpcoming(town.code);
  36.  
  37.                 location.value = '';
  38.             }
  39.         } catch (error) {
  40.             forecast.style.display = "block";
  41.             forecast.innerHTML = `<p id="errorMessage">${error.message}</p>`
  42.         }
  43.     }
  44.  
  45.     async function getWeatherToday(code) {
  46.         if (document.getElementById('errorMessage')) {
  47.             document.getElementById('errorMessage').remove();
  48.         }
  49.  
  50.         const url = `http://localhost:3030/jsonstore/forecaster/today/${code}`;
  51.  
  52.         const response = await fetch(url);
  53.         const data = await response.json();
  54.  
  55.         const current = document.getElementById('current');
  56.  
  57.         let divCurr = document.createElement('div')
  58.         divCurr.setAttribute('class', 'forecasts')
  59.         divCurr.innerHTML = `
  60.           <span class="condition symbol">${conditions[data.forecast.condition]}</span>
  61.           <span class="condition">
  62.             <span class="forecast-data">${data.name}</span>
  63.             <span class="forecast-data">${data.forecast.low}°/${data.forecast.high}°</span>
  64.             <span class="forecast-data">${data.forecast.condition}</span>
  65.           </span>  
  66.         `
  67.         current.appendChild(divCurr)
  68.     }
  69.  
  70.     async function getWeatherUpcoming(code) {
  71.         const url = `http://localhost:3030/jsonstore/forecaster/upcoming/${code}`;
  72.  
  73.         const response = await fetch(url);
  74.         const data = await response.json();
  75.  
  76.         const upcoming = document.getElementById('upcoming');
  77.  
  78.       const divForecastInfo = document.createElement('div');
  79.       divForecastInfo.setAttribute('class', 'forecast-info')
  80.       for (const x of data.forecast) {
  81.         divForecastInfo.innerHTML += `
  82.         <span class="upcoming">
  83.         <span class="symbol">${conditions[x.condition]}</span>
  84.         <span class="forecast-data">${x.low}°/${x.high}°</span>
  85.         <span class="forecast-data">${x.condition}</span>
  86.         </span>
  87.         `
  88.  
  89.       }
  90.       upcoming.appendChild(divForecastInfo);
  91.  
  92.     }
  93.   }
  94.  
  95.   attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement