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