-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactcodeifneeded.jsx
125 lines (113 loc) · 4.18 KB
/
reactcodeifneeded.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React, { useState } from 'react';
const SearchIcon = () => (
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" strokeWidth={1.5} stroke="currentColor" className="w-6 h-6">
<path strokeLinecap="round" strokeLinejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" />
</svg>
);
const WeatherApp = () => {
const [city, setCity] = useState('');
const [weather, setWeather] = useState(null);
const [error, setError] = useState(false);
const handleSearch = async () => {
if (city.trim() === '') {
alert('Please enter a city name.');
return;
}
try {
const api = 'c6bc976394f13845dc637ae1be0ff583';
const url = `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${api}`;
const response = await fetch(url);
const data = await response.json();
if (data.cod === '404') {
setError(true);
setWeather(null);
} else {
setError(false);
setWeather({
temperature: Math.round(data.main.temp - 273.15),
description: data.weather[0].description,
humidity: data.main.humidity,
windSpeed: Math.round(data.wind.speed),
weatherIcon: getWeatherIcon(data.weather[0].main),
});
}
} catch (e) {
setError(true);
setWeather(null);
}
};
const getWeatherIcon = (weather) => {
switch (weather) {
case 'Clouds':
return './assets/cloud.png';
case 'Rain':
return './assets/rain.png';
case 'Clear':
return './assets/clear.png';
case 'Haze':
case 'Mist':
return './assets/mist.png';
case 'Snow':
return './assets/snow.png';
case 'Thunderstorm':
return './assets/rain.png';
default:
return './assets/cloud.png';
}
};
return (
<div className="container mx-auto p-6 max-w-md">
<div className="search-box flex justify-between items-center">
<input
type="text"
placeholder="Enter a city name"
className="bg-[#e6f5fb] rounded-lg px-4 py-3 flex-grow text-black"
value={city}
onChange={(e) => setCity(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && handleSearch()}
/>
<button
className="bg-[#e6f5fb] rounded-full w-12 h-12 text-black hover:bg-[#ababab] hover:text-white transition-colors duration-300 transform-gpu"
onClick={handleSearch}
>
<SearchIcon />
</button>
</div>
{error ? (
<div className="location-not-found flex justify-center items-center flex-col mt-8">
<h1 className="text-[#6b6b6b] font-semibold text-xl mb-4">Location Not Found</h1>
<img src="./assets/404.png" alt="404 Error" className="w-3/4" />
</div>
) : weather ? (
<div className="weather-body flex justify-center items-center flex-col mt-8">
<img src={weather.weatherIcon} alt="Weather Icon" className="weather-img w-3/5" />
<div className="weather-box text-center">
<div className="temprature text-[#dadada] text-4xl font-semibold relative">
{weather.temperature}°C
</div>
<div className="description text-[#dadada] text-2xl font-semibold capitalize">
{weather.description}
</div>
</div>
<div className="weather-details w-full flex justify-between mt-8 gap-5">
<div className="humidity flex items-center">
<i className="text-[#dadada] text-3xl"></i>
<div className="text ml-3 text-[#dadada]">
<span className="font-semibold text-2xl">{weather.humidity}%</span>
<p>Humidity</p>
</div>
</div>
<div className="wind flex items-center">
<i className="text-[#dadada] text-3xl"></i>
<div className="text ml-3 text-[#dadada]">
<span className="font-semibold text-2xl">{weather.windSpeed}km/h</span>
<p>Wind Speed</p>
</div>
</div>
</div>
</div>
) : null}
</div>
);
};
export default WeatherApp;