Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>Weather Project</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
<link href="./style.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="page-header text-center">
<h1>Weather Project</h1>
</div>
</div>
</div>

<div class="row">
<div class="col-md-6 col-md-offset-3">
<form class="form-inline" onsubmit="event.preventDefault();">
<div class="form-group submit-city">
<label class="sr-only" for="location">Enter City Name Here</label>
<input class="form-control" id="location" type="text" placeholder="Enter City Name Here" />
</div>
<div class="form-group submit-btn">
<button id="submit-search" class="btn btn-primary">Search</button>
</div>
</form>
</div>
</div>

<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="container main-weather text-center">
</div>
</div>
</div>

<div class="row">
<div class="col-md-8 col-md-offset-2 no-padding-left">
<div class="container sub-weather text-center no-padding-left">
</div>
</div>
</div>
</div>
<script src="main.js"></script>
</body>
</html>
110 changes: 110 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const currentDate = new Date();

document.getElementById('submit-search').addEventListener('click', function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arrow function next time =)

cityName = document.getElementById('location').value;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch for definition if its const or let

fetchFromCityLocation(cityName);
});

const fetchWeatherData = lat, lon, city => {
const fetchUrl = `https://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=ca90637f0ed0bcfecc469a57ea89c230`;

fetch(fetchUrl, {
method: 'GET',
dataType: 'json'
}).then(data => data.json()).then(data => readWeatherData(data, city));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you know how to use arrow function :)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Data is too generic, consider using something specific like weatherData

Suggested change
}).then(data => data.json()).then(data => readWeatherData(data, city));
}).then(weatherData => weatherData.json()).then(jsonWeatherData => readWeatherData(jsonWeatherData, city));

}

const fetchFromCityLocation = city => {
const geoUrl = `https://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=1&appid=ca90637f0ed0bcfecc469a57ea89c230`;

fetch(geoUrl, {
method: 'GET',
dataType: 'json'
}).then(data => data.json()).then(data => fetchWeatherData(data[0].lat, data[0].lon, city));
}

const readWeatherData = city => {
const weatherData = [];

for(let i = 0; i < data.list.length; i += 8) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a great opportunity to use an array helper method like forEach or map

const currentWeatherData = data.list[i];

const weather = {
temp: ((currentWeatherData.main.temp - 273.15) * (9/5) + 32),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability, the calculation should have been in a function

main: currentWeatherData.weather[0].main
};

weatherData.push(weather);
}
renderWeather(weatherData, city);
}

const renderWeather = weatherData, city => {
if(document.querySelector('.main-weather').hasChildNodes() && document.querySelector('.sub-weather').hasChildNodes()) {
document.querySelector('.main-weather').replaceChildren();
document.querySelector('.sub-weather').replaceChildren();
}
Comment on lines +44 to +47

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for readability, move to a function clearCurrentWeatherDisplay() or something like this



for(let i = 0; i < weatherData.length; i++) {
const mainWeatherTemplate = `
<div class="row">
<div class="col-md-2 col-md-offset-2">
<ul class="list-unstyled">
<li class="text-center temperature">${Math.floor(weatherData[i].temp)}&deg;</li>
<li class="text-center location">${city}</li>
<li class="text-center description">${weatherData[i].main}</li>
</ul>
</div>
<div class="col-md-2">
<image src="https://openweathermap.org/img/wn/${returnWeatherIcon(weatherData[i].main)}d@2x.png" alt="${weatherData[i].main} Icon" />
</div>
</div>`;

const subWeatherTemplate = `
<div class="col-md-2">
<div class="sub-weather-item">
<ul class="list-unstyled">
<li class="text-center temperature">${Math.floor(weatherData[i].temp)}&deg;</li>
<li class="text-center description">${weatherData[i].main}</li>
</ul>
<image src="https://openweathermap.org/img/wn/${returnWeatherIcon(weatherData[i].main)}d@2x.png" alt="${weatherData[i].main} Icon" />
<p>${returnDayOfWeek(i)}</p>
</div>
</div>`;

if (i != 0) {
document.querySelector('.sub-weather').insertAdjacentHTML('beforeend', subWeatherTemplate);
} else {
document.querySelector('.main-weather').insertAdjacentHTML('beforeend', mainWeatherTemplate);
}
}
}

const returnWeatherIcon = description => {
switch(description) {
case 'Thunderstorm':
return '11';
case 'Drizzle':
return '09';
case 'Rain':
return '10';
case 'Snow':
return '13';
case 'Atmosphere':
return '50';
case 'Clear':
return '01';
case 'Clouds':
return '04';
}
}

const returnDayOfWeek = offset => {
let currentDay = currentDate.getDay() + offset;
if (currentDay > 6) {
currentDay -= 7;
}
return daysOfWeek[currentDay];
}
17 changes: 17 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.form-control {
margin: 10px 10px;
}

.no-padding-left {
padding-left: 0px;
}

.sub-weather-item {
border-style: solid;
border-color: black;
border-width: 2px;
}

#location {
width: 400px;
}