-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c6b8b3
commit 421ff7f
Showing
8 changed files
with
170 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
import axios from 'axios'; | ||
import moment from 'moment'; | ||
|
||
export default class Forecast { | ||
constructor(id) { | ||
this.id = id; | ||
constructor(name, data) { | ||
this.name = name; | ||
this.data = data; | ||
} | ||
|
||
getID() { | ||
return this.id; | ||
} | ||
async getForecast() { | ||
const proxy = process.env.PROXY; | ||
const api = process.env.APIKEY; | ||
let query; | ||
if (this.data.length === 1) { | ||
query = `${proxy}api.openweathermap.org/data/2.5/forecast?id=${ | ||
this.data[0] | ||
}&units=metric&appid=${api}`; | ||
} else { | ||
query = `${proxy}api.openweathermap.org/data/2.5/forecast?lat=${ | ||
this.data[0] | ||
}&lon=${this.data[1]}&units=metric&appid=${api}`; | ||
} | ||
try { | ||
const res = await axios.get(`${proxy}${query}`); | ||
|
||
static info() { | ||
return 'This is a test'; | ||
// Get only 1 result for each day since API returns data each 3 hours for 5 days | ||
// Not the most elegant way here | ||
const weather = res.data.list.reduce((acc, el, i) => { | ||
if ([0, 7, 15, 23, 31, 39].includes(i)) { | ||
const data = { | ||
date: moment | ||
.unix(el.dt) | ||
.utc() | ||
.format('dddd, Do MMMM'), | ||
temp: Math.round(el.main.temp), | ||
temp_max: Math.round(el.main.temp_max), | ||
temp_min: Math.round(el.main.temp_min), | ||
name: el.weather[0].main, | ||
icon: el.weather[0].icon, | ||
}; | ||
acc.push(data); | ||
} | ||
return acc; | ||
}, []); | ||
this.weather = weather; | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { elements } from './base'; | ||
|
||
// Render Initial view | ||
export const renderView = name => { | ||
const markup = ` | ||
<div class="forecast open"> | ||
<button class="close-popup"> | ||
<svg class="close-popup--icon"> | ||
<use xlink:href="./img/symbol-defs.svg#icon-plus"></use> | ||
</svg> | ||
</button> | ||
<div class="title"> | ||
<svg class="title__icon"> | ||
<use xlink:href="./img/symbol-defs.svg#icon-location"></use> | ||
</svg> | ||
<h1 class="title__text"> | ||
${name} | ||
</h1> | ||
</div> | ||
<div class="title__text--subtitle">Forecast for the next 5 days</div> | ||
<div class="days"> | ||
</div> | ||
</div> | ||
`; | ||
elements.container.innerHTML = markup; | ||
}; | ||
|
||
// Render each result | ||
export const renderResult = (result, container) => { | ||
const markup = ` | ||
<div class="cities__weather"> | ||
<div class="cities__weather__name">${result.date}</div> | ||
<div class="cities__weather__details"> | ||
<img | ||
src="./img/weather/iconfinder_weather-04_1530389.svg" | ||
class="cities__weather__details--icon" | ||
/> | ||
<div class="cities__weather__details__text"> | ||
<div class="cities__weather__details__text--phrase"> | ||
${result.name} | ||
</div> | ||
<div class="cities__weather__details__text--minmax"> | ||
${result.temp_min}ºC <span class="dot">•</span> ${result.temp_max}ºC | ||
</div> | ||
</div> | ||
<div class="cities__weather__details__temp"> | ||
${result.temp}ºC | ||
</div> | ||
</div> | ||
</div> | ||
`; | ||
container.insertAdjacentHTML('beforeEnd', markup); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters