Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WeatherApp Harry & Ralph #2

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion assets/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ body {
a {
color: inherit;
text-decoration: none;
}
}
120 changes: 120 additions & 0 deletions assets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
const weatherAPIKey = "4afe87cd7eddc53b1473c2bc0ddb2e37";
const unsplashAPIKey =
"17de354439973eb34b4aae66cb3c27ceea000efc05a8c6c94da17edac9cd3a7b";
const form = document.querySelector("#search");
const mainImage = document.querySelector("#photo");
const thumbnails = document.querySelector("#thumbs");
const input = form.querySelector("#search-tf");
const conditions = document.querySelector("#conditions");
const creditUser = document.querySelector("#credit-user");
let imagesCollection = {};

function createRequest(service, request) {
return service === "weather"
? `http://api.openweathermap.org/data/2.5/weather?q=${request}&APPID=${weatherAPIKey}`
: `https://api.unsplash.com/search/photos?query=${request}&client_id=${unsplashAPIKey}`;
}

// api.openweathermap.org/data/2.5/weather?q=$london&APPID=4afe87cd7eddc53b1473c2bc0ddb2e37
function displayImage(imageData, size, numToShow) {
const imageArr = imageData.results;
let html = "";
if (numToShow) {
for (let i = 0; i < numToShow; i += 1) {
html += `
<img src="${imageArr[i].urls[size]}" />
`;

setBgColor(imageArr[i].color);
}
} else {
let markup = imageArr
.map((item, index) => {
return `
<li>
<a href="${item.urls["regular"]}" id="${
item.id
}" class="thumbs__link" alt="${
item.description ? item.description : conditions.textContent
}">
<img class="thumbs__image" src="${item.urls[size]}" />
</a>
</li>
`;
})
.join("");
html = `<ul class="thumbs__list">${markup}</ul>`;
}
return html;
}

function setBgColor(color) {
document.querySelector('#weather__data').style.backgroundColor = color;
}

function submitForm(e) {
e.preventDefault();
fetch(createRequest("weather", input.value))
.then(function(weatherResponse) {
return weatherResponse.json();
})
.then(function(weatherData) {
conditions.textContent = weatherData.weather[0].description;
document.querySelector('#weather__data-heading').innerHTML = `<h1>${weatherData.name}</h1>`;
document.querySelector(
"#weather__data-temp"
).innerHTML = `<strong>Temperature: </strong> ${(
weatherData.main.temp - 273.15
).toFixed(1)} Celsius`;
document.querySelector(
"#weather__data-wind-direction"
).innerHTML = `<strong>Wind direction: </strong> ${
weatherData.wind.deg
} deg`;
document.querySelector(
"#weather__data-wind-speed"
).innerHTML = `<strong>Wind speed: </strong> ${
weatherData.wind.speed
} mph`;
document.querySelector(
"#weather__data-pressure"
).innerHTML = `<strong>Pressure: </strong> ${
weatherData.main.pressure
} bar`;
document.querySelector(
"#weather__data-humidity"
).innerHTML = `<strong>Humidity: </strong> ${weatherData.main.humidity}%`;
return fetch(createRequest("unsplash", conditions.innerHTML + " " + weatherData.name));
})
.then(function(imageResponse) {
return imageResponse.json();
})
.then(function(imageData) {
imagesCollection = imageData;
mainImage.innerHTML = displayImage(imageData, "full", 1);
creditUser.textContent = imageData.results[0].user.name;
thumbnails.innerHTML = displayImage(imageData, "thumb");
})
.catch(function(error) {
console.log(error);
});
}

function thumbToMain(e) {
e.preventDefault();
if (e.target.className === "thumbs__image") {
const fullImg = mainImage.querySelector("img");
imagesCollection.results.forEach(function(image) {
if (image.id === e.target.parentNode.id) {
creditUser.textContent = image.user.name;
creditUser.setAttribute("href", image.user.links.html);
creditUser.setAttribute("target", "_blank");
setBgColor(image.color);
}
});
return (fullImg.src = e.target.parentNode.getAttribute("href"));
}
}

form.addEventListener("submit", submitForm);
thumbnails.addEventListener("click", thumbToMain);
42 changes: 37 additions & 5 deletions assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

overflow: hidden;
position: relative;
margin: 20px 0;
margin: 20px 0 0;
}

.photo::before {
Expand Down Expand Up @@ -78,9 +78,9 @@

/* Thumbs
------------------------------------------------------------------------------*/
.thumbs {
.thumbs ul {
display: flex;

list-style-type: none;
-webkit-overflow-scrolling: touch;
scroll-behavior: smooth;

Expand All @@ -91,9 +91,17 @@
background: rgba(0, 0, 0, 0.25);
}

.thumbs ul li {
overflow: hidden;
}

.thumbs ul li:hover {
cursor: pointer;
}

.thumbs__link {
flex: 0 0 auto;

display: block;
animation: 0.25s forwards fade-in;

width: var(--thumb-w);
Expand All @@ -103,6 +111,11 @@
background: rgba(0, 0, 0, 0.5);
}

.thumbs__link img {
width: 100%;
max-width: 100%;
}

.thumbs__link.active,
.thumbs__link:hover,
.thumbs__link:focus {
Expand Down Expand Up @@ -210,6 +223,25 @@
color: #fff;
}

/* Weather data */
.weather__data {
width: 240px;
background-color: #f1f1f1;
padding: 20px;
position: absolute;
right: 20px;
top: 54px;
z-index: 999;
}
.location-name {
text-align: center;
font-size: 2em;
margin-top: 1px;
padding-top: 0px;
}
.weather__data-heading h1 {
margin: 0;
}
/*----------------------------------------------------------------------------*/
/* Mediaquery overrides */
/*----------------------------------------------------------------------------*/
Expand All @@ -218,7 +250,7 @@
font-weight: 100;
}

.thumbs {
.thumbs ul {
justify-content: center;
}

Expand Down
13 changes: 12 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ <h1 class="title">
</h1>
</header>

<figure class="photo" id="photo"></figure>
<div id="weather__data" class="weather__data">
<div id="weather__data-heading" class="weather__data-heading"></div>
<div id="weather__data-temp" class="weather__data-temp"></div>
<div id="weather__data-wind-direction" class="weather__data-wind-direction"></div>
<div id="weather__data-wind-speed" class="weather__data-wind-speed"></div>
<div id="weather__data-pressure" class="weather__data-pressure"></div>
<div id="weather__data-humidity" class="weather__data-humidity"></div>
</div>

<figure class="photo" id="photo">
</figure>
<div class="location-name" id="location-name"></div>
<div class="info">
<p class="info__item info__item--conditions" id="conditions"></p>
<p class="info__item info__item--credits">
Expand All @@ -44,6 +54,7 @@ <h1 class="title">
</main>

<!-- JS goes here -->
<script src="./assets/index.js"></script>
</body>

</html>