Total up granted amount

This commit is contained in:
Austin Chen 2022-07-29 16:08:43 -07:00
parent 0a75c1b637
commit 82293a196c
2 changed files with 14 additions and 16 deletions

View File

@ -3,15 +3,11 @@ import Image from 'next/image'
import { Grantee } from '.' import { Grantee } from '.'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { sumBy } from 'lodash' import { formatLargeNumber } from 'common/util/format'
import { formatLargeNumber, formatMoney } from 'common/util/format'
export default function GranteeCard(props: { grantee: Grantee }) { export default function GranteeCard(props: { grantee: Grantee }) {
const { grantee } = props const { grantee } = props
const { slug, photo, preview } = grantee const { slug, photo, preview, totalReceived } = grantee
// sumBy grantee.grantsReceived amount
const raised = sumBy(grantee.grantsReceived, (grant) => grant.amount)
return ( return (
<Link href={`/grants/${slug}`} passHref> <Link href={`/grants/${slug}`} passHref>
@ -31,11 +27,11 @@ export default function GranteeCard(props: { grantee: Grantee }) {
</div> </div>
<div className="card-body"> <div className="card-body">
<div className="line-clamp-4 text-sm">{preview}</div> <div className="line-clamp-4 text-sm">{preview}</div>
{raised > 0 && ( {totalReceived > 0 && (
<Row className="mt-4 flex-1 items-end justify-center gap-6 text-gray-900"> <Row className="mt-4 flex-1 items-end justify-center gap-6 text-gray-900">
<Row className="items-baseline gap-1"> <Row className="items-baseline gap-1">
<span className="text-3xl font-semibold"> <span className="text-3xl font-semibold">
{formatUsd(raised)} {formatUsd(totalReceived)}
</span> </span>
raised raised
</Row> </Row>

View File

@ -1,5 +1,5 @@
import { searchInAny } from 'common/util/parse' import { searchInAny } from 'common/util/parse'
import { debounce } from 'lodash' import { debounce, sortBy } from 'lodash'
import { useMemo, useState } from 'react' import { useMemo, useState } from 'react'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Page } from 'web/components/page' import { Page } from 'web/components/page'
@ -16,6 +16,7 @@ export type Grantee = {
preview: string preview: string
description: string description: string
grantsReceived: Grant[] grantsReceived: Grant[]
totalReceived: number
} }
export type Grant = { export type Grant = {
@ -59,10 +60,12 @@ function grantsToGrantees(grantsList: Grant[]) {
preview: grant.description, preview: grant.description,
description: grant.description, description: grant.description,
grantsReceived: [], grantsReceived: [],
totalReceived: 0,
} }
grantees.push(grantee) grantees.push(grantee)
} }
grantee.grantsReceived.push(grant) grantee.grantsReceived.push(grant)
grantee.totalReceived += grant.amount
} }
console.log(grantees) console.log(grantees)
return grantees return grantees
@ -72,13 +75,12 @@ export default function Grants() {
const [query, setQuery] = useState('') const [query, setQuery] = useState('')
const debouncedQuery = debounce(setQuery, 50) const debouncedQuery = debounce(setQuery, 50)
const filteredGrantees = useMemo( const filteredGrantees = useMemo(() => {
() => const g = grantees.filter((grantee) =>
grantees.filter((grantee) => searchInAny(query, grantee.name, grantee.description)
searchInAny(query, grantee.name, grantee.description) )
), return sortBy(g, 'totalReceived').reverse()
[query] }, [query])
)
return ( return (
<Page> <Page>