diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..b8bc70f Binary files /dev/null and b/.DS_Store differ diff --git a/.gitignore b/.gitignore index 6704566..665baf0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,104 +1 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Microbundle cache -.rpt2_cache/ -.rts2_cache_cjs/ -.rts2_cache_es/ -.rts2_cache_umd/ - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env -.env.test - -# parcel-bundler cache (https://parceljs.org/) -.cache - -# Next.js build output -.next - -# Nuxt.js build / generate output -.nuxt -dist - -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and *not* Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# TernJS port file -.tern-port +apikeys.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..1f2035e --- /dev/null +++ b/app.js @@ -0,0 +1,87 @@ +// DOM elements for current weather +const place = document.querySelector('.placeName'); +const currentWeatherDiv = document.querySelector('.currentWeather'); +// DOM elements for future weather +const weatherPic = document.querySelector('img'); +const futureDates = document.querySelectorAll('.date'); +const icons = document.querySelectorAll('.icons'); +const minWeather = document.querySelectorAll('.min'); +const maxWeather = document.querySelectorAll('.max'); +const loader = document.querySelector('.loader'); +const weekday = new Array(7); +weekday[0] = "Sunday"; +weekday[1] = "Monday"; +weekday[2] = "Tuesday"; +weekday[3] = "Wednesday"; +weekday[4] = "Thursday"; +weekday[5] = "Friday"; +weekday[6] = "Saturday"; +//get browser's location +const getBrowsersLocation = async () => { + navigator.geolocation.getCurrentPosition(passLocation, error); + +} +function error(){ + placeName.innerText = "please enable location"; +} + +// +const passLocation = async (position) => { + try{ + const {latitude, longitude} = position.coords; + const res = await axios.get(`http://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=${APILocationKey}`); + const {data: + {results: + [{components: + {city: city, "state_code": stateCode}}] }} = res; + place.innerHTML = `${city}, ${stateCode}`; + getAllWeather(longitude, latitude); + }catch(e){ + console.log(e); + } +} + +const getAllWeather = async (longitude, latitude) => { + try { + const res = await axios.get(`http://api.openweathermap.org/data/2.5/onecall?lat=${latitude}&lon=${longitude}&exclude=minutely,hourly&units=imperial&appid=${APIKey}`); + + getCurrentWeather(res); + getDailyWeather(res); + } + catch (e) { + console.log(e); + } +} + +const getCurrentWeather = async (res) => { + //assign res current weather data to a variable + const {data: + {current : + {temp: temperature, + weather: + [{icon: iconlogo}] + }}} = res; + // show icon value on html + currentWeatherDiv.innerText = Math.trunc(temperature); + weatherPic.src = `http://openweathermap.org/img/wn/${iconlogo}@2x.png`; + +} + +const getDailyWeather = async (res) => { + res.data.daily.map((i) => ({date: i.dt, min:i.temp.min, max:i.temp.max, icon:i.weather[0].icon})).map(convertDate).forEach(showDay); + loader.classList.add('loading--inactive'); +} + +function convertDate ({date, min, max, icon }){ + return {day:weekday[new Date(date * 1000).getDay()], + min, max, icon} +} + +function showDay({day, min,max,icon},index){ + futureDates[index].innerText = day; + icons[index].src = `http://openweathermap.org/img/wn/${icon}@2x.png`; + minWeather[index].innerText = Math.trunc(min); + maxWeather[index].innerText = Math.trunc(max); +} + +getBrowsersLocation(); \ No newline at end of file diff --git a/index.html b/index.html index 3233671..64c27b0 100644 --- a/index.html +++ b/index.html @@ -1,11 +1,111 @@ - -
- - -loading...
+