├── .gitignore ├── README.md ├── docs └── screenshot.png ├── src ├── app │ ├── index.css │ ├── Weather.js │ ├── UI.js │ ├── Store.js │ └── index.js └── index.html ├── webpack.config.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Weather App Javascript 2 |  3 | -------------------------------------------------------------------------------- /docs/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fazt/weather-app-javascript/HEAD/docs/screenshot.png -------------------------------------------------------------------------------- /src/app/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: #8360c3; /* fallback for old browsers */ 3 | background: -webkit-linear-gradient(to right, #2ebf91, #8360c3); /* Chrome 10-25, Safari 5.1-6 */ 4 | background: linear-gradient(to right, #2ebf91, #8360c3); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */ 5 | } 6 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | const HtmlWebpackPlugin = require('html-webpack-plugin'); 3 | 4 | module.exports = { 5 | entry: './src/app/index.js', 6 | output: { 7 | path: path.join(__dirname,'dist'), 8 | filename: 'bundle.js' 9 | }, 10 | devServer: { 11 | port: 3000 12 | }, 13 | module: { 14 | rules: [ 15 | { 16 | test: /\.css$/, 17 | use: ['style-loader', 'css-loader'] 18 | } 19 | ] 20 | }, 21 | plugins: [ 22 | new HtmlWebpackPlugin({ 23 | template: './src/index.html' 24 | }) 25 | ], 26 | }; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "weather-app-js", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "webpack-dev-server --mode=development", 8 | "clean": "rm -rf ./dist", 9 | "build": "webpack -p --progress" 10 | }, 11 | "keywords": [], 12 | "author": "", 13 | "license": "ISC", 14 | "dependencies": { 15 | "css-loader": "^1.0.1", 16 | "html-webpack-plugin": "^3.2.0", 17 | "style-loader": "^0.23.1", 18 | "webpack": "^4.25.1", 19 | "webpack-cli": "^3.1.2", 20 | "webpack-dev-server": "^3.1.10" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/Weather.js: -------------------------------------------------------------------------------- 1 | export class Weather { 2 | 3 | constructor(city, countryCode) { 4 | this.apiKey = 'f56dca24ce05c3835045624436d6a8e8'; 5 | this.city = city; 6 | this.countryCode = countryCode; 7 | } 8 | 9 | async getWeather() { 10 | const URI = `https://api.openweathermap.org/data/2.5/weather?q=${this.city},${this.countryCode}&appid=${this.apiKey}&units=metric`; 11 | const response = await fetch(URI); 12 | const data = await response.json(); 13 | return data; 14 | } 15 | 16 | changeLocation(city, countryCode) { 17 | this.city = city; 18 | this.countryCode = countryCode; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/app/UI.js: -------------------------------------------------------------------------------- 1 | export class UI { 2 | 3 | constructor() { 4 | this.location = document.getElementById('weather-location'); 5 | this.desc = document.getElementById('weather-description'); 6 | this.string = document.getElementById('weather-string'); 7 | this.humidity = document.getElementById('weather-humidity'); 8 | this.wind = document.getElementById('weather-wind'); 9 | } 10 | 11 | render(weather) { 12 | console.log(weather); 13 | this.location.textContent = weather.name + ' / ' + weather.sys.country; 14 | this.desc.textContent = weather.weather[0]['description']; 15 | this.string.textContent = weather.main.temp + ' °C'; 16 | this.humidity.textContent = 'Humidity: ' + weather.main.humidity + '°C'; 17 | this.wind.textContent = 'Weather ' + weather.wind.speed + ' m/s'; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/app/Store.js: -------------------------------------------------------------------------------- 1 | export class Store { 2 | constructor() { 3 | this.city; 4 | this.countryCode; 5 | this.defaultCity = 'London'; 6 | this.defaultCountry = 'uk'; 7 | } 8 | 9 | getLocationData() { 10 | if (localStorage.getItem('city') === null) { 11 | this.city = this.defaultCity; 12 | } else { 13 | this.city = localStorage.getItem('city'); 14 | } 15 | if (localStorage.getItem('countryCode') === null) { 16 | this.countryCode = this.defaultCountry; 17 | } else { 18 | this.countryCode = localStorage.getItem('countryCode'); 19 | } 20 | return { 21 | city: this.city, 22 | countryCode: this.countryCode 23 | } 24 | } 25 | 26 | setLocationData(city, countryCode) { 27 | localStorage.setItem('city', city); 28 | localStorage.setItem('countryCode', countryCode); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/app/index.js: -------------------------------------------------------------------------------- 1 | const { Store } = require('./Store'); 2 | const { Weather } = require('./Weather'); 3 | const { UI } = require('./UI'); 4 | 5 | const storage = new Store(); 6 | const {city, countryCode}= storage.getLocationData(); 7 | const weather = new Weather(city, countryCode); 8 | 9 | // User Interface 10 | const ui = new UI(); 11 | 12 | require('./index.css'); 13 | 14 | async function fetchWeather() { 15 | const data = await weather.getWeather() 16 | ui.render(data); 17 | } 18 | 19 | document.getElementById('w-change-btn').addEventListener('click', (e) => { 20 | const city = document.getElementById('city').value; 21 | const countryCode = document.getElementById('countryCode').value 22 | weather.changeLocation(city, countryCode); 23 | storage.setLocationData(city, countryCode); 24 | fetchWeather(); 25 | 26 | }); 27 | 28 | document.addEventListener('DOMContentLoaded', fetchWeather); 29 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |