Skip to content

Commit

Permalink
Mostly working advice
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jul 6, 2024
1 parent 0a64fe4 commit a4acb5a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
14 changes: 0 additions & 14 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
13 changes: 9 additions & 4 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
useQuery,
QueryClientProvider,
QueryClient,
useQueryClient,
} from "@tanstack/react-query";
import { AdviceAPIType } from "@/types";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -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 (
<div className="flex h-4/5 w-full justify-center">
Expand All @@ -63,9 +68,9 @@ function SkeletonLoader() {
<div className="text-center text-black">
{isLoading
? "Loading advice..."
: advice === null
: advice?.slip.advice === null
? "Unable to fetch advice"
: advice}
: advice?.slip.advice}
</div>
</div>
</div>
Expand Down

0 comments on commit a4acb5a

Please sign in to comment.