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

C22 - Phoenix - Luqi - Sphinx - Lorraine #8

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

shiyinxi
Copy link

@shiyinxi shiyinxi commented Dec 5, 2024

No description provided.

@shiyinxi shiyinxi changed the title C22- Sphinx-Luqi-Lorraine C22 - Phoenix - Luqi - Sphinx - Lorraine Dec 5, 2024
Copy link

@anselrognlie anselrognlie left a comment

Choose a reason for hiding this comment

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

Looks good! Please review my comments, and let me know if you have any questions. Nice job!

Comment on lines +133 to +134
document.addEventListener("DOMContentLoaded", registerEventHandlers);
document.addEventListener("DOMContentLoaded", resetCityName);

Choose a reason for hiding this comment

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

We can register multiple things to happen in response to an event, but typically, we'll just make a single function that calls the several things we'd like to do. So here, we could make a function setupPage that itself calls registerEventHandlers and resetCityName.

Comment on lines +106 to +107
const cityName = document.getElementById("cityNameInput").value;
const headerCityName = document.getElementById("headerCityName");

Choose a reason for hiding this comment

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

Rather than re-getting the elements I need each time a callback gets run, I like to fetch everything once one the page loads and store the references in some globals or in the state structure. That simplifies the code in the event callbacks a bit, and removes the chance of a typo in the selector leading to problems.

}

const increaseTemp = () => {
const temp = document.getElementById("tempValue");

Choose a reason for hiding this comment

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

Rather than re-getting the elements I need each time a callback gets run, I like to fetch everything once one the page loads and store the references in some globals or in the state structure. That simplifies the code in the venet callbacks a bit, and removes the chance of a typo in the selector leading to problems.

Comment on lines +8 to +11
const temp = document.getElementById("tempValue");
state.curTemp += 1;
temp.textContent = state.curTemp;
changeColorAndLandscape(state.curTemp);

Choose a reason for hiding this comment

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

Consider restructuring this function (and the decrease) so that all this does is modify the value of state.curTemp and then call another function to update any UI that depends on the current temperature value. Here, that would mostly mean combining the textContent logic, and the call to changeColorAndLandscape into a single helper (maybe publishTemperatureValue). This could be called in the increase/decrease, as well as after the live weather lookup rather than needing to remember to do multiple things in all those places.

changeColorAndLandscape(state.curTemp);
};

const changeColorAndLandscape = (temp) => {

Choose a reason for hiding this comment

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

Rather than having a function that directly modifies color and landscape elements, consider restructuring this into two functions, one that determines the color and lanscape to use based on the temperature, and another that actuall applies those values to the UI. Note that currently, each branch has a lot of repetition in the stpes it's doing, with the only difference being the values used

const findLatitudeAndLongitude = () => {
let latitude, longitude;
const cityName = document.getElementById("headerCityName").textContent;
axios.get('http://127.0.0.1:5000/location',

Choose a reason for hiding this comment

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

Be sure to return the promise chain. The only way to setup any code that needs to wait for this to finishbefore it runs is to get access to the promise chain we build here, and then .then chain onto the end of it. Never leave a promise chain "trapped" inside a helper method. Always return it.

}

const findWeather = (latitude, longitude) => {
axios.get('http://127.0.0.1:5000/weather', {

Choose a reason for hiding this comment

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

Here again we would want to return the promise chain. This would allow us to chain additional logic after the API call completes outside this function, rather than having this function need to be responsible both for making the API call and updating the UI. This function should only make the API call, and code elsewhere should chain onto the end of this promise chain with .then to add the logic that updates the UI.

})
.then((response) => {
const kelvinTemp = response.data.main.temp;
const fahrenheitTemp = Math.round(kelvinTemp * 9 / 5 - 459.67);

Choose a reason for hiding this comment

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

Consider moving the kelvin to fahrenheit conversion logic to a helper function.

const headerCityName = document.getElementById("headerCityName")
cityName.value = defaultCity;
headerCityName.textContent = defaultCity;
findLatitudeAndLongitude(defaultCity);

Choose a reason for hiding this comment

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

There was no requirement to update the realtime data when resetting the city, but it's easy to do since we have a helper function to handle that. Still, it's a little surprising that it does happen here, when it doesn't happen as we type the city name (we wait until the Get Realtime Temperature button is clicked).

const changeCityName = () => {
const cityName = document.getElementById("cityNameInput").value;
const headerCityName = document.getElementById("headerCityName");
headerCityName.textContent = cityName;

Choose a reason for hiding this comment

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

Simliar to my suggestion for updating values based on the temperature with a helper function, we could make a helper for updating the city name in the UI as well. Even though it's only a single step, it would still be more self-documenting to have a few function calls to something like publishCityName rather than scattered calls to set this textContent. And if we made a change in what happened when publishing the city name, we would then only need to update it in that one function rather than find every place where we updated the textContent directly.

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.

3 participants