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
5 changes: 2 additions & 3 deletions challenge-weather-app/assets/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
------------------------------------------------------------------------------*/
.thumbs {
display: flex;

gap: 10px;
-webkit-overflow-scrolling: touch;
scroll-behavior: smooth;

Expand All @@ -106,7 +106,7 @@
.thumb.active,
.thumb:hover,
.thumb:focus {
outline: 1px solid white;
outline: 3px solid black;
}

.thumb {
Expand All @@ -125,7 +125,6 @@
background: rgba(0, 0, 0, 0.5);
}


/* Credits
------------------------------------------------------------------------------*/
.info {
Expand Down
156 changes: 112 additions & 44 deletions challenge-weather-app/index.html
Original file line number Diff line number Diff line change
@@ -1,49 +1,117 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Meteoropolis</title>
<link rel="stylesheet" href="assets/globals.css">
<link rel="stylesheet" href="assets/styles.css">
</head>

<body>
<!-- Structure -->
<main class="content">
<header class="header">
<h1 class="title">
<i>Meteor</i>
<i>opolis</i>
</h1>
</header>

<figure class="photo" id="photo"></figure>

<div class="info">
<p class="info__item info__item--conditions" id="conditions"></p>
<p class="info__item info__item--credits">
<a href="#" id="credit-user"></a>
<span>on</span>
<a href="#" id="credit-platform">Unsplash</a>
</p>
</div>

<div class="thumbs" id="thumbs"></div>

<div class="controls">
<form class="search" id="search">
<label class="search__label" for="search-tf">City</label>
<input class="search__input" id="search-tf" name="city" placeholder="Enter city name" autocomplete="city" />
<button class="btn search__btn">Go</button>
</form>
</div>
</main>

<!-- JS goes here -->
</body>
<title>Meteoropolis</title>
<link rel="stylesheet" href="assets/globals.css" />
<link rel="stylesheet" href="assets/styles.css" />
</head>

<body>
<!-- Structure -->
<main class="content">
<header class="header">
<h1 class="title">
<i>Meteor</i>
<i>opolis</i>
</h1>
</header>

<figure class="photo" id="photo"></figure>

<div class="info">
<p class="info__item info__item--conditions" id="conditions"></p>
<p class="info__item info__item--credits">
<a href="#" id="credit-user"></a>
<span>on</span>
<a href="#" id="credit-platform">Unsplash</a>
</p>
</div>

<div class="thumbs" id="thumbs"></div>

<div class="controls">
<form class="search" id="search">
<label class="search__label" for="search-tf">City</label>
<input class="search__input" id="search-tf" name="city" placeholder="Enter city name" autocomplete="city" />
<button class="btn search__btn">Go</button>
</form>
</div>
</main>

<!-- JS goes here -->

<script>
const apiKey = "2d2afc913f90548905b086803a05ef5d";
const accessKey = "m89STg41g63uXj4rt_9u5GXtN9LTGDH_ViVVX9Sy9Yc";
Comment on lines +48 to +49
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Currently, we are storing keys in the codebase which is not secure as you can imagine. We will learn to store them somewhere else in the future. Please remember this. No need to any action for now- just info :)

const searchInput = document.getElementById("search-tf");
const thumbCont = document.getElementById("thumbs");
let linkCont = {};
const figure = document.getElementById("photo");

document.getElementById("search").addEventListener("submit", (event) => {
event.preventDefault();
getFetchCity(searchInput.value, apiKey);
});

function getFetchCity(input, key) {
fetch(`https://api.openweathermap.org/data/2.5/weather?q=${input}&APPID=${key}`)
.then((response) => response.json())
.then((data) =>
data.weather[0].description ? getFetchPhoto(data.weather[0].description) : console.log("no city")
);
}
function getFetchPhoto(input) {
fetch(`https://api.unsplash.com/search/photos?query=${input}&client_id=${accessKey}`)
.then((response) => response.json())
.then((data) => {
generateThumbs(data.results);
getMainPhotoLinks(data.results);
generateMainPhoto(linkCont);
thumbCont.addEventListener("click", (event) => {
const imgId = event.target.id;
generateMainPhoto(linkCont, imgId);
});
});
}

function generateThumbs(photos) {
thumbCont.innerHTML = "";
photos.map((photo) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a little tricky. If you don't need to return data from your loop, you can use forEach instead of map. Use map when you want to do some calculation and return data.

const img = document.createElement("img");
img.src = photo.urls.thumb;
img.id = photo.id;
img.className = "thumb";
thumbCont.append(img);
});
}

function getMainPhotoLinks(photos) {
linkCont = {};
photos.map((photo) => (linkCont[`${photo.id}`] = photo.urls.regular));
console.log(linkCont);
}

function generateMainPhoto(links) {
figure.innerHTML = "";
const mainImg = document.createElement("img");
mainImg.src = Object.values(links)[0];
figure.append(mainImg);
}

function generateMainPhoto(links, selectedImgId) {
//there is already css rule about this if (selectedImgId != thumbCont.id) {
figure.innerHTML = "";
//there is already css rule about this document.querySelectorAll(".thumb").forEach((element) => (element.style.border = "0px"));
const mainImg = document.createElement("img");
mainImg.src = links[selectedImgId] ?? Object.values(links)[0];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Great job using the nullish-?? operator here! Well done

figure.append(mainImg);
//there is already css rule about this document.getElementById(selectedImgId ?? Object.keys(links)[0]).style.border = "3px solid black";
//there is already css rule about this }
}
</script>
</body>
</html>