From 66f0368183659665c07b869f094b806fef5f66b2 Mon Sep 17 00:00:00 2001 From: Thomas Camlong <49837342+ajnart@users.noreply.github.com> Date: Tue, 31 Oct 2023 00:31:59 +0100 Subject: [PATCH] =?UTF-8?q?Revert=20"=E2=9C=A8=20Fetch=20the=20weather=20u?= =?UTF-8?q?sing=20a=20local=20request=20instead=20of=20a=20server-side=20o?= =?UTF-8?q?ne"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/api/routers/weather.ts | 14 ++++++++++++++ src/widgets/weather/WeatherTile.tsx | 20 ++------------------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/server/api/routers/weather.ts b/src/server/api/routers/weather.ts index e2a64fb22..ac83728b3 100644 --- a/src/server/api/routers/weather.ts +++ b/src/server/api/routers/weather.ts @@ -36,6 +36,20 @@ export const weatherRouter = createTRPCRouter({ }) ) .query(async ({ input }) => fetchCity(input.query)), + at: publicProcedure + .input( + z.object({ + longitude: z.number(), + latitude: z.number(), + }) + ) + .output(weatherSchema) + .query(async ({ input }) => { + const res = await fetch( + `https://api.open-meteo.com/v1/forecast?latitude=${input.latitude}&longitude=${input.longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min¤t_weather=true&timezone=Europe%2FLondon` + ); + return res.json(); + }), }); export type City = z.infer; diff --git a/src/widgets/weather/WeatherTile.tsx b/src/widgets/weather/WeatherTile.tsx index 7a51d5479..1ace62447 100644 --- a/src/widgets/weather/WeatherTile.tsx +++ b/src/widgets/weather/WeatherTile.tsx @@ -6,8 +6,8 @@ import { IconCloudRain, IconMapPin, } from '@tabler/icons-react'; -import { useQuery } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; +import { api } from '~/utils/api'; import { defineWidget } from '../helper'; import { IWidget } from '../widgets'; @@ -50,23 +50,7 @@ interface WeatherTileProps { } function WeatherTile({ widget }: WeatherTileProps) { - const { - data: weather, - isLoading, - isError, - } = useQuery( - ['weather', widget.properties.location], - async () => - await fetch( - `https://api.open-meteo.com/v1/forecast?latitude=${widget.properties.location.latitude}&longitude=${widget.properties.location.longitude}&daily=weathercode,temperature_2m_max,temperature_2m_min¤t_weather=true&timezone=Europe%2FLondon`, - { - // 15 minutes of cache - cache: 'force-cache', - headers: { 'Cache-Control': 'max-age=900' }, - } - ).then((res) => res.json()), - {} - ); + const { data: weather, isLoading, isError } = api.weather.at.useQuery(widget.properties.location); const { width, ref } = useElementSize(); const { t } = useTranslation('modules/weather');