Advertisement
samimwebdev

weather-app-v-4

Oct 27th, 2021
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //User Interface/DOM related properties and methods
  2. const UI = {
  3.   loadSelector() {
  4.     const cityElm = document.querySelector('#city')
  5.     const cityInfoElm = document.querySelector('#w-city')
  6.     const iconElm = document.querySelector('#w-icon')
  7.     const temperatureElm = document.querySelector('#w-temp')
  8.     const pressureElm = document.querySelector('#w-pressure')
  9.     const humidityElm = document.querySelector('#w-humidity')
  10.     const feelElm = document.querySelector('#w-feel')
  11.     const formElm = document.querySelector('#form')
  12.     const countryElm = document.querySelector('#country')
  13.     const messageElm = document.querySelector('#messageWrapper')
  14.     return {
  15.       cityElm,
  16.       countryElm,
  17.       iconElm,
  18.       cityInfoElm,
  19.       temperatureElm,
  20.       pressureElm,
  21.       humidityElm,
  22.       feelElm,
  23.       formElm,
  24.       messageElm
  25.     }
  26.   },
  27.   hideMessage() {
  28.     const { messageElm } = this.loadSelector()
  29.     setTimeout(() => {
  30.       messageElm.innerHTML = ''
  31.     }, 2000)
  32.   },
  33.   showMessage(msg) {
  34.     const { messageElm } = this.loadSelector()
  35.     const elm = `<div class='alert alert-danger'>${msg}</div>`
  36.     messageElm.innerHTML = elm
  37.     //hiding message
  38.     this.hideMessage()
  39.   },
  40.   validateInput(city, country) {
  41.     if (city === '' || country === '') {
  42.       this.showMessage('please provide necessary information')
  43.       return false
  44.     }
  45.     return true
  46.   },
  47.   getInput() {
  48.     const { cityElm, countryElm } = this.loadSelector()
  49.     const city = cityElm.value
  50.     const country = countryElm.value
  51.  
  52.     //validation of input
  53.     const isValidated = this.validateInput(city, country)
  54.  
  55.     return { city, country, isValidated }
  56.   },
  57.   clearInput() {
  58.     const { cityElm, countryElm } = this.loadSelector()
  59.     cityElm.value = ''
  60.     countryElm.value = ''
  61.   },
  62.  
  63.   getIcon(iconCode) {
  64.     return 'https://openweathermap.org/img/w/' + iconCode + '.png'
  65.   },
  66.   async getAndPopulateUI() {
  67.     console.log(this)
  68.     //load data from localStorage
  69.     const { city, country } = storage.getData()
  70.     // setting to weatherData
  71.     weatherData.city = city
  72.     weatherData.country = country
  73.     //calling API
  74.     const data = await weatherData.getData()
  75.     //populate to UI
  76.     this.populateUI(data)
  77.   },
  78.   populateUI(data) {
  79.     const {
  80.       iconElm,
  81.       cityInfoElm,
  82.       temperatureElm,
  83.       pressureElm,
  84.       humidityElm,
  85.       feelElm
  86.     } = this.loadSelector()
  87.  
  88.     const { weather, main, name: cityName } = data
  89.  
  90.     const url = this.getIcon(weather[0].icon)
  91.  
  92.     //setting element
  93.     cityInfoElm.textContent = cityName
  94.     temperatureElm.textContent = `Temperature: ${main.temp}°C`
  95.     pressureElm.textContent = `Pressure: ${main.pressure} kpa`
  96.     humidityElm.textContent = `Humidity: ${main.humidity}`
  97.     feelElm.textContent = weather[0].main
  98.     iconElm.setAttribute('src', url)
  99.   },
  100.  
  101.   init() {
  102.     const { formElm } = this.loadSelector()
  103.     formElm.addEventListener('submit', async e => {
  104.       //prevent browser reload
  105.       e.preventDefault()
  106.       //take input
  107.       const { city, country, isValidated } = this.getInput()
  108.       //clear Input
  109.       this.clearInput()
  110.  
  111.       if (isValidated) {
  112.         //setting city and country
  113.         weatherData.city = city
  114.         weatherData.country = country
  115.  
  116.         //saving data to localStorage
  117.         storage.city = city
  118.         storage.country = country
  119.         //saving to localStorage
  120.         storage.saveData()
  121.         //getting data from API
  122.         const data = await weatherData.getData()
  123.         //populating UI
  124.         if (data) {
  125.           this.populateUI(data)
  126.         }
  127.       }
  128.     })
  129.  
  130.     window.addEventListener('DOMContentLoaded', () => {
  131.       console.log(this)
  132.       this.getAndPopulateUI()
  133.     })
  134.   }
  135. }
  136.  
  137. UI.init()
  138.  
  139. //temp data store and dealing
  140. const weatherData = {
  141.   city: '',
  142.   country: '',
  143.   APP_ID: 'd777b9cd1436c98f958dac1e189e4f09',
  144.   async getData() {
  145.     //Requesting data from server
  146.     try {
  147.       const res = await fetch(
  148.         `http://api.openweathermap.org/data/2.5/weather?q=${this.city},${this.country}&units=metric&appid=${this.APP_ID}`
  149.       )
  150.       const data = await res.json()
  151.  
  152.       if (data.cod >= 400) {
  153.         //error
  154.         UI.showMessage(data.message)
  155.         return false
  156.       } else {
  157.         return data
  158.       }
  159.     } catch (err) {
  160.       UI.showMessage('Problem in fetching weather')
  161.     }
  162.   }
  163. }
  164.  
  165. //localStorage
  166. const storage = {
  167.   city: '',
  168.   country: '',
  169.   saveData() {
  170.     localStorage.setItem('BD_WEATHER_CITY', this.city)
  171.     localStorage.setItem('BD_WEATHER_COUNTRY', this.country)
  172.   },
  173.   getData() {
  174.     const city = localStorage.getItem('BD_WEATHER_CITY') || 'Dhaka'
  175.     const country = localStorage.getItem('BD_WEATHER_COUNTRY') || 'BD'
  176.     return { city, country }
  177.   }
  178. }
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement