Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/beach.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 assets/cold.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 assets/cool.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 assets/hot.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 assets/hot1.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 assets/warm.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,47 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weather Report</title>
<link rel="stylesheet" href="styles/index.css">
</head>
<body>
<section class = "temp_section">


<h1>The temperature in : <input placeholder="Enter a city" name = "name"/>
<p id ="values"></p> is <span id="Temperature"></span> </h1>
<button id="minusButton">cooler</button>
<button id="plusButton">hotter</button>
<p>
<button id="realTimeButton">Realtime weather</button>
</p>
<p>
<button id="resetButton">Reset City</button>
</p>
</section>

<section class = "sky_section">
<label>
Choose a sky:
<select class="sky" name="sky">
<option value="">Select One …</option>
<option value="Sunny">Sunny</option>
<option value="Cloudy">Cloudy</option>
<option value="Rainy">Rainy</option>
<option value="Snowy">Snowy</option>

</select>
</label>
<div class="result"></div>

</section>

<section class = "landscape">

<img id="newLandscape" src="assets/cold.jpeg" />
</section>


<script src="src/index.js" type="text/javascript"></script>
<script src="./node_modules/axios/dist/axios.min.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": "^0.27.2"
"axios": "^1.2.1"
}
}
128 changes: 128 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// import axios from 'axios';
'use strict';

const state = {
temp: 32,
};

const plusClickCount = () => {
const plusContainer = document.getElementById('Temperature');
state.temp += 1;
plusContainer.textContent = state.temp;
changeColor();
};

const minusClickCount = () => {
const minusContainer = document.getElementById('Temperature');
state.temp -= 1;
minusContainer.textContent = state.temp;
changeColor();
};
//making text color different
const changeColor = () => {
const tempColor = document.querySelector('#Temperature');
const newLS = document.getElementById('newLandscape');

if (state.temp < 49) {
tempColor.style.color = 'teal';
newLS.src = `assets/cold.jpeg`;
} else if (state.temp >= 50 && state.temp < 59) {
tempColor.style.color = 'green';
newLS.src = `assets/cool.jpeg`;
} else if (state.temp >= 60 && state.temp < 69) {
tempColor.style.color = 'yellow';
newLS.src = `assets/warm.jpeg`;
} else if (state.temp >= 70 && state.temp < 79) {
tempColor.style.color = 'orange';
newLS.src = `assets/beach.jpeg`;
} else if (state.temp >= 80) {
tempColor.style.color = 'red';
newLS.src = `assets/hot1.jpeg`;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The logic here is correct, the only thing that is that you didn't need to put state.temp >= 50 && state.temp < 59 and then (state.temp >= 60 && state.temp < 69). You could just do state.temp > 50 and state.temp > 60. These take care of the range between these two numbers.

};

//wave 4 axios call
const realTimeClick = () => {
let query = input.value;
// displayTemp();
findLocationWeather(query);
};

const displayTemp = () => {
let tempContainer = document.getElementById('Temperature');
tempContainer.textContent = state.temp;
};

const findLocationWeather = (query) => {
axios
.get('http://127.0.0.1:5000/location', {
params: {
format: 'json',
q: query,
},
})
.then((response) => {
let latitude = response.data[0].lat;
let longitude = response.data[0].lon;
axios
.get('http://127.0.0.1:5000/weather', {
params: {
format: 'json',
lat: latitude,
lon: longitude,
},
})
.then((response) => {
console.log('success in findWeather!', response.data);
const currentTemp = Math.floor(
(Number(response.data.main.temp) - 273.15) * 1.8 + 32
);
state.temp = currentTemp;
changeColor();
displayTemp();
})
.catch((error) => {
console.log('error in findweather!', error);
});
})
.catch((error) => {
console.log('error!', error);
});
};
Copy link
Copy Markdown

@mikellewade mikellewade Jan 11, 2023

Choose a reason for hiding this comment

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

You handled this nested promise chaining very well! However there is a way we can make this code more concise! Making use of the async & await functionality in Javascript:

const findLocation = async (query) => {
  try {
      const coordinates = await axios.get('http://127.0.0.1:5000/location', {
      params: {
        q: query,
        format: 'json',
      }})
  
      let latitude = coordinates.data[0].lat;
      let longitude = coordinates.data[0].lon;
     
      findWeather(latitude, longitude) 
    } catch (err) {
      console.log(err)
  }
} 

Essentially, the await keyword allows for us to stop the execution of the function we are in and wait for the promise it is attached to be fulfilled and resolved. Then we can save that returned data to a variable and go about our business! We will put this logic inside of try/catch blocks to handle errors. I also would suggest breaking the two functionalities (location and weather) into separate functions to avoid nesting.


//wave 5

const selectSky = document.querySelector('.sky');

selectSky.addEventListener('change', (event) => {
const result = document.querySelector('.result');
result.textContent = `The sky is ${event.target.value}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I love this use of the event object!

});

//wave 3 update text
const input = document.querySelector('input');
const log = document.getElementById('values');

input.addEventListener('input', updateValue);

function updateValue(e) {
log.textContent = e.target.value;
}
//wave 6
const resetCity = () => {
input.value = '';
log.textContent = '';
};

const registerEventHandlers = () => {
const hotterButton = document.getElementById('plusButton');
hotterButton.addEventListener('click', plusClickCount);
const coolerButton = document.getElementById('minusButton');
coolerButton.addEventListener('click', minusClickCount);
let realTimeButton = document.getElementById('realTimeButton');
realTimeButton.addEventListener('click', realTimeClick);
const reset = document.getElementById('resetButton');
reset.addEventListener('click', resetCity);
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✨✨✨


document.addEventListener('DOMContentLoaded', registerEventHandlers);
44 changes: 44 additions & 0 deletions styles/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
body {
display: grid;
grid-template-columns: 1fr 2fr;
grid-template-rows: auto auto ;
grid-gap: 1rem;
font-family: "Rubik", sans-serif;
font-size: 18px;
background-color: #a2b8df;
margin: 5rem;
}

.temp_section,.button_section {
background-color: hwb(102 67% 17%);
border: none;
color: green;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 10px;
grid-column: 1;
grid-row:1
}
.sky_section {
background-color: hwb(102 67% 17%);
border: none;
color: green;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
border-radius: 10px;
grid-column: 1;
grid-row:2
}
img{
width: 500px;
}
.landscape{
grid-column: 2;
grid-row:1/span 2
}
24 changes: 15 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.27.2:
version "0.27.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972"
integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==
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==
dependencies:
follow-redirects "^1.14.9"
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

combined-stream@^1.0.8:
version "1.0.8"
Expand All @@ -27,10 +28,10 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk=

follow-redirects@^1.14.9:
version "1.15.0"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.0.tgz#06441868281c86d0dda4ad8bdaead2d02dca89d4"
integrity sha512-aExlJShTV4qOUOL7yF1U5tvLCB0xQuudbf6toyYA0E/acBNw71mvjFTnLaRp50aQaYocMR0a/RMMBIHeZnGyjQ==
follow-redirects@^1.15.0:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

form-data@^4.0.0:
version "4.0.0"
Expand All @@ -52,3 +53,8 @@ mime-types@^2.1.12:
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
dependencies:
mime-db "1.52.0"

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==