File tree Expand file tree Collapse file tree 3 files changed +41
-20
lines changed
Expand file tree Collapse file tree 3 files changed +41
-20
lines changed Original file line number Diff line number Diff line change @@ -6,31 +6,25 @@ import Link from 'next/link'
66export default async function ProtectedLayout ( {
77 children,
88} : Readonly < { children : React . ReactNode } > ) {
9- // const claim = await IsLoggedIn()
10-
119 const headers = await Verify ( { from : '' , forceRedirect : false } )
1210
13- const res = await getMe (
14- {
15- include_unread_notifications_count : true ,
16- } ,
17- {
18- headers,
19- }
11+ const me = await getMe (
12+ { include_unread_notifications_count : true } ,
13+ { headers }
2014 )
21-
22- if ( 'error' in res ) {
23- console . log ( 'Error getting user ' , res . error )
24- return < div > Something went wrong </ div >
25- }
15+ . then ( ( res ) => ( 'error' in res ? null : res . data ) )
16+ . catch ( ( e ) => {
17+ console . warn ( 'Error fetching me: ' , e )
18+ return null
19+ } )
2620
2721 return (
2822 < div className = "container mx-auto px-4 my-4" >
2923 < nav className = "flex items-center justify-between" >
3024 < Link href = "/" className = "uppercase font-semibold tracking-wider" >
3125 Librarease
3226 </ Link >
33- { res . data && < NavUser user = { res . data } /> }
27+ { me && < NavUser user = { me } /> }
3428 </ nav >
3529
3630 { children }
Original file line number Diff line number Diff line change @@ -16,11 +16,12 @@ import {
1616} from '@/components/ui/pagination'
1717
1818import { Verify } from '@/lib/firebase/firebase'
19- import { BellOff } from 'lucide-react'
19+ import { CheckCircle } from 'lucide-react'
2020import Link from 'next/link'
2121import type { Metadata } from 'next'
2222import { SITE_NAME } from '@/lib/consts'
2323import { getListNotifications } from '@/lib/api/notification'
24+ import { readAllNotificationsAction } from '@/lib/actions/notification'
2425
2526export const metadata : Metadata = {
2627 title : `Notifications · ${ SITE_NAME } ` ,
@@ -80,9 +81,13 @@ export default async function Notifications({
8081 </ BreadcrumbItem >
8182 </ BreadcrumbList >
8283 </ Breadcrumb >
83- < Button >
84- < BellOff className = "mr-2 size-4" />
85- Mark All as Read
84+ < Button
85+ onClick = { readAllNotificationsAction }
86+ disabled = { ! res . meta . unread }
87+ variant = "outline"
88+ >
89+ < CheckCircle className = "mr-2 size-4" />
90+ Mark all as read
8691 </ Button >
8792 </ div >
8893 </ nav >
@@ -101,7 +106,6 @@ export default async function Notifications({
101106 </ li >
102107 ) ) }
103108 </ ul >
104-
105109 < Pagination >
106110 < PaginationContent >
107111 < PaginationItem >
Original file line number Diff line number Diff line change 1+ 'use server'
2+
3+ import { revalidatePath } from 'next/cache'
4+ import { readAllNotifications } from '../api/notification'
5+ import { Verify } from '../firebase/firebase'
6+
7+ export async function readAllNotificationsAction ( ) {
8+ const headers = await Verify ( { from : '/notifications' } )
9+
10+ try {
11+ const res = await readAllNotifications ( {
12+ headers,
13+ } )
14+ return res
15+ } catch ( e ) {
16+ if ( e instanceof Object && 'error' in e ) {
17+ return { error : e . error as string }
18+ }
19+ return { error : 'failed to read all notifications' }
20+ } finally {
21+ revalidatePath ( '/notifications' )
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments