Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix all typescript complaints

Paul Copplestone63584c59c0da
diff --git a/examples/nextjs-ts-user-managment/components/Account.tsx b/examples/nextjs-ts-user-managment/components/Account.tsx
index cd9ddfb40a9..684e4668274 100644
--- a/examples/nextjs-ts-user-managment/components/Account.tsx
+++ b/examples/nextjs-ts-user-managment/components/Account.tsx
@@ -25,16 +25,15 @@ export default function Account({ session }: { session: AuthSession }) {
async function getProfile() {
try {
setLoading(true)
- const user = supabase.auth.user()
let { data, error } = await supabase
.from<Profile>('profiles')
.select(`username, website, avatar_url`)
- .eq('id', user.id)
+ .eq('id', session.user.id)
.single()
- if (error) {
- throw error
+ if (error || !data) {
+ throw error || new Error('Profile not found')
}
setProfile(data)
@@ -48,10 +47,9 @@ export default function Account({ session }: { session: AuthSession }) {
async function updateProfile() {
try {
setLoading(true)
- const user = supabase.auth.user()
const updates = {
- id: user.id,
+ id: session.user.id,
username,
website,
updated_at: new Date(),
diff --git a/examples/nextjs-ts-user-managment/components/ProfileList.tsx b/examples/nextjs-ts-user-managment/components/ProfileList.tsx
index c33d1122905..a49823f1f5f 100644
--- a/examples/nextjs-ts-user-managment/components/ProfileList.tsx
+++ b/examples/nextjs-ts-user-managment/components/ProfileList.tsx
@@ -1,35 +1,50 @@
import ProfileCard from '../components/ProfileCard'
import { Profile } from '../lib/constants'
import { supabase } from '../lib/supabaseClient'
-import { useState, useEffect } from 'react'
+import { useEffect, useReducer } from 'react'
-export default function ProfileList() {
- const [profiles, setProfiles] = useState<Profile[]>([])
+/**
+ * Since we want this component to update in realtime,
+ * we should use "useReducer" for sending Realtime events
+ */
- useEffect(() => {
- getPublicProfiles()
- }, [])
+type State = {
+ profiles: Profile[]
+}
+type Action = {
+ payload: Profile
+}
+type ProfileListProps = {
+ profiles: Profile[]
+}
+
+const handleDatabaseEvent = (state: State, action: Action) => {
+ const otherProfiles = state.profiles.filter((x) => x.id != action.payload.id)
+ return {
+ profiles: [action.payload, ...otherProfiles],
+ }
+}
+
+export default function ProfileList({ profiles }: ProfileListProps) {
+ const initialState: State = { profiles }
+ const [state, dispatch] = useReducer(handleDatabaseEvent, initialState)
- async function getPublicProfiles() {
- try {
- const { data, error } = await supabase
- .from<Profile>('profiles')
- .select('id, username, avatar_url, website, updated_at')
- .order('updated_at', { ascending: false })
+ useEffect(() => {
+ const subscription = supabase
+ .from('profiles')
+ .on('*', (payload) => {
+ dispatch({ payload: payload.new })
+ })
+ .subscribe()
- if (error || ! data) {
- throw error || new Error('No data')
- }
-
- setProfiles(data)
- } catch (error) {
- console.log('error', error.message)
+ return () => {
+ supabase.removeSubscription(subscription)
}
- }
+ }, [])
return (
<>
- {profiles?.map((profile) => (
+ {state.profiles?.map((profile) => (
<ProfileCard profile={profile} key={profile.id} />
))}
</>
diff --git a/examples/nextjs-ts-user-managment/pages/index.tsx b/examples/nextjs-ts-user-managment/pages/index.tsx
index 2ee2905d5aa..b9f63a48d57 100644
--- a/examples/nextjs-ts-user-managment/pages/index.tsx
+++ b/examples/nextjs-ts-user-managment/pages/index.tsx
@@ -4,9 +4,11 @@ import ProfileList from '../components/ProfileList'
import { useState, useEffect } from 'react'
import { supabase } from '../lib/supabaseClient'
import { AuthSession } from '@supabase/supabase-js'
+import { Profile } from '../lib/constants'
export default function Home() {
const [session, setSession] = useState<AuthSession | null>(null)
+ const [profiles, setProfiles] = useState<Profile[]>([])
useEffect(() => {
setSession(supabase.auth.session())
@@ -16,6 +18,27 @@ export default function Home() {
})
}, [])
+ useEffect(() => {
+ getPublicProfiles()
+ }, [])
+
+ async function getPublicProfiles() {
+ try {
+ const { data, error } = await supabase
+ .from<Profile>('profiles')
+ .select('id, username, avatar_url, website, updated_at')
+ .order('updated_at', { ascending: false })
+
+ if (error || !data) {
+ throw error || new Error('No data')
+ }
+ console.log('data', data)
+ setProfiles(data)
+ } catch (error) {
+ console.log('error', error.message)
+ }
+ }
+
return (
<div
style={{
@@ -33,7 +56,7 @@ export default function Home() {
</div>
<div className="flex column w-half" style={{ gap: 20 }}>
<h3>Public Profiles</h3>
- <ProfileList />
+ {profiles.length > 0 && <ProfileList profiles={profiles} />}
</div>
</div>
</div>