@@ -3,47 +3,76 @@ import React, { useEffect, useState } from 'react';
3
3
import { Booking } from '../types/appwrite.type' ;
4
4
import { databases } from '../lib/appwrite.config' ;
5
5
import * as sdk from 'node-appwrite' ;
6
+ import EditBookingForm from './EditBookingForm' ;
6
7
7
8
const CustomerViewBookings = ( ) => {
8
9
const [ bookings , setBookings ] = useState < Booking [ ] > ( [ ] ) ;
10
+ const [ selectedBooking , setSelectedBooking ] = useState < Booking | null > ( null ) ;
11
+ const [ isEditing , setIsEditing ] = useState ( false ) ;
9
12
const userId = JSON . parse ( localStorage . getItem ( 'appwriteSession' ) || '{}' ) . userId ;
10
13
14
+ const fetchBookings = async ( ) => {
15
+ try {
16
+ const response = await databases . listDocuments (
17
+ process . env . DATABASE_ID ! ,
18
+ process . env . BOOKING_COLLECTION_ID ! ,
19
+ [ sdk . Query . equal ( 'consumerId' , userId ) ]
20
+ ) ;
21
+
22
+ const bookingsData : Booking [ ] = response . documents . map ( ( doc : sdk . Models . Document ) => ( {
23
+ $id : doc . $id ,
24
+ $permissions : doc . $permissions ,
25
+ bookingId : doc . $id ,
26
+ $collectionId : doc . $collectionId ,
27
+ $databaseId : doc . $databaseId ,
28
+ $createdAt : doc . $createdAt ,
29
+ $updatedAt : doc . $updatedAt ,
30
+ consumerId : doc . consumerId ,
31
+ providerId : doc . providerId ,
32
+ serviceId : doc . serviceId ,
33
+ date : new Date ( doc . date ) ,
34
+ status : doc . status ,
35
+ address : doc . address ,
36
+ city : doc . city ,
37
+ state : doc . state ,
38
+ zipcode : doc . zipcode ,
39
+ } ) ) ;
40
+
41
+ setBookings ( bookingsData ) ;
42
+ } catch ( error ) {
43
+ console . error ( 'Error fetching bookings:' , error ) ;
44
+ }
45
+ } ;
46
+
11
47
useEffect ( ( ) => {
12
- const fetchBookings = async ( ) => {
48
+ fetchBookings ( ) ;
49
+ } , [ userId ] ) ;
50
+
51
+ const handleEditClick = ( booking : Booking ) => {
52
+ setSelectedBooking ( booking ) ;
53
+ setIsEditing ( true ) ;
54
+ } ;
55
+
56
+ const handleDelete = async ( bookingId : string ) => {
57
+ if ( confirm ( 'Are you sure you want to delete this booking?' ) ) {
13
58
try {
14
- const response = await databases . listDocuments (
59
+ await databases . deleteDocument (
15
60
process . env . DATABASE_ID ! ,
16
61
process . env . BOOKING_COLLECTION_ID ! ,
17
- [ sdk . Query . equal ( 'consumerId' , userId ) ]
62
+ bookingId
18
63
) ;
19
-
20
- const bookingsData : Booking [ ] = response . documents . map ( ( doc : sdk . Models . Document ) => ( {
21
- $id : doc . $id ,
22
- $permissions : doc . $permissions ,
23
- bookingId : doc . $id ,
24
- $collectionId : doc . $collectionId ,
25
- $databaseId : doc . $databaseId ,
26
- $createdAt : doc . $createdAt ,
27
- $updatedAt : doc . $updatedAt ,
28
- consumerId : doc . consumerId ,
29
- providerId : doc . providerId ,
30
- serviceId : doc . serviceId ,
31
- date : new Date ( doc . date ) ,
32
- status : doc . status ,
33
- address : doc . address ,
34
- city : doc . city ,
35
- state : doc . state ,
36
- zipcode : doc . zipcode ,
37
- } ) ) ;
38
-
39
- setBookings ( bookingsData ) ;
64
+ setBookings ( ( prevBookings ) => prevBookings . filter ( ( booking ) => booking . bookingId !== bookingId ) ) ;
40
65
} catch ( error ) {
41
- console . error ( 'Error fetching bookings :' , error ) ;
66
+ console . error ( 'Error deleting booking :' , error ) ;
42
67
}
43
- } ;
68
+ }
69
+ } ;
44
70
45
- fetchBookings ( ) ;
46
- } , [ userId ] ) ;
71
+ const handleSave = async ( ) => {
72
+ setIsEditing ( false ) ;
73
+ setSelectedBooking ( null ) ;
74
+ await fetchBookings ( ) ; // Fetch the bookings again to get the updated list
75
+ } ;
47
76
48
77
return (
49
78
< div >
@@ -55,11 +84,28 @@ const CustomerViewBookings = () => {
55
84
< p > < strong > Date:</ strong > { new Date ( booking . date ) . toLocaleDateString ( ) } </ p >
56
85
< p > < strong > Status:</ strong > { booking . status } </ p >
57
86
< p > < strong > Address:</ strong > { booking . address } , { booking . city } , { booking . state } , { booking . zipcode } </ p >
87
+ < div className = "flex space-x-4" >
88
+ < button
89
+ onClick = { ( ) => handleEditClick ( booking ) }
90
+ className = "bg-yellow-500 text-white py-2 px-4 rounded"
91
+ >
92
+ Edit
93
+ </ button >
94
+ < button
95
+ onClick = { ( ) => handleDelete ( booking . bookingId ) }
96
+ className = "bg-red-500 text-white py-2 px-4 rounded"
97
+ >
98
+ Delete
99
+ </ button >
100
+ </ div >
58
101
</ div >
59
102
) )
60
103
) : (
61
104
< p > No bookings found.</ p >
62
105
) }
106
+ { isEditing && selectedBooking && (
107
+ < EditBookingForm booking = { selectedBooking } onClose = { ( ) => setIsEditing ( false ) } onSave = { handleSave } />
108
+ ) }
63
109
</ div >
64
110
) ;
65
111
} ;
0 commit comments