From 4c72edda3d3e0d19549d4a6b3e23b78bfd4c7b01 Mon Sep 17 00:00:00 2001 From: Aamir Azad Date: Sat, 6 Jul 2024 13:03:18 -0400 Subject: [PATCH] Mostly working advice --- src/app/actions.ts | 14 -------------- src/components/document-viewer.tsx | 13 +++++++++---- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/app/actions.ts b/src/app/actions.ts index 0441bd4..3908076 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -64,17 +64,3 @@ export async function getPaperlessDocuments(query: string) { return data; } - -export async function getAdvice() { - try { - const response = await fetch("https://api.adviceslip.com/advice"); - if (!response.ok) { - return null; - } - const data = (await response.json()) as { slip: { advice: string } }; - return data.slip.advice; - } catch (error) { - console.error("Failed to fetch advice:", error); - return null; - } -} diff --git a/src/components/document-viewer.tsx b/src/components/document-viewer.tsx index 21f71dd..17de816 100644 --- a/src/components/document-viewer.tsx +++ b/src/components/document-viewer.tsx @@ -8,7 +8,9 @@ import { useQuery, QueryClientProvider, QueryClient, + useQueryClient, } from "@tanstack/react-query"; +import { AdviceAPIType } from "@/types"; const queryClient = new QueryClient(); @@ -44,10 +46,13 @@ async function getPaperlessDocument( function SkeletonLoader() { const { data: advice, isLoading } = useQuery({ queryKey: ["advice"], - queryFn: getAdvice, + queryFn: async () => { + const response = await fetch("https://api.adviceslip.com/advice"); + return (await response.json()) as AdviceAPIType; + }, }); - console.log(advice); + console.log(advice?.slip); return (
@@ -63,9 +68,9 @@ function SkeletonLoader() {
{isLoading ? "Loading advice..." - : advice === null + : advice?.slip.advice === null ? "Unable to fetch advice" - : advice} + : advice?.slip.advice}