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

C17-Sharks-Raha #66

wants to merge 7 commits into from

Conversation

Ramora6627
Copy link

No description provided.

Copy link

@audreyandoy audreyandoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work Raha! Project grade: 🟢

Key takeaways:

  • Event listeners are our friend! They search for HTML elements to apply functions to instead of requiring us to invoke functions in our HTML files. We can clean up the HTML file by removing all function calls and replacing them with event listners in index.js. I provided an example of how in the PR.
  • Always make sure you have a .gitignore file to add .env file to. Never push up your .env file to github! This leaves your API keys vulnerable for others to steal!
  • venv/ is NOT needed for javascript projects, only for python projects. Be wary of what you push inside of PRs!

Other than that keep up the great work Raha!

Comment on lines +1 to +2
LOCATION_KEY="pk.8e923bdd6caecdd41fb592621e6457ec"
WEATHER_KEY="8ef694b082e1cf1d9e6407167bd6a7b5"

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.

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.

Comment on lines +41 to +47
.catch((error) => {
console.log('error');
});
})
.catch((error) => {
console.log('error :(');
});

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.

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.

☀️🫖

Comment on lines +21 to +34
<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>

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

Comment on lines +10 to +11
let city = 'Atlanta';
let temperature = 70;

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.

Comment on lines +13 to +21
const increaseTemp = () => {
temperature += 1;
newTemperature();
};

const decreaseTemp = () => {
temperature -= 1;
newTemperature();
};

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

Comment on lines +28 to +40
.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();
})

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.

@audreyandoy
Copy link

audreyandoy commented Jul 20, 2022

Also great work with the design of your weather report! The layout is really neat and I love the images you chose for the landscape and the sky!! I'd say my one feedback is to make the font a little bit bigger so it's easier to read the buttons and options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants