flexbox hell

This commit is contained in:
ChrQR 2024-05-10 19:23:02 +02:00
parent 65091eecf7
commit 19d3aec4fe
6 changed files with 37 additions and 24 deletions

View File

@ -21,8 +21,8 @@ export default function Home() {
}; };
return ( return (
<main className='flex'> <main>
<div className='mx-auto'> <div className='mx-auto max-w-3xl'>
<LocationContext.Provider value={contextValue}> <LocationContext.Provider value={contextValue}>
<LocationSearch /> <LocationSearch />
<WeatherNow /> <WeatherNow />

View File

@ -3,12 +3,12 @@ import WeatherIcon from "./WeatherIcon"
import HourlyTemp from "./HourlyTemp" import HourlyTemp from "./HourlyTemp"
export default function HourlyCard(props: HourlyCardPropType) { export default function HourlyCard(props: HourlyCardPropType) {
const weatherCode = props.weather.weather_code[props.timeIndex] const weatherCode = props.weatherCode
const temperature = props.weather.apparent_temperature[props.timeIndex] const temperature = props.temperature
return ( return (
<> <div className="m-2 text-center shadow-md border-slate-200 border-2 rounded">
<WeatherIcon weatherCode={weatherCode}/> <WeatherIcon weatherCode={weatherCode}/>
<HourlyTemp temperature={temperature} /> <HourlyTemp temperature={temperature} />
</> </div>
) )
}; };

View File

@ -2,6 +2,6 @@
export default function HourlyTemp(props: {temperature: number;}) { export default function HourlyTemp(props: {temperature: number;}) {
const feels = props.temperature; const feels = props.temperature;
return ( return (
<p>{feels}°C</p> <p className="my-2">{feels}°C</p>
) )
} }

View File

@ -1,15 +1,15 @@
import Image from "next/image"; import Image from "next/image";
import cloudAndLightening from '../public/cloud-and-lightening.svg' import cloudAndLightening from '../../public/cloud-and-lightening.svg'
import cloudAndMoon from '../public/cloud-and-moon.svg' import cloudAndMoon from '../../public/cloud-and-moon.svg'
import cloudAndRain from '../public/cloud-and-rain.svg' import cloudAndRain from '../../public/cloud-and-rain.svg'
import cloudAndSnow from '../public/cloud-and-snow.svg' import cloudAndSnow from '../../public/cloud-and-snow.svg'
import cloudAndWind from '../public/cloud-and-wind.svg' import cloudAndWind from '../../public/cloud-and-wind.svg'
import cloudSnowRain from '../public/cloud-snow-rain.svg' import cloudSnowRain from '../../public/cloud-snow-rain.svg'
import cloudAndSun from '../public/cloud-and-sun.svg' import cloudAndSun from '../../public/cloud-and-sun.svg'
import cloudWindRain from '../public/cloud-wind-rain.svg' import cloudWindRain from '../../public/cloud-wind-rain.svg'
import cloud from '../public/cloud.svg' import cloud from '../../public/cloud.svg'
import moon from '../public/moon.svg' import moon from '../../public/moon.svg'
import sun from '../public/sun.svg' import sun from '../../public/sun.svg'
@ -81,8 +81,8 @@ function getCurrentWeatherIcon(weatherCode: number) {
export default function WeatherIcon(props: {weatherCode: number}) { export default function WeatherIcon(props: {weatherCode: number}) {
const weatherCode = props.weatherCode const weatherCode = props.weatherCode
return ( return (
<> <div className="p-2">
<Image className="h-1/3 mx-auto" src={getCurrentWeatherIcon(weatherCode)} alt="Weather icon"/> <Image src={getCurrentWeatherIcon(weatherCode)} alt="Weather icon"/>
</> </div>
) )
}; };

View File

@ -2,17 +2,19 @@
import { getHourlyForecast } from "@/app/actions"; import { getHourlyForecast } from "@/app/actions";
import Temperature from "./Temperature"; import Temperature from "./Temperature";
import { HourlyForecast, WeatherContextType } from "@/types/types"; import { HourlyCardPropType, HourlyForecast, WeatherContextType } from "@/types/types";
import { createContext, useContext, useEffect, useState } from "react"; import { createContext, useContext, useEffect, useState } from "react";
import { defaultHourlyForecast } from "@/app/defaultState"; import { defaultHourlyForecast } from "@/app/defaultState";
import { LocationContext } from "@/app/page"; import { LocationContext } from "@/app/page";
import WeatherHero from "./WeatherHero"; import WeatherHero from "./WeatherHero";
import HourlyCard from "./HourlyCard/HourlyCard";
export const WeatherContext = createContext<WeatherContextType>({ export const WeatherContext = createContext<WeatherContextType>({
weather: defaultHourlyForecast, weather: defaultHourlyForecast,
setWeather: () => {}, // Default function, does nothing setWeather: () => {}, // Default function, does nothing
}); });
export default function WeatherNow() { export default function WeatherNow() {
const { geoLocation } = useContext(LocationContext); const { geoLocation } = useContext(LocationContext);
const [weather, setWeather] = useState<HourlyForecast>(defaultHourlyForecast); const [weather, setWeather] = useState<HourlyForecast>(defaultHourlyForecast);
@ -20,6 +22,7 @@ export default function WeatherNow() {
weather, weather,
setWeather, setWeather,
}; };
useEffect(() => { useEffect(() => {
let mounted = true; let mounted = true;
getHourlyForecast(geoLocation).then((data) => { getHourlyForecast(geoLocation).then((data) => {
@ -39,6 +42,16 @@ export default function WeatherNow() {
<WeatherContext.Provider value={contextValue}> <WeatherContext.Provider value={contextValue}>
<WeatherHero /> <WeatherHero />
<Temperature /> <Temperature />
<div className="flex flex-wrap h-full">
{weather.hourly && weather.hourly.apparent_temperature.map((temp, index) => (
index < 12 && // Ensure we only render the first 12 hours
<HourlyCard
key={index}
temperature={temp}
weatherCode={weather.hourly.weather_code[index]}
/>
))}
</div>
</WeatherContext.Provider> </WeatherContext.Provider>
</> </>
); );

View File

@ -215,6 +215,6 @@ export interface WeatherContextType {
} }
export interface HourlyCardPropType { export interface HourlyCardPropType {
weather: HourlyWeather; temperature: number;
timeIndex: number; weatherCode: number;
} }