This repository contains a weather dashboard application built using React and TypeScript. The app integrates with the Open-Meteo API to provide current weather data and a 5-day forecast for any city. Additionally, the app detects the user's current location and displays the weather of that location by default until the user searches for a different city.
- Search Functionality: Users can search for any city to display its current weather and forecast.
- Current Weather: Displays the current weather conditions (temperature, wind, etc.).
- 5-Day Forecast: A detailed 5-day weather forecast displayed as a list or cards.
- Geolocation Integration: Automatically detects the user’s current location and shows its weather data.
- Geocoding Integration: Uses the Open-Meteo Geocoding API to convert a city name to latitude and longitude.
- Error Handling: Provides user-friendly error messages for invalid city names, geolocation issues, and network errors.
- Loading States: Includes loading indicators during data fetching to enhance user experience.
- Persist Coordinates in URL: The latitude and longitude of the selected location are persisted in the URL, allowing for shareable links.
- Vite: Fast build tool and development server for modern web projects.
- React: JavaScript library for building user interfaces.
- TypeScript: Typed superset of JavaScript for better code quality and developer experience.
- shadcn/ui: UI components library for consistent design and styling.
- TailwindCSS: Utility-first CSS framework for styling.
- React Query: Manages server-side state, caching, and synchronizing weather data with API calls efficiently.
- pnpm: Fast, disk space-efficient package manager for managing dependencies.
-
Current Weather API
URL:https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=trueParameters:latitude: Latitude of the location.longitude: Longitude of the location.current_weather: Set totrueto return current weather data.
Example for London:
https://api.open-meteo.com/v1/forecast?latitude=51.51&longitude=-0.13¤t_weather=true -
5-Day Forecast API
URL:https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}&daily=temperature_2m_max,temperature_2m_min&timezone=autoParameters:latitude: Latitude of the location.longitude: Longitude of the location.daily: Specify the weather data you want to return.timezone: Set toautoto adjust for the local time zone.
Example for London:
https://api.open-meteo.com/v1/forecast?latitude=51.51&longitude=-0.13&daily=temperature_2m_max,temperature_2m_min&timezone=auto
-
Clone the Repository:
git clone https://github.com/KaroMourad/weather-app.git cd weather-app -
Install Dependencies: Ensure
pnpmis installed globally on your machine. Then, run:pnpm install
-
Start the Application: To start the development server, use:
pnpm dev
The app will be available at
http://localhost:5173/. -
API Key:
No API key is required for the Open-Meteo API.
State management is handled using React Query and React’s built-in hooks (useState, useEffect). The current weather data and 5-day forecast are fetched asynchronously and stored in the state. The app also synchronizes the coordinates with the search query parameters in the URL for easier navigation and bookmarking.
The app automatically detects the user's current location on page load using the browser's geolocation feature. If geolocation is allowed, the app shows the weather of the user’s current location. The coordinates (latitude and longitude) are then persisted in the URL search parameters. When a user searches for another city, the app updates the URL with the new location’s coordinates. This enables easy sharing of the current weather and forecast with a shareable URL.
The app leverages:
- React Query to handle data fetching, caching, and synchronizing the state efficiently.
- Memoization to avoid unnecessary re-fetching and re-rendering of weather data.
- Loading Indicators to enhance user experience during API calls.
User-friendly error messages are shown in case of invalid city names, geolocation permission issues, or network problems.
- Offline Mode: Use local storage or service workers to cache weather data for offline access.
- Unit Testing: Add Jest or other testing frameworks for unit testing components.
- Advanced UI Enhancements: Further improve card design and animations for the forecast.
- Live Demo: https://weather-app-gilt-five-82.vercel.app
This project is licensed under the MIT License.