Skip to content

Commit

Permalink
Move back to client and it works!
Browse files Browse the repository at this point in the history
  • Loading branch information
aamirazad committed Jun 28, 2024
1 parent 1078b50 commit c2c8b77
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
29 changes: 0 additions & 29 deletions src/app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,3 @@ export async function getPaperlessDocuments(query: string) {

return data;
}

export async function getPaperlessDocument(
documentId: number,
): Promise<string | null> {
const userData = await getUserData();
if (!userData) {
console.error("Error getting user data");
return null;
}
try {
const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`;
const response = await fetch(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
});
if (response.ok) {
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} else {
console.error("Failed to fetch PDF");
return null;
}
} catch (error) {
console.error("Error fetching PDF:", error);
return null;
}
}
33 changes: 31 additions & 2 deletions src/components/document-viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
"use client";

import { useState, useEffect } from "react";
import { getPaperlessDocument } from "@/app/actions";
import LoadingSpinner from "@/components/loading-spinner";
import { Button } from "./ui/button";
import { useRouter } from "next/navigation";
import { getUserData } from "@/app/actions";

export async function getPaperlessDocument(
documentId: number,
): Promise<string | null> {
const userData = await getUserData();
if (!userData) {
console.error("Error getting user data");
return null;
}
try {
const url = `${userData.paperlessURL}/api/documents/${documentId}/download/`;
const response = await fetch(url, {
headers: {
Authorization: `Token ${userData.paperlessToken}`,
},
});
if (response.ok) {
const blob = await response.blob();
const objectUrl = URL.createObjectURL(blob);
return objectUrl;
} else {
console.error("Failed to fetch PDF");
return null;
}
} catch (error) {
console.error("Error fetching PDF:", error);
return null;
}
}

export default function DocumentViewer(props: { id: number }) {
const router = useRouter();
Expand All @@ -19,10 +48,10 @@ export default function DocumentViewer(props: { id: number }) {
const objectUrl = await getPaperlessDocument(props.id);
if (objectUrl) {
setPdfUrl(objectUrl);
setLoading(false);
// Cleanup function to revoke URL when component unmounts or pdfUrl changes
return () => URL.revokeObjectURL(objectUrl);
} else {
setLoading(false);
}
};

Expand Down

0 comments on commit c2c8b77

Please sign in to comment.