WEATHER APP COMPONENT
WE WILL CONNECT BOTH THE INFOBOX AND SEARCH BOX USING WEATHER APP COMPONENT.
WE WILL IMPLEMENT A NEW FUNCTION INORDER TO UPDATE THE LOCATION.
FOR INFOBOX , WE WILL SEND THE WEATHERINFO OBJECT AS A PROP. DEFAULT LOCATION IS SET TO KHAMMAM.
import SearchBox from "./SearchBox";
import InfoBox from "./InfoBox";
import "./WeatherApp.css";
import { useState } from "react";
export default function WeatherApp() {
let [weatherInfo, setWeatherInfo] = useState({
city: "khammam",
feels_like: 34.48,
humidity: 26,
temp: 35.43,
tempMax: 35.43,
tempMin: 35.43,
weather: "clear sky",
});
let updateInfo = (newInfo) => {
setWeatherInfo(newInfo);
};
return (
<div className="WeatherApp">
<h1>Weather App</h1>
<SearchBox updateInfo={updateInfo}/>
<InfoBox info={weatherInfo} />
</div>
);
}
IN THE SEARCHBOX, UPDATE INFO IS SENT AS A PROP FROM THE WEATHERAPP.JSX
AND IT IS DEFINED IN THE HANDLESUBMIT FUNCTION.
FOR UNKNOWN LOCATIONS AN ERROR IS DISPLAYED BY USING TRY-CATCH BLOCK.
import "./Searchbox.css";
import TextField from "@mui/material/TextField";
import Button from "@mui/material/Button";
import { useState } from "react";
export default function SearchBox({ updateInfo }) {
let [city, setCity] = useState("");
let [error, setError] = useState(false);(FOR ERRORS)
const API_URL = "https://api.openweathermap.org/data/2.5/weather";
const API_KEY = "1040644aa7a47486eb74915ac8525aba";
let getWeatherInfo = async () => {
try {
let res = await fetch(
`${API_URL}?q=${city}&appid=${API_KEY}&units=metric`
);
let jsonRes = await res.json();
let result = {
city: city,
temp: jsonRes.main.temp,
tempMin: jsonRes.main.temp_min,
tempMax: jsonRes.main.temp_max,
humidity: jsonRes.main.humidity,
feels_like: jsonRes.main.feels_like,
weather: jsonRes.weather[0].description,
};
console.log(result);
return result;
} catch (err) {
throw err;
}
};
let handleChange = (event) => {
setCity(event.target.value);
};
let handleSubmit = async (event) => {
try {
event.preventDefault();
console.log(city);
setCity("");
let newInfo = await getWeatherInfo();
console.log(newInfo);
updateInfo(newInfo);
} catch (err) {
setError(true);
}
};
return (
<div className="SearchBox">
<h2>Find weather</h2>
<form onSubmit={handleSubmit}>
<TextField
id="city"
label="City name"
variant="outlined"
value={city}
required
onChange={handleChange}
/>
<br />
<br />
<Button variant="contained" type="submit">
search
</Button>
{error ? <p style={{color:"red"}}>Sorry! Location cannot be found</p> : ""}
</form>
</div>
);
}
IN THE INFOBOX, THE UPDATED WEATHER INFO IS SENT AND IT IS DISPLAYED.
import Card from "@mui/material/Card";
import CardContent from "@mui/material/CardContent";
import CardMedia from "@mui/material/CardMedia";
import Typography from "@mui/material/Typography";
import "./InfoBox.css";
export default function InfoBox({ info }) {
console.log(info);
return (
<div className="InfoBox">
<h1>Weather info - {info.weather}</h1>
<div className="CardComponent">
<Card sx={{ maxWidth: 345 }}>
<CardMedia
sx={{ height: 140 }}
image="https://images.unsplash.com/photo-1603883055407-968560f7522e?q=80&w=1901&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D"
title="green iguana"
/>
<CardContent>
<Typography gutterBottom variant="h5" component="div">
<p> {info.city}</p>
</Typography>
<Typography
variant="body2"
sx={{ color: "text.secondary" }}
component={"span"}
>
<p> Humidity:{info.humidity}</p>
<p>Temperature: {info.temp}°C</p>
<p> Min Temp: {info.tempMin}°C</p>
<p> Max Temp: {info.tempMax}°C</p>
<p>
Weather can be described as <i>{info.weather}</i> but it feels
like {info.feels_like} °C
</p>
</Typography>
</CardContent>
</Card>
</div>
</div>
);
}
ERROR HANDLING





Comments
Post a Comment