Murove1

Untitled

Oct 31st, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. 'use strict'
  2.  
  3. const app = (() => {
  4. // Function for output city, temperature, hour, icon
  5. const renderWeather = function(city, temperature, weatherCode) {
  6. const wrapper = document.getElementById('wrapper');
  7.  
  8. wrapper.setAttribute('data-hour', getHours());
  9. wrapper.innerHTML += `<i class="${weatherIcon(weatherCode)}"></i>`;
  10. wrapper.innerHTML += `<span class="temperature">${temperature}&deg;C</span>`;
  11. wrapper.innerHTML += `<h1 class="city">${city}</h1>`;
  12. }
  13.  
  14. // Function for get weatherIcon
  15. const weatherIcon = function(weatherCode) {
  16. const time = getHours();
  17. const timeOfDay = (time > 7 && time < 18) ? 'day' : 'night';
  18. const className = `weather-icon wi wi-owm-${timeOfDay}-${weatherCode}`;
  19.  
  20. return className;
  21. }
  22.  
  23. // Function for get Hour
  24. const getHours = function() {
  25. const date = new Date();
  26. const time = date.getHours();
  27.  
  28. return time;
  29. }
  30.  
  31. // Fetch weather user
  32. const fetchWeather = city => {
  33. const baseUrl = 'http://api.openweathermap.org';
  34. const path = '/data/2.5/weather';
  35. const appId = '0daa83ea1b190102f2bc310b0c995c55';
  36. const query = `units=metric&lang=en&appid=${appId}`;
  37.  
  38. fetch(`${baseUrl}${path}?q=${city}&${query}`)
  39. .then(response => response.json())
  40. .then(data => renderWeather(data.name, Math.round(data.main.temp), data.weather[0].id))
  41. .catch(error => console.error(error));
  42. }
  43.  
  44. // Fetch city user
  45. const fetchCity = (() => {
  46. fetch('http://freegeoip.net/json/')
  47. .then(response => response.json())
  48. .then(({ city }) => fetchWeather(city))
  49. .catch(error => console.error(error));
  50. })();
  51. })();
Advertisement
Add Comment
Please, Sign In to add comment