1
1
import React , { useContext , useState , useEffect } from "react" ;
2
- import { Pressable , StyleSheet , View , FlatList , TextInput , Platform } from "react-native" ;
2
+ import {
3
+ Pressable ,
4
+ StyleSheet ,
5
+ View ,
6
+ FlatList ,
7
+ TextInput ,
8
+ Platform ,
9
+ } from "react-native" ;
3
10
import { SvgXml } from "react-native-svg" ;
4
11
import { Dropdown } from "react-native-element-dropdown" ;
5
12
import Button from "@/components/UI/Button" ;
6
13
import { Colors } from "@/constants/Colors" ;
7
- import DateTimePicker from ' @react-native-community/datetimepicker' ;
14
+ import DateTimePicker from " @react-native-community/datetimepicker" ;
8
15
import {
9
16
CalenderIcon ,
10
17
CalenderIconDark ,
@@ -19,18 +26,19 @@ import { ThemeContext } from "@/ctx/ThemeContext";
19
26
import { supabase } from "@/lib/supabase" ;
20
27
import { fetchPatientData , getPatientData } from "@/utils/LoggedInUser" ;
21
28
import { User } from "@/utils/LoggedInUser" ;
29
+ import { router } from "expo-router" ;
22
30
23
31
interface PatientData {
24
- id : string ,
25
- first_name : string ,
26
- last_name : string ,
27
- date_of_birth : string ,
28
- gender : string ,
29
- country : string ,
30
- email : string ,
31
- phone : string ,
32
+ id : string ;
33
+ first_name : string ;
34
+ last_name : string ;
35
+ date_of_birth : string ;
36
+ gender : string ;
37
+ country : string ;
38
+ email : string ;
39
+ phone : string ;
32
40
}
33
- interface props {
41
+ interface props {
34
42
disabled ?: boolean ;
35
43
editable ?: boolean ;
36
44
}
@@ -55,7 +63,7 @@ function EditProfile() {
55
63
56
64
const onChange = ( event : any , selectedDate ?: Date ) => {
57
65
const currentDate = selectedDate || date ;
58
- setShow ( Platform . OS === ' ios' ) ;
66
+ setShow ( Platform . OS === " ios" ) ;
59
67
setDate ( currentDate ) ;
60
68
} ;
61
69
@@ -65,11 +73,11 @@ function EditProfile() {
65
73
66
74
const formatDate = ( date : Date ) : string => {
67
75
const year = date . getFullYear ( ) ;
68
- const month = ( date . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , '0' ) ; // Months are 0-based, so add 1
69
- const day = date . getDate ( ) . toString ( ) . padStart ( 2 , '0' ) ;
76
+ const month = ( date . getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , "0" ) ; // Months are 0-based, so add 1
77
+ const day = date . getDate ( ) . toString ( ) . padStart ( 2 , "0" ) ;
70
78
return `${ year } -${ month } -${ day } ` ;
71
79
} ;
72
-
80
+
73
81
useEffect ( ( ) => {
74
82
getPatientData ( supabase , setUserData ) ;
75
83
} , [ ] ) ;
@@ -78,37 +86,38 @@ function EditProfile() {
78
86
const id : string = userData ?. id as string ;
79
87
if ( id ) {
80
88
fetchPatientData ( id , setPatientData ) ;
81
- if ( userData ) {
82
- setEmail ( userData ?. email as string )
83
- setPhone ( userData ?. phone as string )
89
+ if ( userData ) {
90
+ setEmail ( userData ?. email as string ) ;
91
+ setPhone ( userData ?. phone as string ) ;
84
92
}
85
93
}
86
94
} , [ userData ] ) ;
87
-
95
+
88
96
useEffect ( ( ) => {
89
- if ( patientData ) {
90
- setCountry ( patientData [ 0 ] ?. country as string )
91
- setFirstName ( patientData [ 0 ] ?. first_name as string )
92
- setLastName ( patientData [ 0 ] ?. last_name as string )
93
- setGender ( patientData [ 0 ] ?. gender as string )
94
- setBirthDate ( patientData [ 0 ] ?. date_of_birth as string )
95
- setId ( patientData [ 0 ] ?. id )
96
- setPhone ( patientData [ 0 ] ?. phone as string )
97
- }
97
+ if ( patientData ) {
98
+ setCountry ( patientData [ 0 ] ?. country as string ) ;
99
+ setFirstName ( patientData [ 0 ] ?. first_name as string ) ;
100
+ setLastName ( patientData [ 0 ] ?. last_name as string ) ;
101
+ setGender ( patientData [ 0 ] ?. gender as string ) ;
102
+ setBirthDate ( patientData [ 0 ] ?. date_of_birth as string ) ;
103
+ setId ( patientData [ 0 ] ?. id ) ;
104
+ setPhone ( patientData [ 0 ] ?. phone as string ) ;
98
105
}
99
- , [ patientData ] ) ;
106
+ } , [ patientData ] ) ;
100
107
101
108
useEffect ( ( ) => {
102
- setBirthDate ( `${ formatDate ( date ) } ` )
103
- } , [ date ] )
109
+ setBirthDate ( `${ formatDate ( date ) } ` ) ;
110
+ } , [ date ] ) ;
104
111
105
112
useEffect ( ( ) => {
106
113
const getUser = async ( ) => {
107
- const { data :{ user} } = await supabase . auth . getUser ( )
108
- setEmail ( user ?. email as string )
109
- }
110
- getUser ( )
111
- } , [ ] )
114
+ const {
115
+ data : { user } ,
116
+ } = await supabase . auth . getUser ( ) ;
117
+ setEmail ( user ?. email as string ) ;
118
+ } ;
119
+ getUser ( ) ;
120
+ } , [ ] ) ;
112
121
113
122
const countryNames : { label : string ; value : string } [ ] = Object . keys (
114
123
typedCountries
@@ -119,22 +128,26 @@ function EditProfile() {
119
128
const handleUpdate = async ( ) => {
120
129
try {
121
130
const data = await supabase
122
- . from ( ' patients' )
131
+ . from ( " patients" )
123
132
. update ( {
124
133
first_name : firstName ,
125
134
last_name : lastName ,
126
135
gender : gender ,
127
136
date_of_birth : birthDate ,
128
137
country : country ,
129
- phone : phone
138
+ phone : phone ,
130
139
} )
131
- . eq ( 'id' , id ) ;
140
+ . eq ( "id" , id ) ;
141
+
142
+ const res = await supabase . auth . updateUser ( { email : email } ) ;
143
+
144
+ console . log ( res ) ;
132
145
133
146
if ( data . error ) throw data . error ;
134
- console . log ( data )
135
147
alert ( "Profile updated successfully" ) ;
148
+ router . back ( ) ;
136
149
} catch ( error ) {
137
- console . log ( error )
150
+ console . log ( error ) ;
138
151
alert ( "Error updating profile" ) ;
139
152
}
140
153
} ;
@@ -165,32 +178,44 @@ function EditProfile() {
165
178
Typography . semiBold . medium ,
166
179
{
167
180
backgroundColor :
168
- theme === "light" ? Colors . grayScale . _50 : Colors . dark . _2 ,
181
+ theme === "light"
182
+ ? Colors . grayScale . _50
183
+ : Colors . dark . _2 ,
169
184
borderRadius : 15 ,
170
185
flexDirection : "row" ,
171
186
alignItems : "center" ,
172
187
paddingHorizontal : 20 ,
173
- color : theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
188
+ color :
189
+ theme === "light"
190
+ ? Colors . grayScale . _900
191
+ : Colors . others . white ,
174
192
paddingVertical : 15 ,
175
- } ] }
193
+ } ,
194
+ ] }
176
195
placeholderTextColor = { Colors . grayScale . _500 }
177
196
placeholder = "First Name"
178
197
value = { firstName }
179
- onChangeText = { ( text ) => setFirstName ( text ) }
198
+ onChangeText = { ( text ) => setFirstName ( text ) }
180
199
/>
181
200
< TextInput
182
201
style = { [
183
202
Typography . semiBold . medium ,
184
203
{
185
204
backgroundColor :
186
- theme === "light" ? Colors . grayScale . _50 : Colors . dark . _2 ,
205
+ theme === "light"
206
+ ? Colors . grayScale . _50
207
+ : Colors . dark . _2 ,
187
208
borderRadius : 15 ,
188
209
flexDirection : "row" ,
189
210
alignItems : "center" ,
190
211
paddingHorizontal : 20 ,
191
- color : theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
212
+ color :
213
+ theme === "light"
214
+ ? Colors . grayScale . _900
215
+ : Colors . others . white ,
192
216
paddingVertical : 15 ,
193
- } ] }
217
+ } ,
218
+ ] }
194
219
placeholderTextColor = { Colors . grayScale . _500 }
195
220
placeholder = "Last Name"
196
221
value = { lastName }
@@ -204,13 +229,16 @@ function EditProfile() {
204
229
flexDirection : "row" ,
205
230
alignItems : "center" ,
206
231
paddingHorizontal : 20 ,
207
- } } >
232
+ } }
233
+ >
208
234
< TextInput
209
235
style = { [
210
236
Typography . semiBold . medium ,
211
237
{
212
238
color :
213
- theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
239
+ theme === "light"
240
+ ? Colors . grayScale . _900
241
+ : Colors . others . white ,
214
242
flexGrow : 1 ,
215
243
paddingVertical : 15 ,
216
244
} ,
@@ -223,19 +251,17 @@ function EditProfile() {
223
251
/>
224
252
< Pressable onPress = { showDatepicker } >
225
253
< SvgXml
226
- xml = {
227
- theme === "light" ? CalenderIcon : CalenderIconDark
228
- }
254
+ xml = { theme === "light" ? CalenderIcon : CalenderIconDark }
229
255
/>
230
256
</ Pressable >
231
257
{ show && (
232
- < DateTimePicker
233
- value = { date }
234
- mode = "date"
235
- display = "default"
236
- onChange = { onChange }
237
- />
238
- ) }
258
+ < DateTimePicker
259
+ value = { date }
260
+ mode = "date"
261
+ display = "default"
262
+ onChange = { onChange }
263
+ />
264
+ ) }
239
265
</ View >
240
266
< View
241
267
style = { {
@@ -245,13 +271,16 @@ function EditProfile() {
245
271
flexDirection : "row" ,
246
272
alignItems : "center" ,
247
273
paddingHorizontal : 20 ,
248
- } } >
274
+ } }
275
+ >
249
276
< TextInput
250
277
style = { [
251
278
Typography . semiBold . medium ,
252
279
{
253
280
color :
254
- theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
281
+ theme === "light"
282
+ ? Colors . grayScale . _900
283
+ : Colors . others . white ,
255
284
flexGrow : 1 ,
256
285
paddingVertical : 15 ,
257
286
} ,
@@ -263,9 +292,7 @@ function EditProfile() {
263
292
/>
264
293
< Pressable >
265
294
< SvgXml
266
- xml = {
267
- theme === "light" ? MessageIcon : MessageIconDark
268
- }
295
+ xml = { theme === "light" ? MessageIcon : MessageIconDark }
269
296
/>
270
297
</ Pressable >
271
298
</ View >
@@ -329,14 +356,20 @@ function EditProfile() {
329
356
Typography . semiBold . medium ,
330
357
{
331
358
backgroundColor :
332
- theme === "light" ? Colors . grayScale . _50 : Colors . dark . _2 ,
359
+ theme === "light"
360
+ ? Colors . grayScale . _50
361
+ : Colors . dark . _2 ,
333
362
borderRadius : 15 ,
334
363
flexDirection : "row" ,
335
364
alignItems : "center" ,
336
365
paddingHorizontal : 20 ,
337
- color : theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
366
+ color :
367
+ theme === "light"
368
+ ? Colors . grayScale . _900
369
+ : Colors . others . white ,
338
370
paddingVertical : 15 ,
339
- } ] }
371
+ } ,
372
+ ] }
340
373
placeholderTextColor = { Colors . grayScale . _500 }
341
374
placeholder = "+250"
342
375
value = { phone }
@@ -347,14 +380,20 @@ function EditProfile() {
347
380
Typography . semiBold . medium ,
348
381
{
349
382
backgroundColor :
350
- theme === "light" ? Colors . grayScale . _50 : Colors . dark . _2 ,
383
+ theme === "light"
384
+ ? Colors . grayScale . _50
385
+ : Colors . dark . _2 ,
351
386
borderRadius : 15 ,
352
387
flexDirection : "row" ,
353
388
alignItems : "center" ,
354
389
paddingHorizontal : 20 ,
355
- color : theme === "light" ? Colors . grayScale . _900 : Colors . others . white ,
390
+ color :
391
+ theme === "light"
392
+ ? Colors . grayScale . _900
393
+ : Colors . others . white ,
356
394
paddingVertical : 15 ,
357
- } ] }
395
+ } ,
396
+ ] }
358
397
placeholderTextColor = { Colors . grayScale . _500 }
359
398
placeholder = "Gender"
360
399
value = { gender }
@@ -378,7 +417,7 @@ function EditProfile() {
378
417
) }
379
418
</ >
380
419
) ;
381
- } ;
420
+ }
382
421
383
422
export default EditProfile ;
384
423
0 commit comments