@@ -13,18 +13,23 @@ import {
1313import { supabase } from "../supabase" ;
1414import Cardcomponent from "./doctorcard/cards" ;
1515import Cardscomponent from "./doctorcard/cardss" ;
16+ import { useDispatch , useSelector } from "react-redux" ;
17+ import { RootState } from "@/redux/store/store" ;
18+ import { getAppointments } from "@/redux/reducers/appointment" ;
1619
1720function Screen ( ) {
1821 const [ upcoming , setUpcoming ] = useState ( true ) ;
1922 const [ cancels , setCancels ] = useState ( false ) ;
2023 const [ complete , setComlpete ] = useState ( false ) ;
2124 const [ notupcome , setNotupcome ] = useState ( false ) ;
2225 const [ isModalVisible , setIsModalVisible ] = useState ( false ) ;
23- const [ appointmentData , setAppointmentData ] = useState < any [ ] > ( [ ] ) ;
2426 const { doctorId } = useLocalSearchParams < { doctorId : string } > ( ) ;
25- const [ appointId , setAppointId ] = useState ( { } ) ;
27+ const appointments = useSelector (
28+ ( state : RootState ) => state . appointment . appointments
29+ ) ;
2630 const [ canceledData , setcanceledData ] = useState < any [ ] > ( [ ] ) ;
27-
31+ const [ appointId , setAppointId ] = useState ( { } ) ;
32+ const dispatch = useDispatch ( ) ;
2833 const handleUpcoming = ( ) => {
2934 setCancels ( false ) ;
3035 setUpcoming ( true ) ;
@@ -55,27 +60,39 @@ function Screen() {
5560
5661 const fetchAppointment = async ( ) => {
5762 try {
58- const { data : userData , error : userError } = await supabase . auth . getUser ( ) ;
63+ const { data : userData , error : userError } =
64+ await supabase . auth . getUser ( ) ;
5965 if ( userError ) throw userError ;
6066 const userId = userData ?. user ?. id ;
6167 const { data, error } = await supabase
6268 . from ( "appointment" )
63- . select ( `
69+ . select (
70+ `
6471 *,
6572 doctor(
6673 id,
6774 name,
6875 role,
6976 image,
7077 hospital
71- )
72- ` )
78+ ),
79+ patient(
80+ id,
81+ email,
82+ phone,
83+ full_name,
84+ nickname,
85+ date_of_birth,
86+ gender
87+ )
88+ `
89+ )
7390 . eq ( "patient_id" , userId ) ;
7491
7592 if ( error ) {
7693 console . log ( "Error occurred while fetching appointments" , error ) ;
7794 } else {
78- setAppointmentData ( data ) ;
95+ dispatch ( getAppointments ( data ) ) ;
7996 if ( data . length === 0 ) {
8097 setNotupcome ( true ) ;
8198 setUpcoming ( false ) ;
@@ -92,10 +109,10 @@ function Screen() {
92109 fetchAppointment ( ) ;
93110 } , [ ] ) ;
94111
95-
96112 const cancelAppointment = async ( ) => {
97113 try {
98- const { data : userData , error : userError } = await supabase . auth . getUser ( ) ;
114+ const { data : userData , error : userError } =
115+ await supabase . auth . getUser ( ) ;
99116 if ( userError ) throw userError ;
100117 const userId = userData ?. user ?. id ;
101118 const { data, error } = await supabase
@@ -117,14 +134,13 @@ function Screen() {
117134 cancelAppointment ( ) ;
118135 } , [ ] ) ;
119136
120-
121137 const getPackageIcon = ( typecall : any ) => {
122138 switch ( typecall ) {
123- case ' Voice Call' :
139+ case " Voice Call" :
124140 return require ( "../../assets/appointmentIcon/voice.png" ) ;
125- case ' Video Call' :
141+ case " Video Call" :
126142 return require ( "../../assets/appointmentIcon/video.png" ) ;
127- case ' Messaging' :
143+ case " Messaging" :
128144 return require ( "../../assets/appointmentIcon/message.png" ) ;
129145 default :
130146 return null ;
@@ -136,14 +152,18 @@ function Screen() {
136152 appointmentId : appointment . id ,
137153 appointment_date : appointment . appointment_date ,
138154 appointment_time : appointment . appointment_time ,
139- doctorId :appointment . doctor ?. id ,
140- doctorname :appointment . doctor . name ,
141- doctimage :appointment . doctor . image ,
142- typecall :appointment . package
155+ doctorId : appointment . doctor ?. id ,
156+ doctorname : appointment . doctor . name ,
157+ doctimage : appointment . doctor . image ,
158+ typecall : appointment . package ,
143159 } ) ;
144160 setIsModalVisible ( true ) ;
145161 } ;
146162
163+ useEffect ( ( ) => {
164+ fetchAppointment ( ) ;
165+ } , [ ] ) ;
166+
147167 return (
148168 < >
149169 < View className = "pb-6" >
@@ -226,26 +246,27 @@ function Screen() {
226246 >
227247 { cancels && (
228248 < View style = { styles . content } >
229- { canceledData . map ( ( canceled , index ) => (
230- < Cardcomponent
231- key = { index }
232- name = { canceled . doctorname }
233- imager = { canceled . doctimage }
234- typecall = { canceled . package }
235- action = "Cancelled"
236- date = { canceled . appointment_date }
237- time = { canceled . appointment_time . slice ( 0 , 5 ) }
238- imagerr = { getPackageIcon ( canceled . package ) }
239- styles = { styles . cancelStyles }
240- />
241- ) ) }
249+ { canceledData . map ( ( canceled , index ) => (
250+ < Cardcomponent
251+ key = { index }
252+ name = { canceled . doctorname }
253+ imager = { canceled . doctimage }
254+ typecall = { canceled . package }
255+ action = "Cancelled"
256+ date = { canceled . appointment_date }
257+ time = { canceled . appointment_time . slice ( 0 , 5 ) }
258+ imagerr = { getPackageIcon ( canceled . package ) }
259+ styles = { styles . cancelStyles }
260+ />
261+ ) ) }
242262 </ View >
243263 ) }
244264 { upcoming && (
245265 < View style = { styles . content } >
246- { appointmentData . map ( ( appointment : any , index : any ) => (
266+ { appointments . map ( ( appointment : any , index : any ) => (
247267 < Cardscomponent
248268 key = { index }
269+ appointment = { appointment }
249270 name = { appointment . doctor . name }
250271 imager = { appointment . doctor . image }
251272 typecall = { appointment . package }
@@ -262,8 +283,8 @@ function Screen() {
262283 pathname : "/Appointments/reschedul" ,
263284 params : {
264285 appointmentId : appointment . id ,
265- appointmentdate :appointment . appointment_date ,
266- appointmentime :appointment . appointment_time ,
286+ appointmentdate : appointment . appointment_date ,
287+ appointmentime : appointment . appointment_time ,
267288 } ,
268289 } )
269290 }
@@ -275,7 +296,9 @@ function Screen() {
275296
276297 { notupcome && (
277298 < View style = { styles . centerbar } >
278- < Image source = { require ( "../../assets/appointmentIcon/first.png" ) } />
299+ < Image
300+ source = { require ( "../../assets/appointmentIcon/first.png" ) }
301+ />
279302 < Text style = { styles . centerbartitle } >
280303 You don't have an appointment yet
281304 </ Text >
@@ -341,15 +364,14 @@ const styles = StyleSheet.create({
341364 flexGrow : 1 ,
342365 paddingBottom : 50 ,
343366 paddingTop : 20 ,
344- height :"80%"
367+ height : "80%" ,
345368 } ,
346369 centerbar : {
347370 display : "flex" ,
348371 justifyContent : "center" ,
349372 alignItems : "center" ,
350373 height : "76%" ,
351374 gap : 20 ,
352-
353375 } ,
354376 centerbartitle : {
355377 fontSize : 20 ,
@@ -442,4 +464,4 @@ const styles = StyleSheet.create({
442464 mode : {
443465 color : "#f75555" ,
444466 } ,
445- } ) ;
467+ } ) ;
0 commit comments