Skip to content

weather api lab #1

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

Open
wants to merge 4 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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ and populate with as much data as you would like, based off of the API's respons
Remember, we'll need to add our Axios and JS files in the head of our HTML before we can do anything

```html
<head>
...
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script defer src="script.js"></script>
</head>

```

Now we can get working on our JS, which may look something like this
Expand Down
33 changes: 33 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


<head>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script defer src="script.js"></script>
<link rel="stylesheet" href="style.css"/>
</head>

<body>
<div></div>

<header>
<h1>check the weather</h1>

<input id="textInput" type="text" placeholder="enter city" />
<button id="submitButton"> Submit </button>
<div class="toggle-container">
<div class="toggle" onclick="toggle()">
<div class="circle"></div>
</div>
<span class="text">°F</span>
</div>
</header>
<div class = "weather-container">
<h1 id="cityName"> </h1>
<p id="countryName"></p>
<p id="temp"> </p>

</div>


</body>

51 changes: 51 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const apiKey = "288ddf51280c492b9cd221023230504"
const button = document.querySelector("#submitButton")
const input = document.querySelector("#textInput")
let cityName = document.querySelector("#cityName")
let countryName = document.querySelector("#countryName")
let cityTemp = document.querySelector("#temp")

let convertToC = false

const toggle = () => {
let toggle = document.querySelector('.toggle')
let text = document.querySelector('.text')
convertToC = !convertToC
if (convertToC) {
toggle.classList.add('active')
text.innerHTML = '°C'
button.click()
} else {
toggle.classList.remove('active')
text.innerHTML = '°F'
button.click()
}
}

button.addEventListener('click', async () => {
let city = input.value.toLowerCase();
let response = await axios.get(`http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${city}&aqi=no`)
let temp = response.data.current.temp_f
let tempC = response.data.current.temp_c
let feelsLike = response.data.current.feelslike_f
let feelsLikeC = response.data.current.feelslike_c
let condition = response.data.current.condition.text
let country = response.data.location.country

cityName.innerText = `${city.toUpperCase()}`
countryName.innerText = `(${country})`

if (convertToC == true){
cityTemp.innerText = `${condition} \n\n ${tempC} °C \n\n Feels like: ${feelsLikeC} °C`
}
else {
cityTemp.innerText = `${condition} \n\n ${temp} °F \n\n Feels like: ${feelsLike} °F`
}
})


document.querySelector('#textInput').addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
button.click()
}
})
81 changes: 81 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
body{
padding: 50px 0 0 0;
text-align: center;
background-image: url("https://images.unsplash.com/photo-1504253163759-c23fccaebb55?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1170&q=80");
background-size: 100%;
background-position: center;
background-repeat: no-repeat;
}

input{
padding: 5px;
text-align: center;
margin: 20 auto
}

button{
padding: 5px;
background-color: bd959f
}
p {
font-size: 18px;
}

.weather-container {
background-color: f1f4f9;
border: 5px;
margin: 60 auto;
padding: 20px;
width: 50%;
}

.toggle-container {
position: absolute;
align-items: center;
width:100%;
}

.text {
font-size: 18px;
color: #494949;
}

.toggle {
position: relative;
width: 40px;
height: 20px;
border: 2px solid #494949;
border-radius: 20px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: .3s;
margin: 0 auto

}

.circle {
position: absolute;
left: 0;
width: 20px;
height: 20px;
border-radius: 20px;
background-color: #494949;
transition: .3s;
}

.active {
border-color: blue;
}

.active + .text {
color: blue;
}

.active .circle {
left: 100%;
transform: translateX(-100%);
transition: .3s;
background-color: 494949;
}