From 46f0906f33de1b1c3f7137a576048bd009ee797a Mon Sep 17 00:00:00 2001 From: No0ne003 Date: Fri, 17 May 2024 18:14:16 +0100 Subject: [PATCH] Weather app project (take the whole day) --- src/App.jsx | 6 +- src/components/Search.jsx | 21 ++++ src/components/ui/Loading.jsx | 7 +- src/data/projects.js | 12 +++ src/pages/Weather-app/index.jsx | 169 ++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+), 8 deletions(-) create mode 100644 src/components/Search.jsx create mode 100644 src/pages/Weather-app/index.jsx diff --git a/src/App.jsx b/src/App.jsx index 86a8b0b..a8fe8d6 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,6 +1,6 @@ import { lazy, Suspense, useState } from "react"; import { ThemeProvider } from "@/components/theme-provider"; -import { Link, Route, Routes, useNavigate } from "react-router-dom"; +import { Route, Routes } from "react-router-dom"; import { useLocation } from "react-router-dom"; // layout import Header from "@/layouts/header"; @@ -48,6 +48,7 @@ const UseWindowResizeTest = lazy( () => import("./pages/CustomHooks/use-window-resize/test"), ); const ScrollToSection = lazy(() => import("./pages/ScrollToSection")); +const WeatherApp = lazy(() => import("@/pages/Weather-app/index")); function App() { const [cursorVariant, setCursorVariant] = useState("default"); @@ -66,7 +67,7 @@ function App() {
{location.pathname !== "/React-Projects/" && } - }> + }> } /> + } /> } /> diff --git a/src/components/Search.jsx b/src/components/Search.jsx new file mode 100644 index 0000000..5427a97 --- /dev/null +++ b/src/components/Search.jsx @@ -0,0 +1,21 @@ +import { Button } from "./ui/button"; +import { Input } from "./ui/input"; + +const Search = ({ search, setSearch, handleSearch }) => { + return ( +
+ setSearch(event.target.value)} + /> + +
+ ); +}; + +export default Search; diff --git a/src/components/ui/Loading.jsx b/src/components/ui/Loading.jsx index f11cd1f..be0643c 100644 --- a/src/components/ui/Loading.jsx +++ b/src/components/ui/Loading.jsx @@ -1,10 +1,5 @@ - const Loading = () => { - return ( -
-
-
- ); + return
; }; export default Loading; diff --git a/src/data/projects.js b/src/data/projects.js index 7ca5e36..40691ed 100644 --- a/src/data/projects.js +++ b/src/data/projects.js @@ -8,6 +8,7 @@ export const projects = [ id: 2, name: "Random Color Generator", path: "color-generator", + tags: ['project'] }, { id: 3, @@ -23,6 +24,7 @@ export const projects = [ id: 5, name: "Load More Button", path: "load-more-data", + tags: ['project'] }, { id: 6, @@ -33,6 +35,7 @@ export const projects = [ id: 7, name: "QR Code Generator", path: "qr-code-generator", + tags: ['project'] }, { id: 8, @@ -53,6 +56,7 @@ export const projects = [ id: 11, name: "Github Profile Finder", path: "github-profile-finder", + tags: ['project'] }, { id: 12, @@ -63,6 +67,7 @@ export const projects = [ id: 13, name: "Tic Tac Toe", path: "tic-tac-toe", + tags: ['project'] }, { id: 14, @@ -91,5 +96,12 @@ export const projects = [ id: 18, name: 'Scroll to particular section', path: 'scroll-to-particular-section', + tags: ['project'], + }, + { + id: 19, + name: 'Weather App', + path: 'weather-app', + tags: ['project'] } ]; diff --git a/src/pages/Weather-app/index.jsx b/src/pages/Weather-app/index.jsx new file mode 100644 index 0000000..4fe6595 --- /dev/null +++ b/src/pages/Weather-app/index.jsx @@ -0,0 +1,169 @@ +import Search from "@/components/Search"; +import Loading from "@/components/ui/Loading"; +import { useEffect, useState } from "react"; +import { FaCloud } from "react-icons/fa"; +import { FaMapPin, FaWind } from "react-icons/fa6"; +import { WiHumidity } from "react-icons/wi"; + +const WeatherApp = () => { + const [search, setSearch] = useState(""); + const [loading, setLoading] = useState(false); + const [weatherData, setWeatherData] = useState(null); + + async function fetchWeatherData(param) { + setLoading(true); + try { + const response = await fetch( + `https://api.openweathermap.org/data/2.5/weather?q=${param}&appid=4910fb3e9fe1947b8ae9b22b46fc8d37`, + ); + + const data = await response.json(); + + if (data) { + setWeatherData(data); + setLoading(false); + } + } catch (e) { + setLoading(false); + console.log(e); + } + } + + function handleSearch() { + fetchWeatherData(search); + } + + function getCurrentDate() { + return new Date().toLocaleDateString("en-us", { + weekday: "long", + month: "long", + day: "numeric", + year: "numeric", + }); + } + + useEffect(() => { + fetchWeatherData("marrakech"); + }, []); + + console.log(weatherData); + + return ( +
+ + {loading ? ( + + ) : weatherData?.cod === "404" && weatherData?.message ? ( + + ) : ( +
+
+
+
+

+ + {weatherData?.name}, {weatherData?.sys?.country} +

+ + {getCurrentDate()} + +
+ +
+
+ {weatherData?.main?.temp} +
+

+ + { + + {weatherData && weatherData.weather && weatherData.weather[0] + ? weatherData.weather[0].description + : null} +

+
+
+ +
+

+ {weatherData && weatherData.weather && weatherData.weather[0] + ? weatherData.weather[0].description + : null} +

+
+ { +
+
+
+ +
+
+ +

{weatherData?.wind?.speed} km/h

+
+
+ +

{weatherData?.main?.humidity}%

+
+
+ +

{weatherData?.clouds?.all}%

+
+
+
+ )} +
+ ); +}; + +const ErrorMsg = () => { + return ( +
+

+ City Not Found +

+
+ ); +}; + +const LoadingMsg = () => { + return ( +
+ +
+ ); +}; + +export default WeatherApp;