Update user profile UI to use new denormalized fields
This commit is contained in:
parent
7c09b20ea0
commit
61c13d171a
|
@ -1,12 +1,8 @@
|
||||||
import { useEffect, useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { Dictionary, keyBy, uniq } from 'lodash'
|
|
||||||
|
|
||||||
import { Comment } from 'common/comment'
|
import { Comment } from 'common/comment'
|
||||||
import { Contract } from 'common/contract'
|
import { groupConsecutive } from 'common/util/array'
|
||||||
import { filterDefined, groupConsecutive } from 'common/util/array'
|
|
||||||
import { contractPath } from 'web/lib/firebase/contracts'
|
|
||||||
import { getUsersComments } from 'web/lib/firebase/comments'
|
import { getUsersComments } from 'web/lib/firebase/comments'
|
||||||
import { getContractFromId } from 'web/lib/firebase/contracts'
|
|
||||||
import { SiteLink } from './site-link'
|
import { SiteLink } from './site-link'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { Avatar } from './avatar'
|
import { Avatar } from './avatar'
|
||||||
|
@ -20,12 +16,20 @@ import { LoadingIndicator } from './loading-indicator'
|
||||||
|
|
||||||
const COMMENTS_PER_PAGE = 50
|
const COMMENTS_PER_PAGE = 50
|
||||||
|
|
||||||
type ContractComment = Comment & { contractId: string }
|
type ContractComment = Comment & {
|
||||||
|
contractId: string
|
||||||
|
contractSlug: string
|
||||||
|
contractQuestion: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function contractPath(slug: string) {
|
||||||
|
// in honor of austin, who insists that contract URLs are prefixed with a username
|
||||||
|
return `/Austin/${slug}`
|
||||||
|
}
|
||||||
|
|
||||||
export function UserCommentsList(props: { user: User }) {
|
export function UserCommentsList(props: { user: User }) {
|
||||||
const { user } = props
|
const { user } = props
|
||||||
const [comments, setComments] = useState<ContractComment[] | undefined>()
|
const [comments, setComments] = useState<ContractComment[] | undefined>()
|
||||||
const [contracts, setContracts] = useState<Dictionary<Contract> | undefined>()
|
|
||||||
const [page, setPage] = useState(0)
|
const [page, setPage] = useState(0)
|
||||||
const start = page * COMMENTS_PER_PAGE
|
const start = page * COMMENTS_PER_PAGE
|
||||||
const end = start + COMMENTS_PER_PAGE
|
const end = start + COMMENTS_PER_PAGE
|
||||||
|
@ -37,34 +41,23 @@ export function UserCommentsList(props: { user: User }) {
|
||||||
})
|
})
|
||||||
}, [user.id])
|
}, [user.id])
|
||||||
|
|
||||||
useEffect(() => {
|
if (comments == null) {
|
||||||
if (comments) {
|
|
||||||
const contractIds = uniq(comments.map((c) => c.contractId))
|
|
||||||
Promise.all(contractIds.map(getContractFromId)).then((contracts) => {
|
|
||||||
setContracts(keyBy(filterDefined(contracts), 'id'))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}, [comments])
|
|
||||||
|
|
||||||
if (comments == null || contracts == null) {
|
|
||||||
return <LoadingIndicator />
|
return <LoadingIndicator />
|
||||||
}
|
}
|
||||||
|
|
||||||
const pageComments = groupConsecutive(
|
const pageComments = groupConsecutive(comments.slice(start, end), (c) => {
|
||||||
comments.slice(start, end),
|
return { question: c.contractQuestion, slug: c.contractSlug }
|
||||||
(c) => c.contractId
|
})
|
||||||
)
|
|
||||||
return (
|
return (
|
||||||
<Col className={'bg-white'}>
|
<Col className={'bg-white'}>
|
||||||
{pageComments.map(({ key, items }, i) => {
|
{pageComments.map(({ key, items }, i) => {
|
||||||
const contract = contracts[key]
|
|
||||||
return (
|
return (
|
||||||
<div key={start + i} className="border-b p-5">
|
<div key={start + i} className="border-b p-5">
|
||||||
<SiteLink
|
<SiteLink
|
||||||
className="mb-2 block pb-2 font-medium text-indigo-700"
|
className="mb-2 block pb-2 font-medium text-indigo-700"
|
||||||
href={contractPath(contract)}
|
href={contractPath(key.slug)}
|
||||||
>
|
>
|
||||||
{contract.question}
|
{key.question}
|
||||||
</SiteLink>
|
</SiteLink>
|
||||||
<Col className="gap-6">
|
<Col className="gap-6">
|
||||||
{items.map((comment) => (
|
{items.map((comment) => (
|
||||||
|
|
Loading…
Reference in New Issue
Block a user