Skip to content

Quote #14

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

Open
wants to merge 4 commits into
base: DisplayWeatherDataFromAPI
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
94 changes: 49 additions & 45 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,83 @@
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,
weatherType: '',
cloudsCoverage: 0
};

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);
};


const getWeather = async () => {
try {
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();

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);
62 changes: 42 additions & 20 deletions src/scripts/quotes.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,65 @@

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",
)
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: '',
};
}
};

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();
}
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);
// });
// }