From cc96bbc93ff42c8e15f562200705d6b4147f187f Mon Sep 17 00:00:00 2001 From: Anjali Date: Mon, 18 Jan 2021 17:19:31 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Implementacja=20wy=C5=9Bwietlania=20cytat?= =?UTF-8?q?=C3=B3w=20w=20index.html=20|=20index.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.js | 94 ++++++++++++++++++++++--------------------- src/scripts/quotes.js | 51 ++++++++++++++--------- 2 files changed, 82 insertions(+), 63 deletions(-) diff --git a/src/index.js b/src/index.js index 82be2f0..6012779 100644 --- a/src/index.js +++ b/src/index.js @@ -1,34 +1,33 @@ +import 'regenerator-runtime/runtime' //async/await with Parcel + +import { CheckWeatherMain, displayQuotes } from './scripts/quotes'; + // autolokalizacja -let localization = {lat: 50, lng: 19} +let localization = { lat: 50, lng: 19 } let findMebtn = document.getElementById("FindMeBtn"); let foramttedAdressParagraph = document.getElementById("info"); -findMebtn.addEventListener("click",getLocation); +findMebtn.addEventListener("click", getLocation); let temporaryCityInfo = document.getElementById("test"); - + // sprawdzam dlugosc i szerokosc geograficzna, a nastepnie wrzucam je google maps api function showPosition(position) { - let LAT = position.coords.latitude.toFixed(4); - let LNG = position.coords.longitude.toFixed(4); - - - Object.assign(localization, {lat: LAT, lng:LNG}); - - console.log(localization); - // tutaj wrzucamy getWeather - weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${localization.lat}&lon=${localization.lng}&appid=${API_KEY}`; - getWeather(); + let LAT = position.coords.latitude.toFixed(4); + let LNG = position.coords.longitude.toFixed(4); + + Object.assign(localization, { lat: LAT, lng: LNG }); + + console.log(localization); + // tutaj wrzucamy getWeather + getWeather(); } -function getLocation(){ - navigator.geolocation ? navigator.geolocation.getCurrentPosition(showPosition) : console.log("Brak wspracia wykrywania geolokalizacji w tej przeglÄ…darce"); - - - +function getLocation() { + navigator.geolocation ? navigator.geolocation.getCurrentPosition(showPosition) : console.log("Brak wspracia wykrywania geolokalizacji w tej przeglÄ…darce"); } //----------------------------------------------- const API_KEY = 'a53136f1a7cfa62997f97997cfb14cde'; let weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${localization.lat}&lon=${localization.lng}&appid=${API_KEY}`; -var exports = weatherData = { +var weatherData = { cityName: '', temp: 0, feelsTemp: 0, @@ -36,44 +35,49 @@ var exports = weatherData = { cloudsCoverage: 0 }; +const getAndDisplayQuotes = async () => { + const ContentElement = document.querySelector("#quote-content"); + const AuthorElement = document.querySelector("#quote-author"); + + let quote = await CheckWeatherMain(weatherData.weatherType); + + displayQuotes(quote, ContentElement, AuthorElement); +}; + const getWeather = async () => { try { + const weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${localization.lat}&lon=${localization.lng}&appid=${API_KEY}`; const response = await fetch(`${weatherApiUrl}`) if (response.ok) { const data = await response.json(); + setWeatherData(data); + + await getAndDisplayQuotes(); } } catch (error) { - window.alert(error) + window.alert(error) } } - const setWeatherData = (data) => { - weatherData.cityName = data.name; - weatherData.temp = data.main.temp; - weatherData.feelsTemp = data.main.feels_like; - weatherData.weatherType = data.weather[0].main; - weatherData.cloudsCoverage = data.clouds.all; - - insertWeatherDataToStage(); - }; - - const insertWeatherDataToStage = () => { - Object.keys(weatherData).forEach((key) => { - document.querySelector(`.${key} .data`) && - (document.querySelector( - `.${key} .data`, - ).innerText = `${weatherData[key]}`); - }); - }; - - console.log(weatherData); - -(() => { - -})(); +const setWeatherData = (data) => { + weatherData.cityName = data.name; + weatherData.temp = data.main.temp; + weatherData.feelsTemp = data.main.feels_like; + weatherData.weatherType = data.weather[0].main; + weatherData.cloudsCoverage = data.clouds.all; + insertWeatherDataToStage(); +}; -getWeather(); +const insertWeatherDataToStage = () => { + Object.keys(weatherData).forEach((key) => { + document.querySelector(`.${key} .data`) && + (document.querySelector( + `.${key} .data`, + ).innerText = `${weatherData[key]}`); + }); +}; +console.log(weatherData); diff --git a/src/scripts/quotes.js b/src/scripts/quotes.js index 890ce83..4c5a6bb 100644 --- a/src/scripts/quotes.js +++ b/src/scripts/quotes.js @@ -1,12 +1,4 @@ - -const btn = document.querySelector('.find'); -const quote = document.querySelector(".quote-content"); -const author = document.querySelector(".quote-author"); -// docelowo weather.main -let weather = data.weather[0].main -console.log(`weatherData w quotes.js`, weatherData); - -const getFromRonSwanson = async () => { +export const getFromRonSwanson = async () => { try { const response = await fetch( "https://ron-swanson-quotes.herokuapp.com/v2/quotes", @@ -21,23 +13,46 @@ const getFromRonSwanson = async () => { } }; -const getFromBreakingBad = async () => { +export const getFromBreakingBad = async () => { try { const response = await fetch( "https://breaking-bad-quotes.herokuapp.com/v1/quotes", ); if (response.ok) { const data = await response.json(); - quote.textContent = data['0'].quote; - author.textContent = data['0'].author; + return { + quote: data['0'].quote, + author: data['0'].author + }; } } catch (error) { - console.log(`Http error: ${response.status}`);; + console.log(`Http error: ${response.status}`); + return { + quote: '', + author: '', + }; } }; -// main button -btn.addEventListener('click', () => CheckWeatherMain(weather)); -function CheckWeatherMain(weather) { - (weather === "Rain" || weather === "Snow") ? getFromRonSwanson(): getFromBreakingBad(); -} \ No newline at end of file +export async function CheckWeatherMain(weather) { + return (weather === "Rain" || weather === "Snow") ? await getFromRonSwanson(): await getFromBreakingBad(); +} + +export function displayQuotes(quote, content, author) { + + content.textContent = quote.quote; + author.textContent = quote.author; +} + +// window.onload = () => { +// const ContentElement = document.querySelector("#quote-content"); +// const AuthorElement = document.querySelector("#quote-author"); + +// let weather = 'Rain'; + +// // main button +// btn.addEventListener('click', async () => { +// let quote = await CheckWeatherMain(weather); +// displayQuotes(quote, ContentElement, AuthorElement); +// }); +// } From 16abe6e91d07244563bf863e20022827f3d852b2 Mon Sep 17 00:00:00 2001 From: Anjali Date: Mon, 18 Jan 2021 17:28:35 +0100 Subject: [PATCH 2/3] =?UTF-8?q?:bug:=20drugie=20api=20prawid=C5=82owy=20zw?= =?UTF-8?q?rot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scripts/quotes.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/scripts/quotes.js b/src/scripts/quotes.js index 4c5a6bb..46c0f0b 100644 --- a/src/scripts/quotes.js +++ b/src/scripts/quotes.js @@ -5,11 +5,18 @@ export const getFromRonSwanson = async () => { ) if (response.ok) { const data = await response.json(); - quote.textContent = data - author.textContent = "Ron Swanson"; + + return { + quote: data, + author: "Ron Swanson", + }; } } catch (error) { console.log(`Http error: ${response.status}`); + return { + quote: '', + author: '', + }; } }; From 6fb0df1290563c5ca640de430fedaf733befef7e Mon Sep 17 00:00:00 2001 From: Anjali Date: Mon, 18 Jan 2021 19:53:30 +0100 Subject: [PATCH 3/3] temp dla celsius --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 6012779..d9747cb 100644 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ var weatherData = { const getAndDisplayQuotes = async () => { const ContentElement = document.querySelector("#quote-content"); const AuthorElement = document.querySelector("#quote-author"); - + // weatherData.weatherType = 'Snow' let quote = await CheckWeatherMain(weatherData.weatherType); displayQuotes(quote, ContentElement, AuthorElement); @@ -47,7 +47,7 @@ const getAndDisplayQuotes = async () => { const getWeather = async () => { try { - const weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${localization.lat}&lon=${localization.lng}&appid=${API_KEY}`; + const weatherApiUrl = `https://api.openweathermap.org/data/2.5/weather?lat=${localization.lat}&lon=${localization.lng}&appid=${API_KEY}&units=metric`; const response = await fetch(`${weatherApiUrl}`) if (response.ok) { const data = await response.json();