11import React , { useState , useEffect } from "react" ;
22import { View , Text , TouchableOpacity , Image , TextInput , StyleSheet , Modal , ScrollView , SafeAreaView , Pressable } from "react-native" ;
3- import { router } from "expo-router" ;
3+ import { router , useGlobalSearchParams } from "expo-router" ;
44import { useFonts } from 'expo-font' ;
55import FiveStarRating from "../doctorcard/star" ;
6+ import { supabase } from "../../supabase" ;
67
78interface CustomCheckBoxProps {
89 selected : boolean ;
@@ -21,25 +22,107 @@ export default function Writereview() {
2122 const [ isModalVisible , setIsModalVisible ] = useState ( false ) ;
2223 const [ isoption , setIsoption ] = useState ( "" ) ;
2324 const [ text , setText ] = useState ( '' ) ;
25+ const [ selectedStars , setSelectedStars ] = useState ( 0 ) ;
26+ const [ patientImageUrl , setPatientImageUrl ] = useState ( "" ) ;
2427 const [ fontLoaded ] = useFonts ( {
2528 'UrbanistBold' : require ( '../../../assets/fonts/Urbanist-Bold.ttf' ) ,
2629 'UrbanistRegular' : require ( "../../../assets/fonts/Urbanist-Regular.ttf" ) ,
2730 'Urbanist-SemiBold' : require ( "../../../assets/fonts/Urbanist-SemiBold.ttf" ) ,
2831 'UrbanistMedium' : require ( "../../../assets/fonts/Urbanist-Medium.ttf" )
2932 } ) ;
3033
34+
35+ useEffect ( ( ) => {
36+ const fetchUserProfile = async ( ) => {
37+ const { data, error } = await supabase . auth . getUser ( ) ;
38+ if ( error ) {
39+ console . log ( error ) ;
40+ return ;
41+ }
42+ const userId = data . user ?. id ;
43+
44+ const { data : patientData , error : errordata } = await supabase
45+ . from ( "patient" )
46+ . select ( "profile_picture,full_name" )
47+ . eq ( "id" , userId ) ;
48+
49+ if ( errordata ) {
50+ console . log ( errordata ) ;
51+ } else {
52+ const patient = patientData ?. [ 0 ] ;
53+ if ( patient ?. profile_picture ) {
54+ const { data : publicUrlData , error : publicUrlError } = await supabase
55+ . storage
56+ . from ( "files" )
57+ . createSignedUrl ( `${ patient . profile_picture } ` , 63072000 ) ;
58+
59+ if ( publicUrlError ) {
60+ console . error ( "Error fetching signed URL:" , publicUrlError . message ) ;
61+ return ;
62+ }
63+
64+ setPatientImageUrl ( publicUrlData . signedUrl || "" ) ;
65+ }
66+ }
67+ } ;
68+ fetchUserProfile ( ) ;
69+ } , [ ] ) ;
70+
3171 const handleChangeText = ( value : string ) => {
3272 setText ( value ) ;
3373 } ;
3474
75+ const handleStarPress = ( index : number ) => {
76+ setSelectedStars ( index + 1 ) ;
77+ } ;
3578 const isSubmitEnabled = text . trim ( ) . length > 0 && isoption . length > 0 ;
3679
80+ const Options = [ "Yes" , "No" ] ;
81+ const { appointmentId, doctorImage, role, hospital, name } = useGlobalSearchParams < { appointmentId : string , doctorImage : string , role : string , hospital : string , name : string } > ( ) ;
82+
83+ const writeReview = async ( ) => {
84+ const { data, error } = await supabase . auth . getUser ( ) ;
85+ if ( error ) {
86+ console . log ( error ) ;
87+ return ;
88+ }
89+ const userId = data . user ?. id ;
90+
91+ const { data : patientData , error : errordata } = await supabase
92+ . from ( "patient" )
93+ . select ( "profile_picture,full_name" )
94+ . eq ( "id" , userId ) ;
95+
96+ if ( errordata ) {
97+ console . log ( errordata ) ;
98+ } else {
99+ const patient = patientData ?. [ 0 ] ;
100+
101+ const { data : reviewData , error : reviewDataError } = await supabase
102+ . from ( "reviews" )
103+ . insert ( {
104+ doctorId : appointmentId ,
105+ stars : selectedStars ,
106+ content : text ,
107+ image : patientImageUrl ,
108+ role : role ,
109+ hospital : hospital ,
110+ name : patient ?. full_name ,
111+ liked : "false" ,
112+ } )
113+
114+ if ( reviewDataError ) {
115+ console . log ( reviewDataError ) ;
116+ } else {
117+ console . log ( reviewData ) ;
118+ setIsModalVisible ( true )
119+ }
120+ }
121+ } ;
122+
37123 if ( ! fontLoaded ) {
38124 return null ;
39125 }
40-
41- const Options = [ "Yes" , "No" ] ;
42-
43126 return (
44127 < >
45128 < Modal
@@ -52,7 +135,7 @@ export default function Writereview() {
52135 < Image source = { require ( "../../../assets/appointmentIcon/review.png" ) } />
53136 < Text className = "text-[#246bfd] font-UrbanistBold text-[20px]" > Review successful!</ Text >
54137 < Text className = "font-UrbanistRegular text-center w-[300px] text-[16px]" > Your review has been successfully submitted. thank you very much!</ Text >
55- < TouchableOpacity onPress = { ( ) => { setIsModalVisible ( false ) } }
138+ < TouchableOpacity onPress = { ( ) => router . push ( "/(tabs)/" ) }
56139 className = 'w-[276px] bg-blue-600 rounded-[100px] h-[58px] justify-center mt-5'
57140 >
58141 < Text className = 'text-white text-center font-UrbanistBold' > OK</ Text >
@@ -62,7 +145,6 @@ export default function Writereview() {
62145 </ Modal >
63146 < ScrollView contentContainerStyle = { styles . scrollViewContent } >
64147 < View style = { styles . container } >
65-
66148 < View className = "w-[370px]" >
67149 < View className = "w-[370px] flex flex-row items-center pb-10 pt-5" >
68150 < TouchableOpacity onPress = { ( ) => router . back ( ) } >
@@ -71,14 +153,14 @@ export default function Writereview() {
71153 < Text className = "text-[#212121] font-UrbanistBold text-[24px] pl-5" > Write a Review</ Text >
72154 </ View >
73155 < View className = "w-[370px] flex justify-center items-center gap-5 mb-2" >
74- < Image source = { require ( "../../../assets/doctors/Ellipse.png" ) } />
156+ < Image source = { { uri : doctorImage } } style = { { width : 100 , height : 100 , borderRadius : 50 } } />
75157 < View >
76158 < Text className = "text-[#212121] font-UrbanistBold text-[20px] text-center" > How was your experience </ Text >
77- < Text className = "text-[#212121] font-UrbanistBold text-[20px] text-center" > with Dr. Drake Boeson ?</ Text >
159+ < Text className = "text-[#212121] font-UrbanistBold text-[20px] text-center" > with < Text > { name } </ Text > ?</ Text >
78160 </ View >
79161 < View className = " w-[350px] flex flex-row justify-center pb-3" >
80162 < SafeAreaView >
81- < FiveStarRating />
163+ < FiveStarRating selectedStars = { selectedStars } onStarPress = { handleStarPress } />
82164 </ SafeAreaView >
83165 </ View >
84166 </ View >
@@ -92,7 +174,7 @@ export default function Writereview() {
92174 placeholder = "Your review here..."
93175 style = { styles . textInput }
94176 />
95- < Text className = "text-[#212121] font-UrbanistBold text-[20px] pb-5 pt-3" > Would you recommend Dr. Drake Boeson to your friends?</ Text >
177+ < Text className = "text-[#212121] font-UrbanistBold text-[20px] pb-5 pt-3" > Would you recommend < Text > { name } </ Text > to your friends?</ Text >
96178 < View className = "flex flex-row items-center pl-2 justify-between w-[130px]" >
97179 { Options . map ( ( option , index ) => (
98180 < View key = { index } className = "flex flex-row items-center justify-center gap-2" >
@@ -106,7 +188,6 @@ export default function Writereview() {
106188 </ View >
107189 </ View >
108190 </ View >
109-
110191 < View className = "flex flex-row w-[370px] justify-between items-center pt-3" >
111192 < TouchableOpacity onPress = { ( ) => router . back ( ) } >
112193 < View className = " rounded-3xl pb-3 pt-3 pl-14 pr-14 bg-slate-100 " >
@@ -117,7 +198,7 @@ export default function Writereview() {
117198 </ TouchableOpacity >
118199 < View style = { styles . container } >
119200 < Pressable
120- onPress = { ( ) => setIsModalVisible ( true ) }
201+ onPress = { ( ) => writeReview ( ) }
121202 style = { [
122203 styles . button ,
123204 { backgroundColor : isSubmitEnabled ? '#246bfd' : '#3062cb' }
@@ -133,7 +214,6 @@ export default function Writereview() {
133214 </ >
134215 )
135216}
136-
137217const styles = StyleSheet . create ( {
138218 container : {
139219 flex : 1 ,
0 commit comments