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

Wilson-Munson-Amethyst-Weather-Report-C19 #60

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Binary file added assets/clouds/clouds.jpg
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 assets/clouds/puffyclouds.jpg
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 assets/clouds/rainclouds.jpg
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 assets/clouds/sunny.jpg
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 assets/landscapes/dunes.jpg
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 assets/landscapes/forest.jpg
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 assets/landscapes/grass.jpg
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 assets/landscapes/tundra.jpg
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 assets/wireframe.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 39 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,45 @@
<link rel="stylesheet" href="styles/index.css" />
</head>
<body>
<script src="./src/index.js"></script>
<main>
<header id="sky" class="sunny">
<section id="city-container">
<h1 id="city">Anamosa</h1>
<select name="" id="sky-dropdown">
<option value="">Please Choose An Option for the Sky</option>
<option value="cloudy">Cloudy</option>
<option value="puffy-cloud">Puffy Clouds</option>
<option value="storm-cloud">Storm Clouds</option>
<option value="sunny">Sunny</option>
</select>
</section>
<section id="content-container">
<section class="temp69To80Bk" id="temp-container">
<aside>
<p class="temp69To80" id="temp">72</p>
</aside>
<section id="temp-buttons">
<button id="temp-increase" type="button">▲</button>
<button id="temp-decrease" type="button">▼</button>
</section>
</section>
<section id="change-content-container">
<section id="city-choice-container">
<label for="city-choice">Input a City</label>
<input type="text" name="city-choice" id="city-choice">
</section>
<section id="button-container">
<button type="button" id="get-current-temp">Get Current Temp</button>
<button type="button" id="reset-city">Reset City</button>
</section>
</section>
</section>
<section id="landscape">
<img id='landscape-img' src="assets/landscapes/grass.jpg" alt="Happy cow in lush grassy green hills landscape">
</section>
</header>
</main>
<script src="./node_modules/axios/dist/axios.min.js"></script>
<script src="./src/index.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"axios": "^1.2.1"
"axios": "^1.4.0"
}
}
139 changes: 139 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
"use strict";
const state = {

Choose a reason for hiding this comment

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

Love how you all chose to use this psuedostate! I think that it is great practice for what you will see in React though it will be utilized differently!

tempEl: document.getElementById("temp"),
tempConEl: document.getElementById("temp-container"),
tempIncrease: document.getElementById("temp-increase"),
tempDecrease: document.getElementById("temp-decrease"),
tempVal: parseInt(document.getElementById("temp").textContent),
landscapeEl: document.getElementById("landscape"),
mainTitle: document.getElementById("city"),
inputField: document.getElementById("city-choice"),
getTempButton: document.getElementById("get-current-temp"),
skyDropdown: document.getElementById("sky-dropdown"),
skyEl: document.getElementById("sky"),
resetCityButton: document.getElementById("reset-city"),
}
Comment on lines +3 to +15

Choose a reason for hiding this comment

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

Pieces of code like this are usually found in an event handler that runs on the event DOMContentLoaded, basically once the page is html is loaded on the webpage you will run a bunch of initial logic such as grabbing elements and adding event handlers to them. It will look something like this:

document.addEventListener("DOMContentLoaded", function () {
    state.increaseTemp = document.querySelector("#increase-temp");
    state.decreaseTemp = document.querySelector("#decrease-temp");
    state.displayTemp = document.querySelector("#display-temp");
    state.resetButton = document.querySelector("#reset-button"); 
    state.searchButton = document.querySelector("#search-button");
 
    
    state.increaseTemp.addEventListener("click", increaseTemperature);
    state.decreaseTemp.addEventListener("click", decreaseTemperature);
    ...
}

This is also usually found at the bottom of the file too, that way you can put all your function definitions for event handlers above it! This also just adds an extra layer of protection when it comes to things loading in order.


// Wave2
const changeTempColor = () => {
if (state.tempVal > 80) {
state.tempEl.className = 'above80Temp';
state.tempConEl.className = 'above80Bk';
} else if (state.tempVal < 80 && state.tempVal > 69) {
state.tempEl.className = 'temp69To80';
state.tempConEl.className = 'temp69To80Bk';
} else if (state.tempVal < 70 && state.tempVal > 59) {
state.tempEl.className = 'temp59To70';
state.tempConEl.className = 'temp59To70Bk';
} else if (state.tempVal < 60 && state.tempVal > 49) {
state.tempEl.className = 'temp49To60';
state.tempConEl.className = 'temp49To60Bk';
} else if (state.tempVal < 50) {
state.tempEl.className = 'blow50';
state.tempConEl.className = 'blow50Bk';
};
}

const changeLandscape = () => {
const landscapeImage = document.getElementById("landscape-img");
if (state.tempVal >= 80) {
landscapeImage.src = 'assets/landscapes/dunes.jpg';
landscapeImage.alt = 'Desert dunes landscape';
} else if (state.tempVal < 80 && state.tempVal >= 70) {
landscapeImage.src = 'assets/landscapes/grass.jpg';
landscapeImage.alt = 'Happy cow in lush grassy green hills landscape';
} else if (state.tempVal < 70 && state.tempVal >= 33) {
landscapeImage.src = 'assets/landscapes/forest.jpg';
landscapeImage.alt = 'Foggy dense forest landscape';
} else {
landscapeImage.src = 'assets/landscapes/tundra.jpg';
landscapeImage.alt = 'Icy expansive landscape';
}
}

state.tempIncrease.addEventListener('click', (event) => {
event.preventDefault()

Choose a reason for hiding this comment

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

Usually you want to use the preventDefault method to prevent the default behavior of an event, however I don't see the reasoning for doing it here. Is there a reason as to why you want to prevent it from happening?

state.tempVal += 1;
state.tempEl.textContent = state.tempVal;
changeTempColor();
changeLandscape();
})

state.tempDecrease.addEventListener('click', (event) => {
event.preventDefault()
state.tempVal -= 1;
state.tempEl.textContent = state.tempVal;
changeTempColor();
changeLandscape();
})

//Wave 3
state.inputField.addEventListener('keyup', (event) => {
state.mainTitle.textContent = state.inputField.value
})

//Wave 4
let lat;
let lon;
const getLocation = () => {
// console.log(state.mainTitle);
const url = `http://127.0.0.1:5000/location`;
axios.get(url, {
params: {
q: state.mainTitle.textContent,
}})
.then((response) => {
console.log(response.data[0].lat, response.data[0].lon)
getWeather(response.data[0].lat, response.data[0].lon);
}
)
.catch((error) => {
console.log(error);
})
};
Comment on lines +76 to +93

Choose a reason for hiding this comment

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

Here's an alternative to promise chaining! You can use the async/await keywords to have your function wait until a promise is fulfilled and then you pass the results of that promise to other pieces of your function like assigning your return values as variables or other things like that. It looks something like this:

const findCityLocation = async () => {
    let lat;
    let lon;
    const resp = await axios.get('http://127.0.0.1:5000/location', {
        params: {
            q: document.getElementById("city").value
        }
    })
    
    try {
      [lat, lon] = resp.data[0]
      getWeather({ lat: lat, lon: lon});
    } catch (error) {
       console.log(error)
    }
};


const getWeather = (lat, lon) => {
return (axios.get('http://127.0.0.1:5000/weather', {
params: {
lat: lat,
lon: lon,
}})
.then((response) => {

console.log(response.data.main.temp)
const tempInF = (response.data.main.temp - 273.15) * (9/5) + 32;
state.tempVal = parseInt(tempInF);
// console.log(state.tempVal)
state.tempEl.textContent = state.tempVal;
changeTempColor();
changeLandscape();
})
.catch((error)=> {
console.log(error);
})
)
}
state.getTempButton.addEventListener('click', getLocation);

// WAVE 55555
const changeSky = () => {
const skyValue = state.skyDropdown.value;
console.log(skyValue);
if (skyValue === 'puffy-cloud') {
state.skyEl.className = 'puffy-clouds';
} else if (skyValue === 'cloudy') {
state.skyEl.className = 'cloudy';
} else if (skyValue === 'storm-cloud') {
state.skyEl.className = 'storm-cloud';
} else if (skyValue === 'sunny') {
state.skyEl.className = 'sunny';
}
};

state.skyDropdown.addEventListener('change', changeSky)

// WAVE 6
state.resetCityButton.addEventListener('click', () => {
state.mainTitle.textContent = 'Anamosa';
state.inputField.value = 'Anamosa';
})

Choose a reason for hiding this comment

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

Awesome job everyone! You really ate this one up! Please feel free to reach out to me if you have any questions about the comments I left or if you want to discuss anything in greater detail! ⭐️

112 changes: 112 additions & 0 deletions styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
* {
font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif
}
#city {
color:black
}

.above80Temp {
color: red;
}

.above80Bk {
background-color: salmon;
/* opacity: 50%; */
}

.temp69To80 {
color: teal;
}

.temp69To80Bk {
background-color: lightblue;
}

.temp59To70 {
color: chocolate;
}

.temp59To70Bk {
background-color: bisque;
}

.temp49To60 {
color: green;
}

.temp49To60Bk {
background-color: yellowgreen;
}

.blow50 {
color:teal;
}

.blow50Bk {
background-color: lightcyan;
}

#city-container {
display: flex;
flex-direction: row;
justify-content: space-between;
}

main {
height: 100vh;
flex:auto;
/* width: 50%; */
}

#sky-dropdown {
height: 1.5rem;
}

#city-container {
align-items: center;
padding:1rem;
}
#content-container {
display: flex;
/* flex-direction: row; */
justify-content: space-between;
padding: 1rem;
}

#temp-container {
display: flex;
}

#temp {
padding: .25rem;
}
#temp-buttons {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 25%;
padding: .5rem;
}

#change-content-container {
display: flex;
flex-direction: column;
}

img {
max-width: 100%;
}

.puffy-clouds {
background-image: url("../assets/clouds/puffyclouds.jpg");
}
.storm-cloud {
background-image: url("../assets/clouds/rainclouds.jpg");
}
.sunny {
background-image: url("../assets/clouds/sunny.jpg");
}
.cloudy {
background-image: url("../assets/clouds/clouds.jpg");
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.1.tgz#44cf04a3c9f0c2252ebd85975361c026cb9f864a"
integrity sha512-I88cFiGu9ryt/tfVEi4kX2SITsvDddTajXTOFmt2uK1ZVA8LytjtdeyefdQWEf5PU8w+4SSJDoYnggflB5tW4A==
axios@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.4.0.tgz#38a7bf1224cd308de271146038b551d725f0be1f"
integrity sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
Expand Down