1
1
import { useState , useEffect , ChangeEvent } from 'react'
2
2
import { supabase } from '../lib/api'
3
3
import Auth from '../components/Auth'
4
- import UploadButton from '../components/UploadButton '
4
+ import Account from '../components/Account '
5
5
import Avatar from '../components/Avatar'
6
6
import styles from '../styles/Home.module.css'
7
7
import { AuthSession } from '../../../dist/main'
@@ -15,9 +15,6 @@ type Profile = {
15
15
16
16
export default function Home ( ) {
17
17
const [ session , setSession ] = useState < AuthSession | null > ( null )
18
- const [ avatar , setAvatar ] = useState < string | null > ( null )
19
- const [ username , setUsername ] = useState < string | null > ( null )
20
- const [ dob , setDob ] = useState < string | null > ( null )
21
18
22
19
useEffect ( ( ) => {
23
20
setSession ( supabase . auth . session ( ) )
@@ -27,174 +24,9 @@ export default function Home() {
27
24
} )
28
25
} , [ ] )
29
26
30
- useEffect ( ( ) => {
31
- if ( session ) {
32
- getProfile ( )
33
- } else {
34
- setAvatar ( null )
35
- setUsername ( null )
36
- setDob ( null )
37
- }
38
- } , [ session ] )
39
-
40
- async function signOut ( ) {
41
- const { error } = await supabase . auth . signOut ( )
42
- if ( error ) console . log ( 'Error logging out:' , error . message )
43
- }
44
-
45
- async function uploadAvatar ( event : ChangeEvent < HTMLInputElement > ) {
46
- try {
47
- if ( ! session ) {
48
- throw new Error ( 'uploadAvatar() Not logged in.' )
49
- }
50
-
51
- const user = supabase . auth . user ( )
52
-
53
- if ( ! event . target . files || event . target . files . length == 0 ) {
54
- throw 'You must select an image to upload.'
55
- }
56
-
57
- const file = event . target . files [ 0 ]
58
- const fileExt = file . name . split ( '.' ) . pop ( )
59
- const fileName = `${ session ?. user . id } ${ Math . random ( ) } .${ fileExt } `
60
- const filePath = `${ DEFAULT_AVATARS_BUCKET } /${ fileName } `
61
-
62
- let { error : uploadError } = await supabase . storage . uploadFile ( filePath , file )
63
-
64
- if ( uploadError ) {
65
- throw uploadError
66
- }
67
-
68
- let { error : updateError } = await supabase . from ( 'profiles' ) . upsert ( {
69
- id : user . id ,
70
- avatar_url : fileName ,
71
- } )
72
-
73
- if ( updateError ) {
74
- throw updateError
75
- }
76
-
77
- setAvatar ( null )
78
- setAvatar ( fileName )
79
- } catch ( error ) {
80
- alert ( error . message )
81
- }
82
- }
83
-
84
- function setProfile ( profile : Profile ) {
85
- setAvatar ( profile . avatar_url )
86
- setUsername ( profile . username )
87
- setDob ( profile . dob )
88
- }
89
-
90
- async function getProfile ( ) {
91
- try {
92
- if ( ! session ) {
93
- throw new Error ( 'getProfile() Not logged in.' )
94
- }
95
-
96
- const user = supabase . auth . user ( )
97
-
98
- let { data, error } = await supabase
99
- . from ( 'profiles' )
100
- . select ( `username, dob, avatar_url` )
101
- . eq ( 'id' , user . id )
102
- . single ( )
103
-
104
- if ( error ) {
105
- throw error
106
- }
107
-
108
- setProfile ( data )
109
- } catch ( error ) {
110
- console . log ( 'error' , error . message )
111
- }
112
- }
113
-
114
- async function updateProfile ( ) {
115
- try {
116
- if ( ! session ) {
117
- throw new Error ( 'Not logged in.' )
118
- }
119
-
120
- const user = supabase . auth . user ( )
121
-
122
- const updates = {
123
- id : user . id ,
124
- username,
125
- dob,
126
- }
127
-
128
- let { error } = await supabase . from ( 'profiles' ) . upsert ( updates , {
129
- returning : 'minimal' , // Don't return the value after inserting
130
- } )
131
-
132
- if ( error ) {
133
- throw error
134
- }
135
- } catch ( error ) {
136
- console . log ( 'error' , error . message )
137
- }
138
- }
139
-
140
27
return (
141
28
< div className = { styles . container } >
142
- { ! session ? (
143
- < Auth />
144
- ) : (
145
- < div
146
- style = { {
147
- minWidth : 250 ,
148
- maxWidth : 600 ,
149
- margin : 'auto' ,
150
- display : 'flex' ,
151
- flexDirection : 'column' ,
152
- gap : 20 ,
153
- } }
154
- >
155
- < div className = { styles . card } >
156
- < div className = { styles . avatarContainer } >
157
- < Avatar avatar = { avatar } />
158
- </ div >
159
- < UploadButton onUpload = { uploadAvatar } />
160
- </ div >
161
-
162
- < div >
163
- < label htmlFor = "email" > Email</ label >
164
- < input id = "email" type = "text" value = { session . user . email } disabled />
165
- </ div >
166
- < div >
167
- < label htmlFor = "username" > Username</ label >
168
- < input
169
- id = "username"
170
- type = "text"
171
- value = { username || '' }
172
- onChange = { ( e ) => setUsername ( e . target . value ) }
173
- />
174
- </ div >
175
- < div >
176
- < label htmlFor = "dob" > Date of birth</ label >
177
- < input
178
- id = "dob"
179
- type = "date"
180
- value = { dob || '' }
181
- onChange = { ( e ) => setDob ( e . target . value ) }
182
- />
183
- </ div >
184
-
185
- < div >
186
- < button className = "button block primary" onClick = { ( ) => updateProfile ( ) } >
187
- Update profile
188
- </ button >
189
- </ div >
190
-
191
- < div >
192
- < button className = "button block" onClick = { ( ) => signOut ( ) } >
193
- Sign Out
194
- </ button >
195
- </ div >
196
- </ div >
197
- ) }
29
+ { ! session ? < Auth /> : < Account key = { session . user . id } session = { session } /> }
198
30
</ div >
199
31
)
200
32
}
0 commit comments