Skip to content

Commit

Permalink
trying to fix fileid
Browse files Browse the repository at this point in the history
  • Loading branch information
talhathmd committed Oct 6, 2024
1 parent caabaa6 commit 73524ac
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/app/dashboard/[fileid]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import PdfSummary from "@/components/PdfSummary";
import PdfRenderer from "@/components/PdfRenderer";
import { fetchReport } from "@/lib/actions/report.actions"; // Import your fetchReport function
import { fetchReport, fetchAllReportIds } from "@/lib/actions/report.actions"; // Import your fetch functions

interface PageProps {
params: {
fileid: string;
};
}

export async function generateStaticParams() {
// Fetch all the report IDs that you want to pre-generate static pages for
const reportIds = await fetchAllReportIds();

// Return an array of params, each containing a report ID
return reportIds.map((id) => ({
fileid: id,
}));
}

const Page = async ({ params }: PageProps) => {
const { fileid } = params;

// Fetch the report using the fileid
const report = await fetchReport(fileid);

// Check if the report is found, and if not, you can handle it (e.g., show an error message)
if (!report) {
return (
<div className="flex-1 justify-between flex flex-col h-[calc(100vh-3.5rem)]">
Expand All @@ -32,16 +41,13 @@ const Page = async ({ params }: PageProps) => {
return (
<div className="flex flex-1 h-[calc(100vh-3.5rem)]">
<div className="mx-auto w-full max-w-8xl grow lg:flex xl:px-2">
{/* Left sidebar & main wrapper */}
<div className="flex-1 xl:flex">
<div className="px-4 py-6 sm:px-6 lg:pl-8 xl:flex-1 xl:pl-6 flex flex-col">
{/* Main area */}
<div className="flex-1 h-full overflow-hidden">
<PdfRenderer url={report.fileUrl} /> {/* Pass the fileUrl to PdfRenderer */}
<PdfRenderer url={report.fileUrl} />
</div>
</div>
</div>
{/* Scrollable container for PdfSummary */}
<div className="shrink-0 flex-[0.75] h-full border-t border-gray-200 lg:w-96 lg:border-l lg:border-t-0">
<div className="h-full flex flex-col">
<PdfSummary pdfUrl={report.fileUrl} />
Expand All @@ -50,7 +56,6 @@ const Page = async ({ params }: PageProps) => {
</div>
</div>
);

};

export default Page;
22 changes: 10 additions & 12 deletions src/lib/actions/report.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function createReport({
fileUrl,
fileId,
});
const reports = await Report.find({}, 'fileId');

await User.findOneAndUpdate(
{ email: author }, // Find the user by email
Expand All @@ -42,23 +43,20 @@ export async function createReport({
}
}

export async function fetchReport(fileId: string) {
export async function fetchAllReportIds() {
try {
// Connect to the database
await connectToDB();

// Find the report by fileId instead of _id
const report = await Report.findOne({ fileId: fileId });
// Fetch all reports and return their fileId fields
const reports = await Report.find({}, 'fileId'); // Only return the fileId field

// If no report is found, throw an error
if (!report) {
throw new Error("Report not found");
}

// Return the report data
return report;
// Map through the results to extract fileIds
return reports.map((report) => report.fileId);
} catch (error: any) {
console.error(`Error fetching report: ${error}`);
throw new Error("Error fetching report");
console.error(`Error fetching report IDs: ${error.message}`);
throw new Error("Error fetching report IDs");
}
}


0 comments on commit 73524ac

Please sign in to comment.