@@ -21,10 +21,13 @@ export function Home() {
2121
2222 const { userId } = useContext ( AuthContext ) ;
2323
24- const { id, appointmentId} = useGlobalSearchParams ( )
25- const doctorId = id ;
2624 const chatRoomId = "matadores-medica" + uuid . v4 ( ) ;
2725
26+ const [ loggedInUserId , setLoggedInUserId ] = useState < string > ( "" ) ;
27+ const CDNURL =
28+ "https://vbwbfflzxuhktdvpbspd.supabase.co/storage/v1/object/public/patients/" ;
29+ const { doctorId, appointmentId } = useGlobalSearchParams ( ) ;
30+
2831 useEffect ( ( ) => {
2932 if ( userId ) {
3033 fetchPatientData ( userId , setPatientData ) ;
@@ -35,44 +38,40 @@ export function Home() {
3538
3639 useEffect ( ( ) => {
3740 const setupClient = async ( ) => {
38- try {
39- if ( patientData && patientData [ 0 ] ) {
40- const user = {
41- id : patientData [ 0 ] . id ,
42- name : `${ patientData [ 0 ] . first_name } ${ patientData [ 0 ] . last_name } ` ,
43- image : "https://i.imgur.com/fR9Jz14.png" ,
44- } ;
45-
46- await chatClient . connectUser (
47- user ,
48- chatClient . devToken ( user . id ) ) ;
49- setClientIsReady ( true ) ;
50- setClient ( chatClient ) ;
41+ try {
42+ if ( patientData && patientData [ 0 ] ) {
43+ setLoggedInUserId ( patientData [ 0 ] . id ) ;
44+ const user = {
45+ id : patientData [ 0 ] . id ,
46+ name : `${ patientData [ 0 ] . first_name } ${ patientData [ 0 ] . last_name } ` ,
47+ image : `${ CDNURL + patientData [ 0 ] ?. image } ` ,
48+ } ;
49+ await chatClient . connectUser ( user , chatClient . devToken ( user . id ) ) ;
5150
52- const channel = chatClient . channel ( "messaging" , ` ${ chatRoomId } ` , {
53- image : "https://vbwbfflzxuhktdvpbspd.supabase.co/storage/v1/object/public/doctors/adaptive_icon.png" ,
54- name : "Medica Chat Room" ,
55- members : [ user ?. id , ` ${ doctorId } ` ] ,
56- } ) ;
57- await channel . watch ( ) ;
58- setChannel ( channel ) ;
59- } else {
60- console . log ( "No patient data found" ) ;
61- }
62- } catch ( error ) {
63- console . error ( "An error occurred while connecting the user:" , error ) ;
51+ setClientIsReady ( true ) ;
52+ setClient ( chatClient ) ;
53+ const channel = chatClient . channel ( "messaging" , ` ${ chatRoomId } ` , {
54+ image :
55+ "https://vbwbfflzxuhktdvpbspd.supabase.co/storage/v1/object/public/doctors/adaptive_icon.png" ,
56+ name : ` ${ user ?. name } in the chat` ,
57+ members : [ user ?. id , ` ${ doctorId } ` ] ,
58+ } ) ;
59+ await channel . watch ( ) ;
60+ setChannel ( channel ) ;
61+ } else {
62+ console . log ( "No patient data found" ) ;
6463 }
64+ } catch ( error ) {
65+ console . error ( "An error occurred while connecting the user:" , error ) ;
66+ }
6567 } ;
6668 if ( ! chatClient . userID ) {
6769 setupClient ( ) ;
6870 }
6971 // if(client){return async()=> await client.disconnectUser()}
7072 } , [ patientData ] ) ;
7173
72- return (
73- < ChannelLists appointmentId = { appointmentId } />
74- )
74+ return < ChannelLists appointmentId = { appointmentId } loggedInUserId = { loggedInUserId } doctorId = { doctorId } />
7575}
7676
77- export default Home ;
78-
77+ export default Home ;
0 commit comments