Support user profiles on /[username]

Currently all bets are also shown
This commit is contained in:
Austin Chen 2021-12-15 17:34:36 -08:00
parent 8e119a6338
commit 9df78a4963
5 changed files with 49 additions and 30 deletions

View File

@ -10,6 +10,7 @@ import {
path, path,
} from '../lib/firebase/contracts' } from '../lib/firebase/contracts'
import { formatMoney } from '../lib/util/format' import { formatMoney } from '../lib/util/format'
import { User } from '../lib/firebase/users'
export function ContractDetails(props: { contract: Contract }) { export function ContractDetails(props: { contract: Contract }) {
const { contract } = props const { contract } = props
@ -95,9 +96,8 @@ export function ContractsGrid(props: { contracts: Contract[] }) {
) )
} }
export function ContractsList(props: {}) { export function ContractsList(props: { creator: User }) {
const creator = useUser() const { creator } = props
const [contracts, setContracts] = useState<Contract[]>([]) const [contracts, setContracts] = useState<Contract[]>([])
useEffect(() => { useEffect(() => {

View File

@ -5,6 +5,11 @@ import {
setDoc, setDoc,
getDoc, getDoc,
onSnapshot, onSnapshot,
collection,
query,
where,
limit,
getDocs,
} from 'firebase/firestore' } from 'firebase/firestore'
import { getAuth } from 'firebase/auth' import { getAuth } from 'firebase/auth'
import { ref, getStorage, uploadBytes, getDownloadURL } from 'firebase/storage' import { ref, getStorage, uploadBytes, getDownloadURL } from 'firebase/storage'
@ -35,6 +40,15 @@ export async function getUser(userId: string) {
return docSnap.data() as User return docSnap.data() as User
} }
export async function getUserByUsername(username: string) {
// Find a user whose username matches the given username, or null if no such user exists.
const userCollection = collection(db, 'users')
const q = query(userCollection, where('username', '==', username), limit(1))
const docs = await getDocs(q)
const users = docs.docs.map((doc) => doc.data() as User)
return users[0] || null
}
export async function setUser(userId: string, user: User) { export async function setUser(userId: string, user: User) {
await setDoc(doc(db, 'users', userId), user) await setDoc(doc(db, 'users', userId), user)
} }

View File

@ -1,18 +1,23 @@
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import React from 'react' import React, { useEffect, useState } from 'react'
import { Header } from '../../components/header' import { getUserByUsername, User } from '../../lib/firebase/users'
import { Col } from '../../components/layout/col' import { UserPage } from '../account'
import { Title } from '../../components/title' import Error from 'next/error'
// For now, render a placeholder page export default function UserProfile() {
export default function ContractPage() {
const router = useRouter() const router = useRouter()
const [user, setUser] = useState<User | null>(null)
const { username } = router.query as { username: string } const { username } = router.query as { username: string }
useEffect(() => {
if (username) {
getUserByUsername(username).then(setUser)
}
}, [username])
return ( const errorMessage = `Who is this "${username}" you speak of..`
<Col className="max-w-7xl mx-auto sm:px-6 lg:px-8"> return user ? (
<Header /> <UserPage user={user} />
<Title text={username} /> ) : (
</Col> <Error statusCode={404} title={errorMessage} />
) )
} }

View File

@ -64,29 +64,29 @@ function SignInCard() {
) )
} }
export default function Account() { export function UserPage(props: { user: User }) {
const user = useUser() const { user } = props
return ( return (
<div> <div>
<Header /> <Header />
<div className="max-w-4xl pt-8 pb-0 sm:pb-8 mx-auto"> <div className="max-w-4xl pt-8 pb-0 sm:pb-8 mx-auto">
{user ? ( <div>
<div> <UserCard user={user} />
<UserCard user={user} />
<Title className="px-2" text="Your markets" /> <Title className="px-2" text="Your markets" />
<ContractsList /> <ContractsList creator={user} />
<Spacer h={4} /> <Spacer h={4} />
<Title className="px-2" text="Your bets" /> <Title className="px-2" text="Your bets" />
<BetsList user={user} /> <BetsList user={user} />
</div> </div>
) : (
<SignInCard />
)}
</div> </div>
</div> </div>
) )
} }
export default function Account() {
const user = useUser()
return user ? <UserPage user={user} /> : <SignInCard />
}

View File

@ -115,7 +115,7 @@ export default function NewContract() {
<Title text="Your markets" /> <Title text="Your markets" />
<ContractsList /> <ContractsList creator={creator} />
</div> </div>
</div> </div>
) )