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

Teraesa Metivier - Weather App Project #19

Open
wants to merge 2 commits into
base: main
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
3 changes: 2 additions & 1 deletion cypress.json
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{}
{"defaultCommandTimeout": 30000,
"requestTimeout": 30000}
110 changes: 110 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather App</title>
<link rel="stylesheet" href="stylesheet.css" />
<script defer src="index.js"></script>
</head>
<body>
<div class="container-1">
<header id="header">
<h1 >Weather App</h1>

<form>
<label for="locationID" id="locationID">Pick a Location</label>
<input id="locationID" name="location" type="text" />
<br>
<br>
<input type="submit" onclick="toggle()"/>
</form>

</header>
</div>
<!--will contain a temperature conversion widget starts empty-->
<div class="container-2">
<aside id="widget" class="inactive">
<form id="convert-temp">
<label>Convert the temperature:</label>
<input type="number" id="temp-to-convert"/>
<label for="to-c">To Celsius</label>
<input type="radio" id="to-c" name="convert-temp" value="c" />
<br>
<label>To Fahrenheit</label>
<input type="radio" id="to-f" name="convert-temp" value="f"/>
<br>
<br>
<input type="submit" id="temp-button"/>
<!--store the result of the calculation below-->
<h4 id="convert-h4">0.00</h4>
</form>


</aside>
</div>


<main id="main">

<!--contains current weather and it starts empty-->
<article id="current-weather" >
<p id="current-weather-p" class="active">Choose a location to view the weather.</p>
<h4 id="input-location"></h4>
<p id="area"></p>
<p id="region"></p>
<p id="country"></p>
<p id="feels-like"></p>
</article>
<div class="for-future-weather">
<aside class="inactive" id="future-weather">
<!--upcoming weather-->
<article id="today">
<h4 id="today-title">Today</h4>
<p id="today-avg"></p>
<p id="today-max"></p>
<p id="today-min"></p>
</article>
<article id="tomorrow">
<h4 id="tomorrow-title">Tomorrow</h4>
<p id="tomorrow-avg"></p>
<p id="tomorrow-max"></p>
<p id="tomorrow-min"></p>
</article>
<article id="day-after">
<h4 id="day-after-title">Day After Tomorrow</h4>
<p id="day-after-avg"></p>
<p id="day-after-max"></p>
<p id="day-after-min"></p>
</article>
</aside>
</div>
</main>


<div class="container-4">
<aside id="aside">
<!-- Choose a location to view the weather -->
<section >

<h4>Previous Searches</h4>
</section>
</aside>

<sidebar id="sidebar">
<p id="sidebar-ptag">No searches have been made yet</p>
<ul>

</ul>
</sidebar>
</div>
</body>
</html>







207 changes: 207 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
const BASE_URL = "https://www.wttr.in";

//here we want to grab the input (form) and place in a variable
let inputForm = document.querySelector("form");
inputForm.addEventListener("submit", (event) => {
event.preventDefault();
//input is the country/place typed in to input field
const input = inputForm.querySelector('input[id="locationID"]').value;

console.log(input);
fetch(`${BASE_URL}/${input}?format=j1`)
.then((response) => response.json())
.then((json) => {
console.log(json);
createLocationInformation(json);
})
.catch((error) => {
console.log(error);
});
inputForm.reset();
});

//here we want to fetch the url and receive the json object with the information and place the information in the html to be printed to screen
const pickAlocationId = (input) => {
console.log(input);
fetch(`${BASE_URL}/${input}?format=j1`)
.then((response) => response.json())
.then((json) => {
console.log(json);
createLocationInformation(json);
});
};

//here we want to
const createLocationInformation = (json) => {
//name of location
const h4 = document.getElementById("input-location");
// //Area
const p1 = document.getElementById("area");
// //Region
const p2 = document.getElementById("region");
// //Country
const p3 = document.getElementById("country");
// //currently feels like
const p4 = document.getElementById("feels-like");
//ptag
const p5 = document.getElementById("current-weather-p");

const widgetSection = document.getElementById("widget");

//replace innerContent of tags with information from json
h4.textContent = `${json.nearest_area[0].areaName[0].value}`;
p1.textContent = `Area: ${json.nearest_area[0].areaName[0].value}`;
p2.textContent = `Region: ${json.nearest_area[0].region[0].value}`;
p3.textContent = `Country: ${json.nearest_area[0].country[0].value}`;

p4.textContent = `Currently: Feels like ${json.current_condition[0].FeelsLikeF} °F`;
//this toggles the ptag for choose location
p5.classList.add("inactive");
p5.classList.remove("active");

//toggles the widget
widgetSection.classList.add("active");
widgetSection.classList.remove("inactive");

previousSearches(json);

//return article;
showInfoNextThreeDays(json);
};

const showInfoNextThreeDays = (json) => {
const weatherForThreeDays = document.getElementById("future-weather");
weatherForThreeDays.classList.add("active");
//text = Today
const todayTag = document.getElementById("today-title");

//text = Average Temperature
const avg = document.getElementById("today-avg");

//Text = Max temp
const max = document.getElementById("today-max");

//text - min temp
const min = document.getElementById("today-min");

const article2 = document.getElementById("tomorrow");

//text = Tomorrow
const tomorrowTag = document.getElementById("tomorrow-title");

//text = Average Temperature
const average = document.getElementById("tomorrow-avg");

//Text = Max temp
const maximum = document.getElementById("tomorrow-max");

//text - min temp
const minimum = document.getElementById("tomorrow-min");

const article3 = document.getElementById("day-after");

//text = day after
const dayAfterTag = document.getElementById("day-after-title");

//text = Average Temperature
const avgTemperature = document.getElementById("day-after-avg");

//Text = Max temp
const maxTemperature = document.getElementById("day-after-max");

//text - min temp
const minTemperature = document.getElementById("day-after-min");

//text content - Article 1 - data taken from Array 1

//text = Average Temperature
avg.textContent = `Average Temperature: ${json.weather[0].avgtempF}°F`;

//Text = Max temp
max.textContent = `Max Temperature ${json.weather[0].maxtempF}°F`;

//text - min temp;
min.textContent = `Min Temperature: ${json.weather[0].mintempF}°F`;

//text content - Article 2

//text = Average Temperature
average.textContent = `Average Temperature: ${json.weather[1].avgtempF}°F`;

//Text = Max temp
maximum.textContent = `Max Temperature: ${json.weather[1].maxtempF}°F`;

//text - min temp
minimum.textContent = `Min Temperature: ${json.weather[1].mintempF}°F`;

//text content - Article 3
//text = day after Array 3
// dayAfterTag.textContent = `Day After Tomorrow`;

//text = Average Temperature
avgTemperature.textContent = `Average Temperature:${json.weather[2].avgtempF}°F`;

//Text = Max temp
maxTemperature.textContent = `Max Temperature: ${json.weather[2].maxtempF}°F`;

//text - min temp
minTemperature.textContent = `Min Temperature: ${json.weather[2].mintempF}°F`;

//console.log(minTemperature);
};

//stores previous searches in the h4 tag
const previousSearches = (json) => {
const ul = document.querySelector("ul");
const li = document.createElement("li");
const atag = document.createElement("a");

atag.setAttribute("href", "#main");
atag.setAttribute("alt", main);

li.textContent = `${json.nearest_area[0].country[0].value}: ${json.current_condition[0].FeelsLikeF} °F`;

atag.append(li);
ul.append(atag);
return li;
};

let convertTempForm = document.getElementById("convert-temp");
convertTempForm.addEventListener("submit", (event) => {
event.preventDefault();
let convertTempInput = convertTempForm.querySelector(
'input[id="temp-to-convert"]'
).value;

let toCelsius = document.querySelector('input[id="to-c"]:checked');

let toFahrenheit = document.querySelector('input[id="to-f"]:checked');

let radios = document.getElementsByName("convert-temp");

const converttag = document.getElementById("convert-h4");

for (let i = 0; i < radios.length; i++) {
if (radios[i].checked) {
let celsiusTemp = (Number(convertTempInput) - 32) * (5 / 9);

converttag.textContent = `${celsiusTemp.toFixed(2)}°C`;
} else {
let farenheitTemp = (Number(convertTempInput) * 9) / (5 + 32);

converttag.textContent = `${farenheitTemp.toFixed(2)}°F`;
}
}

convertTempForm.append(converttag);
});

const toggle = () => {
let toggleInfo = document.getElementById("sidebar-ptag");
let ultag = document.querySelector("ul");
if (toggleInfo.style.display === "none" && ultag.children === "undefined") {
toggleInfo.style.display = "block";
} else {
toggleInfo.style.display = "none";
}
};
Loading