@@ -17,12 +17,17 @@ import type { Metadata } from 'next'
1717import Link from 'next/link'
1818import { LibrarySelector } from '@/components/dashboard/LibrarySelector'
1919import { DateRangeSelector } from '@/components/dashboard/DateRangeSelector'
20+ import { IsLoggedIn } from '@/lib/firebase/firebase'
21+ import { redirect } from 'next/navigation'
22+ import { format , subMonths , parse } from 'date-fns'
23+ import { getListLibraries } from '@/lib/api/library'
24+ import { DateRange } from 'react-day-picker'
2025
2126export const metadata : Metadata = {
2227 title : `Dashboard · ${ SITE_NAME } ` ,
2328}
2429
25- export default async function Dashboard ( {
30+ export default async function DashboardPage ( {
2631 searchParams,
2732} : {
2833 searchParams : Promise < {
@@ -33,25 +38,75 @@ export default async function Dashboard({
3338 skip : number
3439 } >
3540} ) {
41+ const claims = await IsLoggedIn ( )
42+ if ( ! claims ) redirect ( '/login?from=/dashboard' )
43+
3644 const {
37- library_id = 'a32af9bd-74b0-4ef2-8974-4570e2bfb4bb' ,
38- from = '2024-01-05T17:00:47.680Z' ,
39- to = new Date ( ) . toJSON ( ) ,
40- limit = 5 ,
41- skip,
42- } = await searchParams
43- const res = await getAnalysis ( {
44- limit,
45- skip,
45+ library_id,
4646 from,
4747 to,
48- library_id,
49- } )
50- console . log ( res )
48+ // limit = 5,
49+ // skip,
50+ } = await searchParams
51+
52+ if ( claims . librarease . role != 'USER' && ( ! to || ! from || ! library_id ) ) {
53+ const now = new Date ( )
54+ const to = format ( now , 'dd-MM-yyyy' )
55+ const from = format ( subMonths ( now , 4 ) , 'dd-MM-yyyy' )
56+ const libID = claims . librarease . admin_libs . concat (
57+ claims . librarease . staff_libs
58+ )
59+ const sp = new URLSearchParams ( )
60+ sp . set ( 'from' , from )
61+ sp . set ( 'to' , to )
62+ sp . set ( 'library_id' , libID [ 0 ] )
63+
64+ redirect ( '?' + sp . toString ( ) )
65+ }
66+
67+ const [ res , libsRes ] = await Promise . all ( [
68+ getAnalysis ( {
69+ skip : 0 ,
70+ limit : 5 ,
71+ from : parse ( from , 'dd-MM-yyyy' , new Date ( ) ) . toJSON ( ) ,
72+ to : parse ( to , 'dd-MM-yyyy' , new Date ( ) ) . toJSON ( ) ,
73+ library_id,
74+ } ) ,
75+ getListLibraries ( { limit : 5 } ) ,
76+ ] )
5177
5278 if ( 'error' in res ) {
53- console . log ( res )
54- return < div > { JSON . stringify ( res . message ) } </ div >
79+ return < div > { res . error } </ div >
80+ }
81+ if ( 'error' in libsRes ) {
82+ return < div > { libsRes . error } </ div >
83+ }
84+
85+ async function onLibraryChange ( libraryID : string ) {
86+ 'use server'
87+
88+ const p = await searchParams
89+ // @ts -expect-error: skip and imit are not used now
90+ const sp = new URLSearchParams ( p )
91+ sp . set ( 'library_id' , libraryID )
92+
93+ redirect ( '/dashboard?' + sp . toString ( ) )
94+ }
95+
96+ const fromDate = parse ( from , 'dd-MM-yyyy' , new Date ( ) )
97+ const toDate = parse ( to , 'dd-MM-yyyy' , new Date ( ) )
98+
99+ async function onDateRangeChange ( range ?: DateRange ) {
100+ 'use server'
101+
102+ const p = await searchParams
103+ // @ts -expect-error: skip and imit are not used now
104+ const sp = new URLSearchParams ( p )
105+ if ( ! range ) return
106+ if ( range . from ) sp . set ( 'from' , format ( range . from , 'dd-MM-yyyy' ) )
107+ if ( range . to ) sp . set ( 'to' , format ( range . to , 'dd-MM-yyyy' ) )
108+
109+ redirect ( '/dashboard?' + sp . toString ( ) )
55110 }
56111
57112 return (
@@ -73,8 +128,15 @@ export default async function Dashboard({
73128 </ Breadcrumb >
74129
75130 < div className = "grid my-4 grid-cols-1 gap-4 md:grid-cols-2" >
76- < LibrarySelector />
77- < DateRangeSelector />
131+ < LibrarySelector
132+ libs = { libsRes . data }
133+ lib = { library_id }
134+ onChangeAction = { onLibraryChange }
135+ />
136+ < DateRangeSelector
137+ range = { { from : fromDate , to : toDate } }
138+ onChangeAction = { onDateRangeChange }
139+ />
78140 < MostBorrowedBookChart data = { res . data . book } />
79141 < TopMembershipChart data = { res . data . membership } />
80142 < MontlyBorrowChart data = { res . data . borrowing } />
0 commit comments