1
1
import { Colors } from "@/constants/Colors" ;
2
2
import { StatusBar } from "expo-status-bar" ;
3
- import { useContext , useState } from "react" ;
4
- import { ScrollView , TouchableOpacity , View , Image } from "react-native" ;
3
+ import { useContext , useState } from "react" ;
4
+ import { ScrollView , TouchableOpacity , View , Image } from "react-native" ;
5
5
import { Text } from "react-native" ;
6
6
import { ThemeContext } from "@/ctx/ThemeContext" ;
7
7
import Typography from "@/constants/Typography" ;
@@ -12,76 +12,118 @@ import PaymentChooseContainer from "@/components/UI/PaymentChooseContainer/Index
12
12
import { router } from "expo-router" ;
13
13
import React from "react" ;
14
14
import { useLocalSearchParams } from "expo-router" ;
15
- import { PayWithFlutterwave } from ' flutterwave-react-native'
15
+ import { PayWithFlutterwave } from " flutterwave-react-native" ;
16
16
import { supabase } from "@/lib/supabase" ;
17
17
import { useModal } from "@/ctx/ModalContext" ;
18
18
export default function SelectPayment ( ) {
19
19
const { theme, changeTheme } = useContext ( ThemeContext ) ;
20
20
const [ selected , setSelected ] = useState ( false ) ;
21
- const [ loggedEmail , setLoggedEmail ] = useState < string > ( "" )
22
- const { doctor_id, hour, date, packageTitle, packagePrice, problem, user_id, patient_id, duration } = useLocalSearchParams ( )
21
+ const [ loggedEmail , setLoggedEmail ] = useState < string > ( "" ) ;
22
+ const {
23
+ doctor_id,
24
+ hour,
25
+ date,
26
+ packageTitle,
27
+ packagePrice,
28
+ problem,
29
+ user_id,
30
+ patient_id,
31
+ duration,
32
+ } = useLocalSearchParams ( ) ;
33
+
34
+ const modal = useModal ( ) ;
35
+ const flutterKey = process . env . EXPO_PUBLIC_FLUTTERWAVE_KEY ?? "" ;
36
+ console . log ( "this is packageprice from slect Payment:" , packagePrice ) ;
23
37
24
- const modal = useModal ( )
25
- const flutterKey = process . env . EXPO_PUBLIC_FLUTTERWAVE_KEY ?? ""
26
-
27
38
interface RedirectParams {
28
- status : "successful" | "cancelled" ;
29
- transaction_id ?: string ;
30
- tx_ref : string ;
39
+ status : "successful" | "cancelled" ;
40
+ transaction_id ?: string ;
41
+ tx_ref : string ;
31
42
}
32
- let num :number = 1 ;
43
+ let num : number = 1 ;
33
44
if ( duration === "30 minutes" ) {
34
- num = 1
45
+ num = 1 ;
35
46
} else {
36
- num = 2
47
+ num = 2 ;
37
48
}
38
49
let price : number = 0 ;
39
50
if ( packagePrice === "Rwf20" ) {
40
- price = 20
51
+ price = 20 ;
41
52
} else if ( packagePrice === "Rwf40" ) {
42
- price = 40
53
+ price = 40 ;
43
54
} else if ( packagePrice === "Rwf60" ) {
44
- price = 60
55
+ price = 60 ;
45
56
}
46
- const total :number = price * num
47
-
57
+ const total : number = price * num ;
58
+
48
59
useEffect ( ( ) => {
49
60
const fetchUser = async ( ) => {
50
-
51
- const { data : { user } , error } = await supabase . auth . getUser ( )
61
+ const {
62
+ data : { user } ,
63
+ error,
64
+ } = await supabase . auth . getUser ( ) ;
52
65
if ( error ) {
53
- console . error ( "error fetching user" )
66
+ console . error ( "error fetching user" ) ;
54
67
} else {
55
- setLoggedEmail ( user ?. email || "logged Email" )
68
+ setLoggedEmail ( user ?. email || "logged Email" ) ;
56
69
}
57
- }
58
- fetchUser ( )
59
-
60
- } , [ loggedEmail ] )
70
+ } ;
71
+ fetchUser ( ) ;
72
+ } , [ loggedEmail ] ) ;
61
73
async function bookAppointment ( ) {
62
74
try {
63
- const { error } = await supabase
64
- . from ( 'appointment' )
65
- . insert ( {
75
+ const { error } = await supabase . from ( "appointment" ) . insert ( {
66
76
doctor_id : doctor_id ,
67
- time : hour , date : date ,
77
+ time : hour ,
78
+ date : date ,
68
79
package : packageTitle ,
69
80
price : packagePrice ,
70
81
illness_descr : problem ,
71
82
user_id : patient_id ,
72
- duration : duration
83
+ duration : duration ,
73
84
} ) ;
74
85
} catch ( error ) {
75
- console . log ( "Error while inserting data in booking " , error )
76
- }
77
-
86
+ console . log ( "Error while inserting data in booking " , error ) ;
87
+ }
78
88
}
89
+ const addNotification = async ( doctorName : string ) => {
90
+ try {
91
+ const { error } = await supabase . from ( "notifications" ) . insert ( {
92
+ title : "Appointment Booked" ,
93
+ description : `You have successfully booked an appointment with Dr. ${ doctorName } ` ,
94
+ patient_id : patient_id ,
95
+ type : "appointment_booked" ,
96
+ doctor_id : doctor_id ,
97
+ viewed : false ,
98
+ } ) ;
99
+ console . log ( "Notification will be pushed" ) ;
100
+ if ( error ) {
101
+ console . log ( "Error while inserting notification " , error ) ;
102
+ }
103
+ } catch ( error ) {
104
+ console . log ( "Error while inserting notification " , error ) ;
105
+ }
106
+ } ;
107
+ const fetchDoctorName = async ( doctorId : string ) => {
108
+ const { data, error } = await supabase
109
+ . from ( "doctors" )
110
+ . select ( "first_name" )
111
+ . eq ( "id" , doctorId )
112
+ . single ( ) ;
113
+
114
+ if ( error ) {
115
+ console . log ( "Error fetching doctor's name: " , error ) ;
116
+ return "" ;
117
+ }
118
+
119
+ return data . first_name ;
120
+ } ;
79
121
function successBooking ( ) {
80
- router . push ( "ActionMenu" ) ; ;
122
+ router . push ( "ActionMenu" ) ;
81
123
modal . hide ( ) ;
82
124
}
83
125
const showSuccefulModal = ( ) => {
84
- modal . show ( {
126
+ modal . show ( {
85
127
children : (
86
128
< View
87
129
style = { {
@@ -135,13 +177,7 @@ const total:number=price*num
135
177
justifyContent : "center" ,
136
178
} }
137
179
> </ View >
138
- < Button
139
- title = "View Appointment"
140
- onPress = { ( ) => {
141
- successBooking ( )
142
- router . push ( "(app)/Appointments" )
143
- } }
144
- />
180
+ < Button title = "View Appointment" onPress = { successBooking } />
145
181
< TouchableOpacity
146
182
onPress = { ( ) => {
147
183
router . push ( "ActionMenu" ) ;
@@ -173,29 +209,31 @@ const total:number=price*num
173
209
</ View >
174
210
) ,
175
211
} ) ;
176
- }
177
- const handleOnRedirect = ( data : RedirectParams ) => {
178
-
179
- console . log ( "redire data:" , data )
212
+ } ;
213
+ const handleOnRedirect = async ( data : RedirectParams ) => {
180
214
if ( data . status === "successful" ) {
181
- bookAppointment ( )
182
- showSuccefulModal ( )
215
+ bookAppointment ( ) ;
216
+ if ( typeof doctor_id === "string" ) {
217
+ const doctorName = await fetchDoctorName ( doctor_id ) ;
218
+ await addNotification ( doctorName ) ;
219
+ }
220
+ showSuccefulModal ( ) ;
183
221
} else {
184
- alert ( "Payment Failed or cancelled ,please try again" )
222
+ alert ( "Payment Failed or cancelled ,please try again" ) ;
185
223
}
186
- }
224
+ } ;
187
225
const generateRef = ( length : number ) : string => {
188
- const characters = flutterKey ;
189
- const charactersArray = characters . split ( '' ) ;
190
- let result = '' ;
226
+ const characters = flutterKey ;
227
+ const charactersArray = characters . split ( "" ) ;
228
+ let result = "" ;
191
229
192
- for ( let i = 0 ; i < length ; i ++ ) {
193
- const randomIndex = Math . floor ( Math . random ( ) * charactersArray . length ) ;
194
- result += charactersArray [ randomIndex ] ;
195
- }
230
+ for ( let i = 0 ; i < length ; i ++ ) {
231
+ const randomIndex = Math . floor ( Math . random ( ) * charactersArray . length ) ;
232
+ result += charactersArray [ randomIndex ] ;
233
+ }
196
234
197
- return result ;
198
- } ;
235
+ return result ;
236
+ } ;
199
237
200
238
return (
201
239
< >
@@ -222,25 +260,31 @@ const total:number=price*num
222
260
>
223
261
Comfirm the payment by click the button below.
224
262
</ Text >
225
- < View style = { { width :"100%" , height :"70%" , display :"flex" , flexDirection :"row" , alignItems :"center" , justifyContent :"center" } } >
226
- < PayWithFlutterwave
227
- onRedirect = { handleOnRedirect }
228
- options = { {
229
- tx_ref : generateRef ( 11 ) ,
230
- authorization : 'FLWPUBK_TEST-3c390392d62e44fc5788cb0859823f05-X' ,
231
- customer : {
232
- email : loggedEmail
233
- } ,
234
- amount : total ,
235
- currency : 'RWF' ,
236
- payment_options : 'card'
237
- } }
238
- />
239
-
263
+ < View
264
+ style = { {
265
+ width : "100%" ,
266
+ height : "70%" ,
267
+ display : "flex" ,
268
+ flexDirection : "row" ,
269
+ alignItems : "center" ,
270
+ justifyContent : "center" ,
271
+ } }
272
+ >
273
+ < PayWithFlutterwave
274
+ onRedirect = { handleOnRedirect }
275
+ options = { {
276
+ tx_ref : generateRef ( 11 ) ,
277
+ authorization : "FLWPUBK_TEST-3c390392d62e44fc5788cb0859823f05-X" ,
278
+ customer : {
279
+ email : loggedEmail ,
280
+ } ,
281
+ amount : total ,
282
+ currency : "RWF" ,
283
+ payment_options : "card" ,
284
+ } }
285
+ />
240
286
</ View >
241
-
242
-
243
287
</ ScrollView >
244
288
</ >
245
289
) ;
246
- }
290
+ }
0 commit comments