Skip to content

Commit 5feb6ee

Browse files
committed
feat: borrow detail no actions for user
1 parent d015b4f commit 5feb6ee

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

app/(protected)/borrows/[id]/page.tsx

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Link from 'next/link'
2-
import { Verify } from '@/lib/firebase/firebase'
2+
import { IsLoggedIn, Verify } from '@/lib/firebase/firebase'
33
import { getBorrow, getListBorrows } from '@/lib/api/borrow'
44
import { Badge } from '@/components/ui/badge'
55
import {
@@ -31,6 +31,7 @@ import { differenceInDays } from 'date-fns'
3131
import { Borrow } from '@/lib/types/borrow'
3232
import { Button } from '@/components/ui/button'
3333
import { BtnUndoReturn } from '@/components/borrows/BtnUndoReturn'
34+
import { redirect, RedirectType } from 'next/navigation'
3435

3536
export default async function BorrowDetailsPage({
3637
params,
@@ -39,7 +40,9 @@ export default async function BorrowDetailsPage({
3940
}) {
4041
const { id } = await params
4142

42-
await Verify({ from: `/borrows/${id}` })
43+
const from = `/borrows/${id}`
44+
45+
const headers = await Verify({ from })
4346

4447
const [borrowRes] = await Promise.all([getBorrow({ id })])
4548

@@ -49,11 +52,10 @@ export default async function BorrowDetailsPage({
4952

5053
const isDue = isBorrowDue(borrowRes.data)
5154

52-
// const progressPercent = getBorrowProgressPercent(borrowRes.data)
53-
54-
const headers = await Verify({
55-
from: '/borrows',
56-
})
55+
const claim = await IsLoggedIn()
56+
if (!claim || !claim.librarease) {
57+
redirect(`/login?from=${encodeURIComponent(from)}`, RedirectType.replace)
58+
}
5759

5860
let prevBorrows: Borrow[] = []
5961
const [prevBorrowsRes] = await Promise.all([
@@ -75,6 +77,12 @@ export default async function BorrowDetailsPage({
7577
prevBorrows = prevBorrowsRes.data
7678
}
7779

80+
const isSuperAdmin = claim.librarease.role === 'SUPERADMIN'
81+
const isAdmin = claim.librarease.role === 'ADMIN'
82+
const isStaff = claim.librarease.admin_libs
83+
.concat(claim.librarease.staff_libs)
84+
.includes(borrowRes.data.subscription.membership.library_id)
85+
7886
return (
7987
<>
8088
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
@@ -306,27 +314,32 @@ export default async function BorrowDetailsPage({
306314
</Card>
307315
)}
308316

309-
<div className="bottom-0 sticky py-2 flex flex-col md:flex-row gap-2 md:gap-4 basis-1/2">
310-
{borrowRes.data.returning ? (
311-
<BtnUndoReturn
312-
variant="outline"
313-
className="w-full"
314-
borrow={borrowRes.data}
315-
/>
316-
) : (
317-
<BtnReturnBook
318-
variant="outline"
319-
className="w-full"
320-
borrow={borrowRes.data}
321-
/>
322-
)}
323-
<Button asChild>
324-
<Link href={`/borrows/${borrowRes.data.id}/edit`} className="w-full">
325-
<Pen />
326-
Edit
327-
</Link>
328-
</Button>
329-
</div>
317+
{(isSuperAdmin || isAdmin || isStaff) && (
318+
<div className="bottom-0 sticky py-2 flex flex-col md:flex-row gap-2 md:gap-4 basis-1/2">
319+
{borrowRes.data.returning ? (
320+
<BtnUndoReturn
321+
variant="outline"
322+
className="w-full"
323+
borrow={borrowRes.data}
324+
/>
325+
) : (
326+
<BtnReturnBook
327+
variant="outline"
328+
className="w-full"
329+
borrow={borrowRes.data}
330+
/>
331+
)}
332+
<Button asChild>
333+
<Link
334+
href={`/borrows/${borrowRes.data.id}/edit`}
335+
className="w-full"
336+
>
337+
<Pen />
338+
Edit
339+
</Link>
340+
</Button>
341+
</div>
342+
)}
330343
</>
331344
)
332345
}

lib/firebase/firebase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function IsLoggedIn(): Promise<
5555
| (DecodedIdToken & {
5656
librarease: {
5757
id: string
58-
role: string
58+
role: 'USER' | 'ADMIN' | 'SUPERADMIN'
5959
admin_libs: string[]
6060
staff_libs: string[]
6161
}
@@ -76,7 +76,7 @@ export async function IsLoggedIn(): Promise<
7676
return {
7777
librarease: {
7878
id: '',
79-
role: '',
79+
role: 'USER',
8080
admin_libs: [],
8181
staff_libs: [],
8282
},

0 commit comments

Comments
 (0)