-
-
Notifications
You must be signed in to change notification settings - Fork 40
NW6| Fikret Ellek | [TECH ED] JavaScript Challenges | Week-3 #13
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"; | ||
| 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) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great job using the |
||
| 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> | ||
There was a problem hiding this comment.
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 :)