1
1
import { useState , useEffect , ChangeEvent } from 'react'
2
2
import { supabase } from '../lib/api'
3
3
import UploadButton from '../components/UploadButton'
4
- import Avatar from '../components /Avatar'
4
+ import Avatar from './Avatar'
5
5
import styles from '../styles/Home.module.css'
6
6
import { AuthSession } from '../../../dist/main'
7
- import { DEFAULT_AVATARS_BUCKET } from '../lib/constants'
8
-
9
- export type Profile = {
10
- id : string
11
- avatar_url : string
12
- username : string
13
- dob : string
14
- }
7
+ import { DEFAULT_AVATARS_BUCKET , Profile } from '../lib/constants'
15
8
16
9
export default function Account ( { session } : { session : AuthSession } ) {
10
+ const [ loading , setLoading ] = useState < boolean > ( true )
17
11
const [ avatar , setAvatar ] = useState < string | null > ( null )
18
12
const [ username , setUsername ] = useState < string | null > ( null )
19
- const [ dob , setDob ] = useState < string | null > ( null )
13
+ const [ website , setWebsite ] = useState < string | null > ( null )
20
14
21
15
useEffect ( ( ) => {
22
16
getProfile ( )
@@ -48,15 +42,15 @@ export default function Account({ session }: { session: AuthSession }) {
48
42
49
43
let { error : updateError } = await supabase . from ( 'profiles' ) . upsert ( {
50
44
id : user . id ,
51
- avatar_url : fileName ,
45
+ avatar_url : filePath ,
52
46
} )
53
47
54
48
if ( updateError ) {
55
49
throw updateError
56
50
}
57
51
58
52
setAvatar ( null )
59
- setAvatar ( fileName )
53
+ setAvatar ( filePath )
60
54
} catch ( error ) {
61
55
alert ( error . message )
62
56
}
@@ -65,16 +59,17 @@ export default function Account({ session }: { session: AuthSession }) {
65
59
function setProfile ( profile : Profile ) {
66
60
setAvatar ( profile . avatar_url )
67
61
setUsername ( profile . username )
68
- setDob ( profile . dob )
62
+ setWebsite ( profile . website )
69
63
}
70
64
71
65
async function getProfile ( ) {
72
66
try {
67
+ setLoading ( true )
73
68
const user = supabase . auth . user ( )
74
69
75
70
let { data, error } = await supabase
76
71
. from ( 'profiles' )
77
- . select ( `username, dob , avatar_url` )
72
+ . select ( `username, website , avatar_url` )
78
73
. eq ( 'id' , user . id )
79
74
. single ( )
80
75
@@ -85,17 +80,21 @@ export default function Account({ session }: { session: AuthSession }) {
85
80
setProfile ( data )
86
81
} catch ( error ) {
87
82
console . log ( 'error' , error . message )
83
+ } finally {
84
+ setLoading ( false )
88
85
}
89
86
}
90
87
91
88
async function updateProfile ( ) {
92
89
try {
90
+ setLoading ( true )
93
91
const user = supabase . auth . user ( )
94
92
95
93
const updates = {
96
94
id : user . id ,
97
95
username,
98
- dob,
96
+ website,
97
+ updated_at : new Date ( ) ,
99
98
}
100
99
101
100
let { error } = await supabase . from ( 'profiles' ) . upsert ( updates , {
@@ -107,6 +106,8 @@ export default function Account({ session }: { session: AuthSession }) {
107
106
}
108
107
} catch ( error ) {
109
108
alert ( error . message )
109
+ } finally {
110
+ setLoading ( false )
110
111
}
111
112
}
112
113
@@ -123,7 +124,7 @@ export default function Account({ session }: { session: AuthSession }) {
123
124
>
124
125
< div className = { styles . card } >
125
126
< div className = { styles . avatarContainer } >
126
- < Avatar avatar = { avatar } />
127
+ < Avatar url = { avatar } size = { 270 } />
127
128
</ div >
128
129
< UploadButton onUpload = { uploadAvatar } />
129
130
</ div >
@@ -142,13 +143,18 @@ export default function Account({ session }: { session: AuthSession }) {
142
143
/>
143
144
</ div >
144
145
< div >
145
- < label htmlFor = "dob" > Date of birth</ label >
146
- < input id = "dob" type = "date" value = { dob || '' } onChange = { ( e ) => setDob ( e . target . value ) } />
146
+ < label htmlFor = "website" > Website</ label >
147
+ < input
148
+ id = "website"
149
+ type = "website"
150
+ value = { website || '' }
151
+ onChange = { ( e ) => setWebsite ( e . target . value ) }
152
+ />
147
153
</ div >
148
154
149
155
< div >
150
- < button className = "button block primary" onClick = { ( ) => updateProfile ( ) } >
151
- Update profile
156
+ < button className = "button block primary" onClick = { ( ) => updateProfile ( ) } disabled = { loading } >
157
+ { loading ? 'Loading ...' : 'Update' }
152
158
</ button >
153
159
</ div >
154
160
0 commit comments