|
1 | 1 | import { useState, useEffect, ChangeEvent } from 'react'
|
2 | 2 | import { supabase } from '../lib/api'
|
3 | 3 | import Auth from '../components/Auth'
|
4 |
| -import Account from '../components/Account' |
5 |
| -import Avatar from '../components/Avatar' |
| 4 | +import Account, { Profile } from '../components/Account' |
6 | 5 | import styles from '../styles/Home.module.css'
|
7 | 6 | import { AuthSession } from '../../../dist/main'
|
8 |
| -import { DEFAULT_AVATARS_BUCKET } from '../lib/constants' |
9 |
| - |
10 |
| -type Profile = { |
11 |
| - avatar_url: string |
12 |
| - username: string |
13 |
| - dob: string |
14 |
| -} |
15 | 7 |
|
16 | 8 | export default function Home() {
|
17 | 9 | const [session, setSession] = useState<AuthSession | null>(null)
|
| 10 | + const [profiles, setProfiles] = useState<Profile[] | null>(null) |
18 | 11 |
|
19 | 12 | useEffect(() => {
|
20 | 13 | setSession(supabase.auth.session())
|
| 14 | + getPublicProfiles() |
| 15 | + |
| 16 | + // TODO: listen to changes in profile table |
| 17 | + // const mySubscription = supabase |
| 18 | + // .from('profiles') |
| 19 | + // .on('*', (payload) => { |
| 20 | + // console.log('Change received!', payload) |
| 21 | + // }) |
| 22 | + // .subscribe() |
21 | 23 |
|
22 | 24 | supabase.auth.onAuthStateChange((_event: string, session: AuthSession | null) => {
|
23 | 25 | setSession(session)
|
24 | 26 | })
|
25 | 27 | }, [])
|
26 | 28 |
|
| 29 | + async function getPublicProfiles() { |
| 30 | + try { |
| 31 | + const { data, error } = await supabase |
| 32 | + .from('profiles') |
| 33 | + .select('id, username, avatar_url, dob') |
| 34 | + if (error) { |
| 35 | + throw error |
| 36 | + } |
| 37 | + console.log('data', data) |
| 38 | + setProfiles(data) |
| 39 | + } catch (error) { |
| 40 | + console.log('error', error.message) |
| 41 | + } |
| 42 | + } |
| 43 | + |
27 | 44 | return (
|
28 | 45 | <div className={styles.container}>
|
29 |
| - {!session ? <Auth /> : <Account key={session.user.id} session={session} />} |
| 46 | + {!session ? ( |
| 47 | + <Auth /> |
| 48 | + ) : ( |
| 49 | + <div style={{ display: 'flex', gap: 20 }}> |
| 50 | + <div> |
| 51 | + <Account key={session.user.id} session={session} /> |
| 52 | + </div> |
| 53 | + <div> |
| 54 | + {profiles?.map((profile) => ( |
| 55 | + <div key={profile.id}> |
| 56 | + <p>{profile.id}</p> |
| 57 | + <p>{profile.username}</p> |
| 58 | + </div> |
| 59 | + ))} |
| 60 | + </div> |
| 61 | + </div> |
| 62 | + )} |
30 | 63 | </div>
|
31 | 64 | )
|
32 | 65 | }
|
0 commit comments