Liliana797979

forecaster - js apllications

Nov 5th, 2021
1,205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function attachEvents() {
  4.     const forecast = document.getElementById('forecast');
  5.     const currentForecastLabel = document.querySelector('#forecast #current .label');
  6.     const upcomingForecastLabel = document.querySelector('#forecast #upcoming .label');
  7.     const upcomingSection = document.getElementById('upcoming');
  8.     const currentSection = document.getElementById('current');
  9.     const locationInput = document.getElementById('location');
  10.     const submitBtn = document.getElementById('submit');
  11.  
  12.     /*
  13.          Sunny          ☀ // ☀
  14.          Partly sunny   ⛅ // ⛅
  15.          Overcast       ☁ // ☁
  16.          Rain           ☔ // ☂
  17.          Degrees        °   // °
  18.     */
  19.  
  20.     const conditionChecker = {
  21.         'Sunny'() {
  22.             return '&#x2600';
  23.         },
  24.         'Partly sunny'() {
  25.             return '&#x26C5';
  26.         },
  27.         'Overcast'() {
  28.             return '&#x2601';
  29.         },
  30.         'Rain'() {
  31.             return '&#x2614';
  32.         },
  33.         'Degrees'() {
  34.             return '&#176';
  35.         }
  36.     }
  37.  
  38.     submitBtn.addEventListener('click', fetchWeatherData);
  39.  
  40.     async function fetchWeatherData() {
  41.         submitBtn.disabled = true;
  42.  
  43.         const currentForecasts = document.querySelector('.forecasts');
  44.         const upcomingForecast = document.querySelector('.forecasts-info');
  45.         const url = 'http://localhost:3030/jsonstore/forecaster/locations';
  46.  
  47.         if (currentForecasts) {
  48.             currentForecasts.replaceWith();
  49.             upcomingForecast.replaceWith();
  50.         }
  51.  
  52.         forecast.style.display = 'none';
  53.         upcomingSection.style.display = ''
  54.         currentForecastLabel.textContent = 'Loading...';
  55.         upcomingForecastLabel.textContent = 'Loading...';
  56.  
  57.         try {
  58.             const response = await fetch(url);
  59.             const data = await response.json();
  60.             const [searchedLocation] = data.filter(e => e.name.toLowerCase() === locationInput.value.toLowerCase());
  61.  
  62.             locationInput.value = '';
  63.  
  64.             if (searchedLocation === undefined || response.status !== 200) {
  65.                 throw new Error('Error')
  66.             }
  67.             forecast.style.display = 'block';
  68.             await Promise.all([showTodayForecast(searchedLocation), showUpcomingForecast(searchedLocation)]);
  69.         } catch (e) {
  70.             forecast.style.display = '';
  71.             currentForecastLabel.textContent = e.message;
  72.             console.log(e.message);
  73.             upcomingSection.style.display = 'none';
  74.         }
  75.         submitBtn.disabled = false;
  76.     }
  77.  
  78.     async function showTodayForecast(searchedLocation) {
  79.         const url = `http://localhost:3030/jsonstore/forecaster/today/${searchedLocation.code}`;
  80.  
  81.         const response = await fetch(url);
  82.         const data = await response.json();
  83.  
  84.         currentForecastLabel.textContent = 'Current conditions';
  85.  
  86.         const {forecast: {condition, high, low}} = data;
  87.         const cityLocation = data.name;
  88.  
  89.         let conditionCode = conditionChecker[condition]();
  90.  
  91.         const todayForecast = create('div', {className: 'forecasts'},
  92.             create('span', {className: 'condition symbol'}, conditionCode),
  93.             create('span', {className: 'condition'},
  94.                 create('span', {className: 'forecast-data'}, cityLocation),
  95.                 create('span', {className: 'forecast-data'}, `${low}${conditionChecker['Degrees']()}/${high}${conditionChecker['Degrees']()}`),
  96.                 create('span', {className: 'forecast-data'}, condition)
  97.             ));
  98.  
  99.         currentSection.appendChild(todayForecast);
  100.     }
  101.  
  102.     async function showUpcomingForecast(searchedLocation) {
  103.         const url = `http://localhost:3030/jsonstore/forecaster/upcoming/${searchedLocation.code}`;
  104.  
  105.         const response = await fetch(url);
  106.         const data = await response.json();
  107.  
  108.         upcomingForecastLabel.textContent = 'Three-day forecast';
  109.  
  110.         let upcomingForecast = document.createElement('div');
  111.         upcomingForecast.className = 'forecasts-info';
  112.  
  113.         for (let day = 1; day <= data['forecast'].length; day++) {
  114.             const {condition, high, low} = data['forecast'][day - 1];
  115.             const conditionCode = conditionChecker[condition]();
  116.  
  117.             const currentDayForecast = create('span', {className: 'upcoming'},
  118.                 create('span', {className: 'symbol'}, conditionCode),
  119.                 create('span', {className: 'forecast-data'}, `${low}${conditionChecker['Degrees']()}/${high}${conditionChecker['Degrees']()}`),
  120.                 create('span', {className: 'forecast-data'}, condition)
  121.             );
  122.             upcomingForecast.appendChild(currentDayForecast);
  123.         }
  124.         upcomingSection.appendChild(upcomingForecast);
  125.     }
  126.  
  127.     function create(type, attributes, ...content) {
  128.         const element = document.createElement(type);
  129.  
  130.         for (const property in attributes) {
  131.             element[property] = attributes[property];
  132.         }
  133.  
  134.         for (let el of content) {
  135.             if (typeof el === 'string' || typeof el === 'number') {
  136.                 if (el.startsWith('&') || Number.isInteger(Number(el[0]))) {
  137.                     element.innerHTML = el;
  138.                     continue;
  139.                 }
  140.                 el = document.createTextNode(el);
  141.             }
  142.             element.appendChild(el);
  143.         }
  144.         return element
  145.     }
  146. }
  147.  
  148. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment