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

C17-Sharks-Raha #66

Open
wants to merge 7 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
LOCATION_KEY="pk.8e923bdd6caecdd41fb592621e6457ec"
WEATHER_KEY="8ef694b082e1cf1d9e6407167bd6a7b5"
Comment on lines +1 to +2

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.

Binary file removed ada-project-docs/assets/cloudy-62.png
Binary file not shown.
Binary file removed ada-project-docs/assets/example-duckduckgo.png
Binary file not shown.
Binary file added ada-project-docs/assets/images.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ada-project-docs/assets/rainy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ada-project-docs/assets/snowy.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ada-project-docs/assets/spring.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ada-project-docs/assets/sunny.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/atlanta-captions-for-instagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/cartoon-desert-010.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/download.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/freezing.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/images (1).jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/images.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/rainy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/snowy.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/spring.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/sunny.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/warm.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/weather.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/winter-cartoon-png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 52 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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>&copy;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>
138 changes: 138 additions & 0 deletions src/index.js
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 event data like so:

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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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 div containing the error in case it's useful to an end user.

};

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';

Choose a reason for hiding this comment

The 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);
Loading