Fix undefined mostRecentDonor

This commit is contained in:
Austin Chen 2022-07-19 13:59:24 -07:00
parent a992773a65
commit 963f1c156a

View File

@ -6,6 +6,7 @@ import {
sortBy, sortBy,
debounce, debounce,
uniqBy, uniqBy,
last,
} from 'lodash' } from 'lodash'
import { useState, useMemo } from 'react' import { useState, useMemo } from 'react'
import { charities, Charity as CharityType } from 'common/charity' import { charities, Charity as CharityType } from 'common/charity'
@ -36,7 +37,8 @@ export async function getStaticProps() {
]) ])
const matches = quadraticMatches(txns, totalRaised) const matches = quadraticMatches(txns, totalRaised)
const numDonors = uniqBy(txns, (txn) => txn.fromId).length const numDonors = uniqBy(txns, (txn) => txn.fromId).length
const mostRecentDonor = await getUser(txns[txns.length - 1].fromId) const donorId = last(txns)?.fromId
const mostRecentDonor = donorId ? await getUser(donorId) : undefined
return { return {
props: { props: {
@ -89,7 +91,7 @@ export default function Charity(props: {
matches: { [charityId: string]: number } matches: { [charityId: string]: number }
txns: Txn[] txns: Txn[]
numDonors: number numDonors: number
mostRecentDonor: User mostRecentDonor?: User
}) { }) {
const { totalRaised, charities, matches, numDonors, mostRecentDonor } = props const { totalRaised, charities, matches, numDonors, mostRecentDonor } = props
@ -140,8 +142,8 @@ export default function Charity(props: {
}, },
{ {
name: 'Most recent donor', name: 'Most recent donor',
stat: mostRecentDonor.name ?? 'Nobody', stat: mostRecentDonor?.name ?? 'Nobody',
url: `/${mostRecentDonor.username}`, url: `/${mostRecentDonor?.username}`,
}, },
]} ]}
/> />