From 5845517fd123f22bb9071514a7197dda089eca72 Mon Sep 17 00:00:00 2001 From: Aamir Azad <82281117+aamirazad@users.noreply.github.com> Date: Sat, 6 Jul 2024 13:17:28 -0400 Subject: [PATCH] feat: show a fun fact while pdf is loading (#53) ### TL;DR Integrated a new feature in the Document Viewer that fetches and displays random advice from Advice Slip API. ### What changed? 1. Modified `document-viewer.tsx` to include a new state for storing advice fetched from the Advice Slip API. 2. Added `AdviceAPIType` to the `types` directory. ### Why? I like websites that feel fun and developer made, not cooperate. Also, reading and thinking about advice makes the long loading much less painful. --- src/app/actions.ts | 1 + src/components/document-viewer.tsx | 64 +++++++++++++++++++++++++----- src/types/index.ts | 7 ++++ 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/src/app/actions.ts b/src/app/actions.ts index e1b17e7..3908076 100644 --- a/src/app/actions.ts +++ b/src/app/actions.ts @@ -5,6 +5,7 @@ import type { UsersTableType } from "@/server/db/schema"; import { users } from "@/server/db/schema"; import type { PaperlessDocumentsType } from "@/types"; import { auth } from "@clerk/nextjs/server"; +import type { AdviceAPIType } from "@/types"; export async function setUserProperty( propertyName: K, diff --git a/src/components/document-viewer.tsx b/src/components/document-viewer.tsx index 96b9d3e..17de816 100644 --- a/src/components/document-viewer.tsx +++ b/src/components/document-viewer.tsx @@ -3,9 +3,18 @@ import { useState, useEffect, useRef } from "react"; import { Button } from "./ui/button"; import { useRouter } from "next/navigation"; -import { getUserData } from "@/app/actions"; +import { getAdvice, getUserData } from "@/app/actions"; +import { + useQuery, + QueryClientProvider, + QueryClient, + useQueryClient, +} from "@tanstack/react-query"; +import { AdviceAPIType } from "@/types"; -export async function getPaperlessDocument( +const queryClient = new QueryClient(); + +async function getPaperlessDocument( documentId: number, ): Promise { const userData = await getUserData(); @@ -34,22 +43,39 @@ export async function getPaperlessDocument( } } -export default function DocumentViewer(props: { id: number }) { - const router = useRouter(); +function SkeletonLoader() { + const { data: advice, isLoading } = useQuery({ + queryKey: ["advice"], + queryFn: async () => { + const response = await fetch("https://api.adviceslip.com/advice"); + return (await response.json()) as AdviceAPIType; + }, + }); - const [pdfUrl, setPdfUrl] = useState(null); - const [loading, setLoading] = useState(true); - const fetchDataCalledRef = useRef(false); + console.log(advice?.slip); - const SkeletonLoader = () => ( + return (
-
+
{/* PDF Skeleton */} -
+
+ {/* Pulsing Background */} +
+ {/* Text Overlay */} +
+
+ {isLoading + ? "Loading advice..." + : advice?.slip.advice === null + ? "Unable to fetch advice" + : advice?.slip.advice} +
+
+
{/* Button Skeleton */} -
+
@@ -63,6 +89,14 @@ export default function DocumentViewer(props: { id: number }) {
); +} + +function DocumentViewer(props: { id: number }) { + const router = useRouter(); + + const [pdfUrl, setPdfUrl] = useState(null); + const [loading, setLoading] = useState(true); + const fetchDataCalledRef = useRef(false); useEffect(() => { if (!fetchDataCalledRef.current) { @@ -131,3 +165,11 @@ export default function DocumentViewer(props: { id: number }) {
); } + +export default function Page(props: { id: number }) { + return ( + + + + ); +} diff --git a/src/types/index.ts b/src/types/index.ts index 6966b0f..ed86760 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -67,3 +67,10 @@ export type PaperlessDocumentsType = { }; }[]; }; + +export type AdviceAPIType = { + slip: { + slip_id: number; + advice: string; + }; +};