From 71f77fcea197fa498e263be9e521c988c9064fb9 Mon Sep 17 00:00:00 2001 From: aJesusCode Date: Tue, 17 Oct 2023 16:03:12 +0200 Subject: [PATCH] feat: added avarage weekday pintxo condition --- src/app/(surfspots)/favorite/page.tsx | 17 -------- src/app/(surfspots)/layout.tsx | 2 +- src/app/components/SpotDetails.tsx | 12 +++--- .../atom/WeekdayPintxoCondition.tsx | 32 ++++++++++++++ src/app/utils/surfUtils.ts | 43 +++++++++++++++++++ 5 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 src/app/components/atom/WeekdayPintxoCondition.tsx diff --git a/src/app/(surfspots)/favorite/page.tsx b/src/app/(surfspots)/favorite/page.tsx index 7c00e67..9d7a937 100644 --- a/src/app/(surfspots)/favorite/page.tsx +++ b/src/app/(surfspots)/favorite/page.tsx @@ -47,23 +47,6 @@ async function getFavoriteSpots(): Promise { export default async function FavoriteSpots() { const favoriteSpotsData = await getFavoriteSpots(); - // let favoriteSpotsData: PintxoConditions[] = []; - - // if (favoriteSpots.length) { - // favoriteSpotsData = await Promise.all( - // favoriteSpots.map(async (spot) => { - // try { - // return await fetchSpotSurfData(spot); - // } catch (error) { - // console.error( - // `Error fetching data for spot ${spot.name}:`, - // error - // ); - // return null; // or return a default value or error object - // } - // }) - // ); - // } return (
diff --git a/src/app/(surfspots)/layout.tsx b/src/app/(surfspots)/layout.tsx index dfd8b7a..7850628 100644 --- a/src/app/(surfspots)/layout.tsx +++ b/src/app/(surfspots)/layout.tsx @@ -46,7 +46,7 @@ export default async function FavoriteSpotsLayout({ `} > - All Pintxoss + All Pintxos
diff --git a/src/app/components/SpotDetails.tsx b/src/app/components/SpotDetails.tsx index f583d5a..3d91ec3 100644 --- a/src/app/components/SpotDetails.tsx +++ b/src/app/components/SpotDetails.tsx @@ -14,6 +14,7 @@ import { PintxoRange } from "./PintxoRange"; import { getCurrentPintxoConditions } from "../utils/surfUtils"; import { getPintxoColor } from "../utils/uiUtils"; import PintxoConditionNameCard from "./atom/PintxoConditionNameCard"; +import WeekdayPintxoCondition from "./atom/WeekdayPintxoCondition"; type PintxoConditions = Database["public"]["Tables"]["spot_conditions"]["Row"]; type PintxoName = | "Empty Plate" @@ -65,27 +66,28 @@ export default async function SpotDetails({ -
-
+
+
{getCurrentWaveHeightForSpot(spot) || null} {"m"}
-
+
{getCurrentPeriodForSpot(spot) || null}{" "} s.
-
+
- + {getCurrentWind(spot) || null}
+
); } diff --git a/src/app/components/atom/WeekdayPintxoCondition.tsx b/src/app/components/atom/WeekdayPintxoCondition.tsx new file mode 100644 index 0000000..4f704f9 --- /dev/null +++ b/src/app/components/atom/WeekdayPintxoCondition.tsx @@ -0,0 +1,32 @@ +import { Pintxo } from "@/app/constants/types"; +import { getWeekdayPintxoCondition } from "@/app/utils/surfUtils"; +import { getPintxoColor } from "@/app/utils/uiUtils"; +import React from "react"; + +interface WekdayPintxoProps { + pintxo: Pintxo[] | null; +} +function WeekdayPintxoCondition({ pintxo }: WekdayPintxoProps) { + const data = getWeekdayPintxoCondition(pintxo!); + return ( +
+ {Object.keys(data).map((key, index) => ( +
+ + {key} + +
+
+ ))} +
+ ); +} + +export default WeekdayPintxoCondition; diff --git a/src/app/utils/surfUtils.ts b/src/app/utils/surfUtils.ts index 80c608c..91fcc87 100644 --- a/src/app/utils/surfUtils.ts +++ b/src/app/utils/surfUtils.ts @@ -11,9 +11,13 @@ import { TideType, HourlyWeatherData, ForecastDataResponse, + Pintxo, } from "../constants/types"; import { Database } from "../lib/database.types"; type PintxoConditions = Database["public"]["Tables"]["spot_conditions"]["Row"]; +type WeekdayPintxoCondition = { + [weekday: string]: string; +}; //constants const directionToDegrees: { [key: string]: number } = { @@ -250,3 +254,42 @@ export function getCurrentPintxoConditions( return currentPintxoCondition; } + +export function getWeekdayPintxoCondition( + data: Pintxo[] +): WeekdayPintxoCondition { + const weekdays = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; + const weekdayConditions: { + [weekday: string]: { [condition: string]: number }; + } = {}; + console.log("data", data); + + for (const item of data) { + const date = DateTime.fromISO(item.time, { zone: "UTC" }) + .setZone("Europe/Paris") + .startOf("hour"); + const weekday = weekdays[date.weekday - 1]; + + weekdayConditions[weekday] = weekdayConditions[weekday] || {}; + weekdayConditions[weekday][item.condition] = + (weekdayConditions[weekday][item.condition] || 0) + 1; + } + + const averageConditions: WeekdayPintxoCondition = {}; + + for (const weekday in weekdayConditions) { + let maxCount = 0; + let mostFrequentCondition = ""; + + for (const condition in weekdayConditions[weekday]) { + if (weekdayConditions[weekday][condition] > maxCount) { + maxCount = weekdayConditions[weekday][condition]; + mostFrequentCondition = condition; + } + } + + averageConditions[weekday] = mostFrequentCondition; + } + + return averageConditions; +}