Skip to content

Commit

Permalink
feat: show a fun fact while pdf is loading (#53)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
aamirazad authored Jul 6, 2024
1 parent 65d4162 commit 5845517
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K extends keyof UsersTableType>(
propertyName: K,
Expand Down
64 changes: 53 additions & 11 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | null> {
const userData = await getUserData();
Expand Down Expand Up @@ -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<string | null>(null);
const [loading, setLoading] = useState(true);
const fetchDataCalledRef = useRef(false);
console.log(advice?.slip);

const SkeletonLoader = () => (
return (
<div className="flex h-4/5 w-full justify-center">
<div className="flex h-full min-w-0 justify-center md:w-1/2">
<div className="flex h-full w-full flex-col rounded-xl bg-slate-600/50">
<div className="m-4 flex flex-grow animate-pulse flex-col justify-center gap-8 md:m-8 md:flex-row md:gap-16">
<div className="m-4 flex h-full flex-grow flex-col justify-center gap-8 md:m-8 md:flex-row md:gap-16">
{/* PDF Skeleton */}
<div className="h-full flex-shrink flex-grow rounded-lg bg-gray-400"></div>
<div className="relative flex h-full flex-shrink flex-grow items-center justify-center rounded-lg">
{/* Pulsing Background */}
<div className="absolute inset-0 animate-pulse rounded-lg bg-gray-400"></div>
{/* Text Overlay */}
<div className="z-10 flex items-center justify-center">
<div className="text-center text-black">
{isLoading
? "Loading advice..."
: advice?.slip.advice === null
? "Unable to fetch advice"
: advice?.slip.advice}
</div>
</div>
</div>
{/* Button Skeleton */}
<div className="flex flex-shrink-0 flex-col gap-8">
<div className="flex flex-shrink-0 animate-pulse flex-col gap-8">
<div className="h-10 w-24 rounded-md bg-gray-400"></div>
<div className="h-10 w-24 rounded-md bg-gray-400"></div>
<div className="h-10 w-24 rounded-md bg-gray-400"></div>
Expand All @@ -63,6 +89,14 @@ export default function DocumentViewer(props: { id: number }) {
</div>
</div>
);
}

function DocumentViewer(props: { id: number }) {
const router = useRouter();

const [pdfUrl, setPdfUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const fetchDataCalledRef = useRef(false);

useEffect(() => {
if (!fetchDataCalledRef.current) {
Expand Down Expand Up @@ -131,3 +165,11 @@ export default function DocumentViewer(props: { id: number }) {
</div>
);
}

export default function Page(props: { id: number }) {
return (
<QueryClientProvider client={queryClient}>
<DocumentViewer id={props.id} />
</QueryClientProvider>
);
}
7 changes: 7 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ export type PaperlessDocumentsType = {
};
}[];
};

export type AdviceAPIType = {
slip: {
slip_id: number;
advice: string;
};
};

0 comments on commit 5845517

Please sign in to comment.