Skip to content

Commit

Permalink
feat: view document support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed Jun 29, 2024
1 parent 7957e60 commit 980aae7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions apps/web/public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"search": "Search document...",
"empty": "No documents found",
"hint": "Drag and drop a document to upload",
"view": "View document",
"delete": "Delete document",
"download": "Download document",
"columns": {
Expand Down
1 change: 1 addition & 0 deletions apps/web/public/locales/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"search": "Cari dokumen...",
"empty": "Tidak ada dokumen",
"hint": "Seret dan lepas dokumen di sini untuk mengunggah",
"view": "Lihat dokumen",
"delete": "Hapus dokumen",
"download": "Unduh dokumen",
"columns": {
Expand Down
14 changes: 14 additions & 0 deletions apps/web/src/app/(app)/@admin/documents/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { Document } from "@/server/api/routers/documents/documents.schema";
import React, { useMemo } from "react";
import Link from "next/link";
import { useSearchParams } from "next/navigation";
import { api } from "@/trpc/react";
import {
Expand All @@ -25,6 +26,7 @@ import {
} from "@nextui-org/react";
import moment from "moment";
import { useTranslations } from "next-intl";
import { IoOpenOutline } from "react-icons/io5";
import { MdOutlineDownloadForOffline } from "react-icons/md";
import { RiDeleteBin2Line } from "react-icons/ri";

Expand Down Expand Up @@ -110,6 +112,18 @@ export function DocumentsTable() {
case "actions":
return (
<div className="flex items-center gap-2">
<Tooltip content={t("view")} color="default" className="p-2">
<Link href={`/api/documents/${file.id}`} target="_blank">
<Button
className="pointer-events-auto"
isIconOnly
variant="light"
color="default"
>
<IoOpenOutline />
</Button>
</Link>
</Tooltip>
<Tooltip content={t("download")} color="primary" className="p-2">
<Button
className="pointer-events-auto"
Expand Down
16 changes: 15 additions & 1 deletion apps/web/src/app/api/documents/[fileId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,19 @@ export async function GET(_: Request, context: { params: { fileId: string } }) {
const res = await fetch(
env.BACKEND_URL + "/files/download/" + context.params.fileId,
);
return res;

if (!res.ok) {
return await res.json();
}

return new Response(await res.arrayBuffer(), {
headers: {
"Content-Type": "application/pdf",
"Content-Disposition":
res.headers
.get("Content-Disposition")
?.replace("attachment", "inline") ??
'inline; filename="document.pdf"',
},
});
}

0 comments on commit 980aae7

Please sign in to comment.