-
Notifications
You must be signed in to change notification settings - Fork 88
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
C17-Sharks-Raha #66
base: main
Are you sure you want to change the base?
C17-Sharks-Raha #66
Changes from all commits
d225955
780ae19
3d0a27a
d5ab2f0
bc7321c
f15e299
a40d11d
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
LOCATION_KEY="pk.8e923bdd6caecdd41fb592621e6457ec" | ||
WEATHER_KEY="8ef694b082e1cf1d9e6407167bd6a7b5" | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,60 @@ | |
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="stylesheet" href="styles/index.css"> | ||
<title>Weather Report</title> | ||
</head> | ||
<body> | ||
|
||
<header> | ||
<div class="text"> | ||
<h1>Weather Report </h1> | ||
<h2 id="cityQuote"> Hotlanta! The City of Sweet tea and Sunshine</h2> | ||
</div> | ||
</header> | ||
<section class="boxes"> | ||
<div class="grid-container "> | ||
<div class="grid-item"> | ||
|
||
<button onclick="getCurrentTemp()"> Get Current Temperature</button> | ||
<br><br><br> | ||
<label id="temperature"></label> | ||
|
||
<br><br> | ||
<button onclick="increaseTemp()">Increase Temperature</button> | ||
<button onclick="decreaseTemp()">Decrease Temperature</button> | ||
</div> | ||
<div class="grid-item"> | ||
<label for="cityid">CITY</label> | ||
<br> | ||
<input type="text" placeholder="Enter a City" id="cityname"> | ||
<br> | ||
<button onclick="resetCity()"> Reset City </button> | ||
Comment on lines
+21
to
+34
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. We should try to avoid invoking functions in our HTML so that this file's sole responsibility it just to display UI (practicing separating of concerns). We can achieve this by setting event listeners on with elements with specific id's. Here is how we'd change this button to have an id, and how we would apply an event listener to that button in Javascript: HTML <button id="reset"> Reset City </button> Javascript const resetCity = function () {
city = 'Atlanta';
document.querySelector('#cityname').value = 'Atlanta';
const curWeatherHeader = document.getElementById('cityQuote');
curWeatherHeader.textContent = 'Hotlanta! The City of Sweet tea and Sunshine';
getCurrentTemp();
updatteLandBasedonTemp();
};
const registerEventHandlers = () => {
// This function will contain all the event listeners we add to elements in index.html
const resetCityButton = document.getElementById('reset');
resetCityButton.addEventListener('click', resetCity);
}
// This line executes registerEventHandlers immediately on load.
document.addEventListener('DOMContentLoaded', registerEventHandlers); |
||
</div> | ||
<div class="grid-item"> | ||
<label id="sky">SKY</label> | ||
<br> | ||
<select id="skytype"> | ||
<option value="">Select a Sky</option> | ||
<option value="cloudy">Cloudy</option> | ||
<option value="rainy">Rainy</option> | ||
<option value="snowy">Snowy</option> | ||
<option value="sunny">Sunny</option> | ||
</select> | ||
<div id = "skyIllustration" class="grid-item-2"></div> | ||
</div> | ||
</div> | ||
</section> | ||
<section class="boxes2"> | ||
<div id = "skyContent" class="grid-item-1"> | ||
<!-- <section id="weatherfarm"><br>LAND</section> --> | ||
|
||
</div> | ||
</section> | ||
<br> | ||
<footer> | ||
<p>©2022;Raha Rastegar</p> | ||
</footer> | ||
<script src="./src/index.js" type="text/javascript"></script> | ||
<script src="./node_modules/axios/dist/axios.min.js"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
'use strict'; | ||
// const axios = require('axios'); | ||
|
||
window.onload = () => { | ||
newTemperature(); | ||
changeCity(); | ||
changeSky(); | ||
}; | ||
|
||
let city = 'Atlanta'; | ||
let temperature = 70; | ||
Comment on lines
+10
to
+11
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. Consider storing these "global" variables in an object. This simulates how 'state' is managed in most frontend frameworks and is a common pattern you'll see in industry. |
||
|
||
const increaseTemp = () => { | ||
temperature += 1; | ||
newTemperature(); | ||
}; | ||
|
||
const decreaseTemp = () => { | ||
temperature -= 1; | ||
newTemperature(); | ||
}; | ||
Comment on lines
+13
to
+21
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. Notice how there's only one line of code that differs between these two functions. We could combine these two functions and identify the exact element that was clicked using const changeTemp = (e) => {
const degreeCountContainer = document.getElementById("temperature");
if (e.target.id == 'temp-up') {
state.temp += 1;
}
else if (e.target.id == 'temp-down') {
state.temp -= 1;
}
newTemperature();
};
const registerEventHandlers = () => {
const tempUp = document.getElementById('temp-up');
tempUp.addEventListener('click', changeTemp);
const tempDown = document.getElementById('temp-down');
tempDown.addEventListener('click', changeTemp);
} |
||
|
||
const getCurrentTemp = () => { | ||
let latitude; | ||
let longitude; | ||
|
||
const weatherData = axios.get('http://localhost:5001/location', { params: { q: city } }) | ||
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. Consider moving the url into a constant variable towards the top of this file to improve readability. |
||
.then((response) => { | ||
latitude = response.data[0].lat; | ||
longitude = response.data[0].lon; | ||
axios | ||
.get('http://localhost:5001/weather', { | ||
params: { lat: latitude, lon: longitude }, | ||
}) | ||
.then((response) => { | ||
const kelvin = response.data.current.temp; | ||
const fahrenheit = (9 / 5) * (kelvin - 273) + 32; | ||
temperature = Math.round(fahrenheit); | ||
newTemperature(); | ||
}) | ||
Comment on lines
+28
to
+40
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. Good work performing the nested API call using the locationIQ API call and the weather API. |
||
.catch((error) => { | ||
console.log('error'); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.log('error :('); | ||
}); | ||
Comment on lines
+41
to
+47
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. Nice error handling. Perhaps we can write more descriptive errors such as 'temperature not found' or 'location not found'. We could also display a |
||
}; | ||
|
||
const resetCity = function () { | ||
city = 'Atlanta'; | ||
document.querySelector('#cityname').value = 'Atlanta'; | ||
const curWeatherHeader = document.getElementById('cityQuote'); | ||
curWeatherHeader.textContent = 'Hotlanta! The City of Sweet tea and Sunshine'; | ||
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. ☀️🫖 |
||
getCurrentTemp(); | ||
updatteLandBasedonTemp(); | ||
}; | ||
|
||
const changeCity = () => { | ||
// if the #cityname element is changed | ||
const input = document.querySelector('#cityname'); | ||
input.addEventListener('change', updateValue); | ||
|
||
// update header to display city name and update city variable | ||
const curWeatherHeader = document.getElementById('cityQuote'); | ||
function updateValue(e) { | ||
city = e.target.value; | ||
curWeatherHeader.textContent = 'Current Weather for ' + city; | ||
getCurrentTemp(); | ||
} | ||
}; | ||
|
||
const changeSky = () => { | ||
const input = document.querySelector('#skytype-select'); | ||
|
||
input.addEventListener('change', (event) => { | ||
const skyOutput = document.querySelector('#sky'); | ||
skyOutput.textContent = getSky(event.target.value); | ||
}); | ||
}; | ||
|
||
const newTemperature = () => { | ||
const temperatureMessage = temperature + '\u00B0F'; | ||
document.getElementById('temperature').innerHTML = temperatureMessage; | ||
updatteLandBasedonTemp(); | ||
}; | ||
|
||
const updatteLandBasedonTemp = () => { | ||
let img; | ||
let tempColor = ''; | ||
if (temperature >= 80) { | ||
tempColor = 'red'; | ||
img = 'desert' | ||
|
||
} else if (temperature >= 70) { | ||
tempColor = 'orange'; | ||
img = 'spring'; | ||
} else if (temperature >= 60) { | ||
tempColor = 'yellow'; | ||
img = 'warm'; | ||
} else if (temperature >= 50) { | ||
tempColor = 'green'; | ||
img = 'winter'; | ||
} else if (temperature < 50) { | ||
tempColor = 'teal'; | ||
img = 'freez'; | ||
} | ||
document.getElementById('temperature').style.color = tempColor; | ||
document.queryCommandValue("#skyContent"); | ||
skyContent.classList = `grid-item-1 ${img}`; | ||
} | ||
|
||
|
||
const updateSky = () => { | ||
const optionSky = document.querySelector('#skytype').value; | ||
const skyContainer = document.querySelector('#sky'); | ||
let img; | ||
if (optionSky === 'cloudy') { | ||
img = 'cloudy'; | ||
} else if (optionSky === 'sunny') { | ||
img = 'sunny'; | ||
} else if (optionSky === 'rainy') { | ||
img = 'rainy'; | ||
} else if (optionSky === 'snowy') { | ||
img = 'snowy' | ||
} | ||
|
||
skyContainer.textContent = ''; | ||
const skyContent = document.querySelector('#skyIllustration'); | ||
skyContent.classList = `grid-item-2 ${img}`; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
const skySelect = document.querySelector("#skytype"); | ||
skySelect.addEventListener('change', updateSky); |
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.
This is dangerous, your API keys are open to the public and vulnerable to strangers using your account! You need to create a
.gitignore
file and add.env
to it in order to prevent local environment files from being pushed publicly.