Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Add weather widgets to homepage #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions components/WeatherWidget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, { useState, useEffect } from 'react';

const WeatherWidget = ({ location }) => {
const [weatherData, setWeatherData] = useState(null);

useEffect(() => {
const fetchWeatherData = async () => {
// Fetch weather data for the specified location using an API
// Set the weather data in the state variable
};

fetchWeatherData();
}, [location]);

const formatWeatherData = (data) => {
// Format the weather data
// Return the formatted data
};

return (
<div>
{/* Render the formatted weather data */}
</div>
);
};

export default WeatherWidget;
16 changes: 16 additions & 0 deletions components/WeatherWidgetContainer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';
import WeatherWidget from './WeatherWidget';

const WeatherWidgetContainer = () => {
const locations = ['London', 'Edinburgh', 'Alicante'];

return (
<div>
{locations.map((location) => (
<WeatherWidget key={location} location={location} />
))}
</div>
);
};

export default WeatherWidgetContainer;
19 changes: 19 additions & 0 deletions components/utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from 'axios';

const BASE_URL = 'https://api.weatherapi.com/v1';

export const fetchWeatherData = async (location: string) => {
try {
const response = await axios.get(`${BASE_URL}/weather`, {
params: {
key: 'YOUR_API_KEY',
q: location,
},
});

return response.data;
} catch (error) {
console.error('Error fetching weather data:', error);
throw error;
}
};
13 changes: 13 additions & 0 deletions components/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const formatWeatherData = (weatherData: any) => {
const temperature = weatherData.current.temp_c;
const humidity = weatherData.current.humidity;
const windSpeed = weatherData.current.wind_kph;

const formattedWeatherData = {
temperature: `${temperature}°C`,
humidity: `${humidity}%`,
windSpeed: `${windSpeed} km/h`,
};

return formattedWeatherData;
};