Skip to content

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

chore: Update profile cards list and fix loading of profiles

Joshen Lim12a34cc61a6f
diff --git a/examples/nextjs-ts-user-management/components/Avatar.tsx b/examples/nextjs-ts-user-management/components/Avatar.tsx
index cdeefb813bb..b8042e16804 100644
--- a/examples/nextjs-ts-user-management/components/Avatar.tsx
+++ b/examples/nextjs-ts-user-management/components/Avatar.tsx
@@ -24,6 +24,6 @@ export default function Avatar({ url, size }: { url: string | null; size: number
return avatarUrl ? (
<img src={avatarUrl} className="avatar image" style={{ height: size, width: size }} />
) : (
- <div className="avatar no-image" style={{ height: size, width: size }} />
+ <div className="avatar no-image flex flex-center items-center" style={{ height: size, width: size }}>?</div>
)
}
diff --git a/examples/nextjs-ts-user-management/components/ProfileCard.tsx b/examples/nextjs-ts-user-management/components/ProfileCard.tsx
index ad560a44975..f434dc0f5ed 100644
--- a/examples/nextjs-ts-user-management/components/ProfileCard.tsx
+++ b/examples/nextjs-ts-user-management/components/ProfileCard.tsx
@@ -1,20 +1,24 @@
import { Profile } from '../lib/constants'
+import Avatar from './Avatar'
export default function ProfileCard({ profile }: { profile: Profile }) {
const lastUpdated = profile.updated_at ? new Date(profile.updated_at) : null
return (
- <div className="card">
- <p>Username: {profile.username}</p>
- <p>Website: {profile.website}</p>
- <p>Avatar: {profile.avatar_url}</p>
- <p>
- <small>
- Last updated{' '}
- {lastUpdated
- ? `${lastUpdated.toLocaleDateString()} ${lastUpdated.toLocaleTimeString()}`
- : 'Never'}
- </small>
- </p>
+ <div className="profileCard">
+ <Avatar url={profile.avatar_url} size={100} />
+ <div className="userInfo">
+ <p className="username">{profile.username}</p>
+ <a className="website" href={profile.website} target="_blank">{profile.website}</a>
+ <p>
+ <small>
+ Last updated{' '}
+ {lastUpdated
+ ? `${lastUpdated.toLocaleDateString()} ${lastUpdated.toLocaleTimeString()}`
+ : 'Never'}
+ </small>
+ </p>
+ </div>
+ <div />
</div>
)
}
diff --git a/examples/nextjs-ts-user-management/components/ProfileList.tsx b/examples/nextjs-ts-user-management/components/ProfileList.tsx
index a49823f1f5f..f314bbf4717 100644
--- a/examples/nextjs-ts-user-management/components/ProfileList.tsx
+++ b/examples/nextjs-ts-user-management/components/ProfileList.tsx
@@ -12,17 +12,25 @@ type State = {
profiles: Profile[]
}
type Action = {
- payload: Profile
+ type?: string
+ payload: any
}
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],
+ if (action.type === 'upsert') {
+ const otherProfiles = state.profiles.filter((x) => x.id != action.payload.id)
+ return {
+ profiles: [action.payload, ...otherProfiles],
+ }
+ } else if (action.type === 'set') {
+ return {
+ profiles: action.payload
+ }
}
+ return { profiles: [] }
}
export default function ProfileList({ profiles }: ProfileListProps) {
@@ -33,7 +41,7 @@ export default function ProfileList({ profiles }: ProfileListProps) {
const subscription = supabase
.from('profiles')
.on('*', (payload) => {
- dispatch({ payload: payload.new })
+ dispatch({ type: 'upsert', payload: payload.new })
})
.subscribe()
@@ -42,11 +50,21 @@ export default function ProfileList({ profiles }: ProfileListProps) {
}
}, [])
+ useEffect(() => {
+ dispatch({ type: 'set', payload: profiles })
+ }, [profiles])
+
return (
<>
- {state.profiles?.map((profile) => (
- <ProfileCard profile={profile} key={profile.id} />
- ))}
+ {state.profiles.length === 0 ? (
+ <p className="opacity-half font-light m-0">There are no public profiles created yet</p>
+ ) : (
+ <div className="profileList">
+ {state.profiles?.map((profile: any) => (
+ <ProfileCard profile={profile} key={profile.id} />
+ ))}
+ </div>
+ )}
</>
)
}
diff --git a/examples/nextjs-ts-user-management/pages/index.tsx b/examples/nextjs-ts-user-management/pages/index.tsx
index 7190f950b01..b9e1a3758df 100644
--- a/examples/nextjs-ts-user-management/pages/index.tsx
+++ b/examples/nextjs-ts-user-management/pages/index.tsx
@@ -33,7 +33,7 @@ export default function Home() {
if (error || !data) {
throw error || new Error('No data')
}
- console.log('data', data)
+ console.log('Public profiles:', data)
setProfiles(data)
} catch (error) {
console.log('error', error.message)
@@ -53,14 +53,14 @@ export default function Home() {
{!session ? (
<Auth />
) : (
- <div className="flex" style={{ gap: 30 }}>
+ <div className="flex" style={{ width: '75%', maxWidth: '850px', gap: 30 }}>
<div className="flex column w-half">
<h3>Account</h3>
<Account key={session.user.id} session={session} />
</div>
<div className="flex column w-half" style={{ gap: 20 }}>
<h3>Public Profiles</h3>
- {profiles.length > 0 && <ProfileList profiles={profiles} />}
+ <ProfileList profiles={profiles} />
</div>
</div>
)}
diff --git a/examples/nextjs-ts-user-management/styles/globals.css b/examples/nextjs-ts-user-management/styles/globals.css
index e5af8869660..8a6d777acdf 100644
--- a/examples/nextjs-ts-user-management/styles/globals.css
+++ b/examples/nextjs-ts-user-management/styles/globals.css
@@ -78,10 +78,22 @@ input[disabled] {
.flex-center {
justify-content: center;
}
+.items-center {
+ align-items: center;
+}
.text-sm {
font-size: 0.8rem;
font-weight: 300;
}
+.font-light {
+ font-weight: 300;
+}
+.opacity-half {
+ opacity: 50%;
+}
+.m-0 {
+ margin: 0;
+}
/* Button */
@@ -124,26 +136,11 @@ button.primary,
.avatar.image {
object-fit: cover;
}
-.avatar.noImage {
- background-color: rgb(230, 230, 230);
- border: 2px dotted rgb(200, 200, 200);
+.avatar.no-image {
+ background-color: #333;
+ border: 1px solid rgb(200, 200, 200);
border-radius: 5px;
}
-.avatar.noImage:after {
- font-family: var(--custom-font-family);
- content: 'No avatar photo';
- display: block;
- font-size: 16px;
- color: rgb(100, 100, 100);
-
- position: absolute;
- top: 50%;
- left: 0;
- width: 100%;
- -webkit-transform: translateY(-50%);
- transform: translateY(-50%);
- text-align: center;
-}
.footer {
position: absolute;
@@ -275,29 +272,29 @@ button.primary,
/* Profile Card */
+.profileList {
+ max-height: 420px;
+ overflow-y: auto;
+}
+
.profileCard {
border-radius: 5px;
display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
background-color: #2a2a2a;
- padding: 30px;
+ padding: 20px 20px;
+ margin-bottom: 20px;
}
-.profileCard > .avatarContainer {
- border-radius: 50%;
- overflow: hidden;
- margin-bottom: 30px;
+.profileCard:last-child {
+ margin-bottom: 0px;
}
.profileCard > .avatar {
- border-top-right-radius: 0 !important;
- border-bottom-right-radius: 0 !important;
+ border-radius: 50%;
}
.profileCard > .userInfo {
- width: 100%;
+ margin-left: 20px;
font-weight: 300;
display: flex;
flex-direction: column;
@@ -311,12 +308,12 @@ button.primary,
.profileCard > .userInfo > .username {
font-size: 1.3rem;
font-weight: 500;
- margin-bottom: 10px;
+ margin-bottom: 5px;
}
.profileCard > .userInfo > .website {
font-size: 0.9rem;
- margin-bottom: 10px;
color: var(--custom-color-brand);
+ margin-bottom: 10px;
text-decoration: none;
}