|
1 | 1 | import './App.css';
|
2 | 2 | import "axios";
|
| 3 | +import { useState } from 'react'; |
| 4 | +import axios from 'axios'; |
3 | 5 |
|
4 | 6 | function App() {
|
| 7 | + |
| 8 | + const [weather,setWeather]=useState(""); |
| 9 | + const apikey="c6b22ba82ac0661c3d775b2062b31250"; |
| 10 | + |
| 11 | + const apiCall=async(event)=>{ |
| 12 | + event.preventDefault(); |
| 13 | + const loc=event.target.elements.loc.value; |
| 14 | + const res=await axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${loc}&units=metric&appid=${apikey}`); |
| 15 | + setWeather({ |
| 16 | + descp: res.data.weather[0].description, |
| 17 | + temp:res.data.main.temp, |
| 18 | + city:res.data.name, |
| 19 | + icon:res.data.weather[0].icon, |
| 20 | + pressure:res.data.main.pressure, |
| 21 | + humidity:res.data.main.humidity, |
| 22 | + mintemp:res.data.main.temp_min, |
| 23 | + maxtemp:res.data.main.temp_min, |
| 24 | + windspeed:res.data.wind.speed, |
| 25 | + }); |
| 26 | + }; |
| 27 | + // let k=weather.temp; |
| 28 | + // let c=k.toFixed(2); |
5 | 29 | return (
|
6 |
| - <div class = "weatherApp"> |
7 |
| - |
8 |
| - |
9 |
| - |
| 30 | + <div className = "weatherApp"> |
10 | 31 | <div className="Todays_weather">
|
11 |
| - <div className="temp">16 °</div> |
| 32 | + <div className="temp">{weather.temp} °</div> |
12 | 33 | <div className="city_datetime">
|
13 |
| - <span className="city">London</span> |
| 34 | + <span className="city">{weather.city}</span> |
14 | 35 | <span className="datetime">06:09-Monday 9 sep'19</span>
|
15 | 36 | </div>
|
16 | 37 | <div className="weather">
|
17 |
| - <img src="http://openweathermap.org/img/wn/10d@2x.png" alt="weather icon" className="w-icon" height="50px" width="50px"></img> |
18 |
| - <span>Cloudy</span> |
| 38 | + <img src={`http://openweathermap.org/img/wn/${weather.icon}@2x.png`} alt="weather icon" className="w-icon" height="50px" width="50px"></img> |
| 39 | + <span>{weather.descp}</span> |
19 | 40 | </div>
|
20 | 41 |
|
21 | 42 | </div >
|
22 | 43 |
|
23 |
| -<div class="App"> |
| 44 | +<div className="App"> |
24 | 45 | <div>
|
25 | 46 |
|
26 |
| - <div class="input"> |
27 |
| - <form class="d-flex"> |
28 |
| - <input class="form-control me-2" type="search" placeholder="Search Here" aria-label="Search" /> |
29 |
| - <button class="btn" type="submit">Search</button> |
| 47 | + <div className="input"> |
| 48 | + <form className="d-flex" onSubmit={apiCall}> |
| 49 | + <input className="form-control me-2" type="search" placeholder="Search Here" aria-label="Search" name="loc"/> |
| 50 | + <button className="btn" type="submit">Search</button> |
30 | 51 | </form>
|
31 | 52 |
|
32 | 53 |
|
33 | 54 |
|
34 | 55 | </div>
|
35 | 56 | <hr></hr>
|
36 |
| - <div class="page">Todays weather</div> |
| 57 | + <div className="page">Todays weather</div> |
37 | 58 | <table>
|
| 59 | + <tbody> |
| 60 | + <tr> |
| 61 | + <th>Min Temp</th> |
| 62 | + <td>{weather.mintemp}°</td> |
| 63 | + </tr> |
38 | 64 | <tr>
|
39 |
| - <th>Clouds</th> |
40 |
| - <td>50%</td> |
| 65 | + <th>Max Temp</th> |
| 66 | + <td>{weather.maxtemp}°</td> |
41 | 67 | </tr>
|
42 | 68 | <tr>
|
43 |
| - <th>Density</th> |
44 |
| - <td>5.3Kg/m<sup>3</sup></td> |
| 69 | + <th>Presssure</th> |
| 70 | + <td>{weather.pressure}</td> |
| 71 | + </tr> |
| 72 | + <tr> |
| 73 | + <th>Humidity</th> |
| 74 | + <td>{weather.humidity}</td> |
45 | 75 | </tr>
|
46 | 76 | <tr>
|
47 | 77 | <th>WindSpeed</th>
|
48 |
| - <td>20mph</td> |
| 78 | + <td>{weather.windspeed}</td> |
49 | 79 | </tr>
|
| 80 | + </tbody> |
50 | 81 | </table>
|
51 | 82 | <hr></hr>
|
52 |
| - <div class="page">Cities</div> |
53 |
| - {/* <div class = "cities"> |
| 83 | + <div className="page">Cities</div> |
| 84 | + {/* <div className = "cities"> |
54 | 85 | <button id="butn1">Button</button>
|
55 | 86 | <button id="butn2">Button</button>
|
56 | 87 | <button id="butn3">Button</button>
|
57 | 88 | <button id="butn4">Button</button>
|
58 | 89 | </div> */}
|
59 |
| - <div class = "cities"> |
60 |
| - <div><button class="butn">Pune</button></div> |
61 |
| - <div><button class="butn">Kolkata</button></div> |
62 |
| - <div><button class="butn">London</button></div> |
63 |
| - <div><button class="butn">Paris</button></div> |
| 90 | + <div className = "cities"> |
| 91 | + <div><button className="butn">Pune</button></div> |
| 92 | + <div><button className="butn">Kolkata</button></div> |
| 93 | + <div><button className="butn">London</button></div> |
| 94 | + <div><button className="butn">Paris</button></div> |
64 | 95 | </div>
|
65 | 96 | </div>
|
66 | 97 | </div>
|
67 | 98 | </div>
|
68 |
| - |
69 |
| - |
70 |
| - |
71 |
| - |
72 |
| - |
73 |
| - |
74 |
| - |
75 |
| - |
76 |
| - ); |
| 99 | +); |
77 | 100 | }
|
78 | 101 |
|
79 | 102 | export default App;
|
0 commit comments