Skip to content

Commit 154ceeb

Browse files
added download button on pdf files
1 parent 20602b2 commit 154ceeb

File tree

5 files changed

+65
-22
lines changed

5 files changed

+65
-22
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@react-pdf-viewer/core": "3.12.0",
2222
"@react-pdf-viewer/default-layout": "^3.12.0",
2323
"@react-pdf-viewer/full-screen": "^3.12.0",
24+
"@react-pdf-viewer/get-file": "^3.12.0",
2425
"@react-pdf-viewer/zoom": "^3.12.0",
2526
"@t3-oss/env-nextjs": "^0.10.1",
2627
"@types/mongoose": "^5.11.97",

pnpm-lock.yaml

Lines changed: 12 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/fe_api/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"use server";
2+
import { ErrorResponse, PaperResponse } from "@/interface";
3+
import axios, { AxiosResponse } from "axios";
4+
5+
const fetchPaper = async (id: string): Promise<PaperResponse> => {
6+
try {
7+
const response: AxiosResponse<PaperResponse> = await axios.get(
8+
`/api/paper-by-id/${id}`,
9+
);
10+
return response.data;
11+
} catch (err: unknown) {
12+
if (axios.isAxiosError(err)) {
13+
const errorResponse = err.response as AxiosResponse<ErrorResponse>;
14+
15+
// if (errorResponse?.status === 400 || errorResponse?.status === 404) {
16+
// router.push("/");
17+
// } else {
18+
// setError(errorResponse?.data?.message ?? "Failed to fetch paper");
19+
// }
20+
// } else {
21+
// setError("An unknown error occurred");
22+
}
23+
}
24+
};

src/app/paper/[id]/page.tsx

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useState, useEffect } from "react";
33
import axios, { type AxiosResponse } from "axios";
44
import Footer from "@/components/Footer";
55
import Navbar from "@/components/Navbar";
6-
import { Eye, Maximize } from "lucide-react";
6+
import { Download, Eye, Maximize } from "lucide-react";
77
import { useRouter } from "next/navigation";
88
import { Worker } from "@react-pdf-viewer/core";
99
import { Viewer } from "@react-pdf-viewer/core";
@@ -15,23 +15,19 @@ import {
1515
type RenderZoomInProps,
1616
type RenderCurrentScaleProps,
1717
} from "@react-pdf-viewer/zoom";
18+
import { getFilePlugin } from "@react-pdf-viewer/get-file";
19+
1820
import {
1921
fullScreenPlugin,
2022
type RenderEnterFullScreenProps,
2123
} from "@react-pdf-viewer/full-screen";
24+
2225
import "@react-pdf-viewer/full-screen/lib/styles/index.css";
2326
import "@react-pdf-viewer/core/lib/styles/index.css";
2427
import "@react-pdf-viewer/zoom/lib/styles/index.css";
2528
import Loader from "@/components/ui/loader";
2629
import Link from "next/link";
27-
28-
interface PaperResponse {
29-
finalUrl: string;
30-
subject: string;
31-
year: string;
32-
slot: string;
33-
exam: string;
34-
}
30+
import { PaperResponse } from "@/interface";
3531

3632
interface ErrorResponse {
3733
message: string;
@@ -45,7 +41,7 @@ export default function PaperPage({ params }: Params) {
4541
const [paper, setPaper] = useState<PaperResponse | null>(null);
4642
const [error, setError] = useState<string | null>(null);
4743
const router = useRouter();
48-
44+
const getFilePluginInstance = getFilePlugin();
4945
const zoomPluginInstance = zoomPlugin();
5046
const { CurrentScale, ZoomIn, ZoomOut } = zoomPluginInstance;
5147
const fullScreenPluginInstance = fullScreenPlugin();
@@ -116,7 +112,14 @@ export default function PaperPage({ params }: Params) {
116112
)}
117113
</ZoomIn>
118114
</div>
119-
<div className="hidden gap-x-4 md:flex">
115+
<div className="hidden gap-x-4 md:flex md:items-center">
116+
<getFilePluginInstance.Download>
117+
{(props) => (
118+
<button className="" onClick={props.onClick}>
119+
<Download />
120+
</button>
121+
)}
122+
</getFilePluginInstance.Download>
120123
<EnterFullScreen>
121124
{(props: RenderEnterFullScreenProps) => (
122125
<button onClick={() => props.onClick()}>
@@ -126,15 +129,19 @@ export default function PaperPage({ params }: Params) {
126129
</EnterFullScreen>
127130
</div>
128131
<Link className="flex md:hidden" href={paper.finalUrl}>
129-
<Eye size={20} />
132+
<Download />
130133
</Link>
131134
</div>
132135

133136
<Worker workerUrl="https://unpkg.com/[email protected]/build/pdf.worker.min.js">
134137
<div className="border-1 w-[95%] overflow-x-hidden md:w-[80%]">
135138
<Viewer
136139
fileUrl={paper.finalUrl}
137-
plugins={[zoomPluginInstance, fullScreenPluginInstance]}
140+
plugins={[
141+
zoomPluginInstance,
142+
getFilePluginInstance,
143+
fullScreenPluginInstance,
144+
]}
138145
/>
139146
</div>
140147
</Worker>

src/interface.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export interface PostRequestBody {
5555
tags: string;
5656
}
5757

58+
export interface PaperResponse {
59+
finalUrl: string;
60+
subject: string;
61+
year: string;
62+
slot: string;
63+
exam: string;
64+
}
65+
5866
export interface IPaper{
5967
public_id_cloudinary: string;
6068
finalUrl: string;

0 commit comments

Comments
 (0)