11import { SearchOutlined } from '@ant-design/icons'
2- import { Button , Form , Input , notification , Space , Table } from 'antd'
2+ import { Button , Input , notification , Space , Table } from 'antd'
33import axios from 'axios'
4- import React , { useRef , useState } from 'react'
4+ import React , { useEffect , useRef , useState } from 'react'
55import Highlighter from 'react-highlight-words'
66import { ApiEndpoint } from '../constant/api'
7- import { BookingHistory , DashboardResponse } from '../types/index '
7+ import { BookingHistory , DashboardResponse } from '../types'
88import Navigation from '../components/navigation'
9+ import { StorageKey } from '../constant/storage'
10+ import { useRouter } from 'next/router'
11+ import dayjs from 'dayjs'
912
10- type dashboardform = {
11- booking_id : string
12- registration : string
13- start_time : string
14- end_time : string
15- cost : string
16- }
17-
18- type dashboardProps = {
19- dashboard : BookingHistory [ ]
20- }
21-
22- export default function dashboardPage ( { dashboard } : dashboardProps ) {
13+ export default function ViewHistory ( ) {
14+ const [ dashboard , setDashboard ] = useState < BookingHistory [ ] > ( [ ] )
2315 const columns = [
2416 {
2517 title : 'Booking ID' ,
@@ -53,12 +45,47 @@ export default function dashboardPage({ dashboard }: dashboardProps) {
5345 searchedColumn : '' ,
5446 } )
5547 const searchInputEl = useRef ( null )
56- const [ form ] = Form . useForm < dashboardform > ( )
48+ const router = useRouter ( )
49+
50+ useEffect ( ( ) => {
51+ const fetchApi = async ( ) => {
52+ try {
53+ const email = localStorage . getItem ( StorageKey . EMAIL )
54+ if ( ! email ) {
55+ notification . error ( {
56+ message : 'Invalid credential' ,
57+ description : 'This user has no email' ,
58+ placement : 'bottomRight' ,
59+ } )
60+ await router . replace ( '/login' )
61+ return
62+ }
63+ const res = await axios . get < DashboardResponse > ( `${ ApiEndpoint . booking } ?Customer_id=${ email } ` )
64+ console . log ( 'Booking data of current customer' , res . data )
65+
66+ const tableData = res . data . Items . map ( ( item ) => {
67+ return {
68+ ...item ,
69+ Start_time : dayjs ( item . Start_time ) . format ( 'YYYY-MM-DD HH:mm' ) ,
70+ End_time : dayjs ( item . End_time ) . format ( 'YYYY-MM-DD HH:mm' ) ,
71+ }
72+ } )
73+
74+ setDashboard ( tableData )
75+ } catch ( { message } ) {
76+ console . error ( 'Error getting data' , message )
77+ notification . error ( {
78+ message,
79+ placement : 'bottomRight' ,
80+ } )
81+ }
82+ }
83+ fetchApi ( )
84+ } , [ ] )
5785
5886 function getColumnSearchProps ( dataIndex ) {
5987 return {
60- filterDropdown : ( { setSelectedKeys, selectedKeys, confirm, clearFilters } ) =>
61- (
88+ filterDropdown : ( { setSelectedKeys, selectedKeys, confirm, clearFilters } ) => (
6289 < div style = { { padding : 8 } } >
6390 < Input
6491 ref = { searchInputEl }
@@ -119,17 +146,15 @@ export default function dashboardPage({ dashboard }: dashboardProps) {
119146 }
120147 }
121148
122- function handleSearch ( selectedKeys , confirm , dataIndex )
123- {
149+ function handleSearch ( selectedKeys , confirm , dataIndex ) {
124150 confirm ( )
125151 setSearchState ( {
126152 searchText : selectedKeys [ 0 ] ,
127153 searchedColumn : dataIndex ,
128154 } )
129155 }
130156
131- function handleReset ( clearFilters )
132- {
157+ function handleReset ( clearFilters ) {
133158 clearFilters ( )
134159 setSearchState ( { ...searchState , searchText : '' } )
135160 }
@@ -139,20 +164,8 @@ export default function dashboardPage({ dashboard }: dashboardProps) {
139164 < Navigation />
140165 < div style = { { marginTop : '5%' } } />
141166 < div className = "container" >
142- < Table columns = { columns } dataSource = { dashboard } rowKey = "id " />
167+ < Table columns = { columns } dataSource = { dashboard } rowKey = "Booking_id " />
143168 </ div >
144169 </ >
145170 )
146171}
147-
148- export async function getServerSideProps ( context ) {
149- const res = await axios . get < DashboardResponse > ( ApiEndpoint . booking )
150-
151- const dashboardRes = res . data
152-
153- return {
154- props : {
155- dashboard : dashboardRes . Items ,
156- } ,
157- }
158- }
0 commit comments