Skip to content

Commit

Permalink
It's not done but I want to do something else and I can't figure it out
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 30, 2024
1 parent 7400cea commit 0a64fe4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
15 changes: 15 additions & 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 Expand Up @@ -63,3 +64,17 @@ 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;
}
}
65 changes: 37 additions & 28 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import { useState, useEffect, useRef } from "react";
import { Button } from "./ui/button";
import { useRouter } from "next/navigation";
import { getUserData } from "@/app/actions";
import type { AdviceAPIType } from "@/types";
import { getAdvice, getUserData } from "@/app/actions";
import {
useQuery,
QueryClientProvider,
QueryClient,
} from "@tanstack/react-query";

export async function getPaperlessDocument(
const queryClient = new QueryClient();

async function getPaperlessDocument(
documentId: number,
): Promise<string | null> {
const userData = await getUserData();
Expand Down Expand Up @@ -35,32 +41,15 @@ export async function getPaperlessDocument(
}
}

export default function DocumentViewer(props: { id: number }) {
const router = useRouter();
function SkeletonLoader() {
const { data: advice, isLoading } = useQuery({
queryKey: ["advice"],
queryFn: getAdvice,
});

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

useEffect(() => {
const fetchAdvice = async () => {
try {
const response = await fetch("https://api.adviceslip.com/advice");
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = (await response.json()) as AdviceAPIType;
setAdvice(data.slip.advice);
} catch (error) {
console.error("Failed to fetch advice:", error);
}
};

void fetchAdvice();
}, []);

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">
Expand All @@ -72,7 +61,11 @@ export default function DocumentViewer(props: { id: number }) {
{/* Text Overlay */}
<div className="z-10 flex items-center justify-center">
<div className="text-center text-black">
{advice ? advice : "Loading advice..."}
{isLoading
? "Loading advice..."
: advice === null
? "Unable to fetch advice"
: advice}
</div>
</div>
</div>
Expand All @@ -91,6 +84,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 @@ -159,3 +160,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>
);
}

0 comments on commit 0a64fe4

Please sign in to comment.