Pre-load charity order to prevent "jump" (#122)

This commit is contained in:
Sinclair Chen 2022-05-03 10:25:14 -07:00 committed by GitHub
parent a982b86cfe
commit abf23a1462
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 27 deletions

View File

@ -1,9 +1,6 @@
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { Txn } from '../../common/txn' import { Txn } from '../../common/txn'
import { import { listenForCharityTxns } from '../lib/firebase/txns'
listenForAllCharityTxns,
listenForCharityTxns,
} from '../lib/firebase/txns'
export const useCharityTxns = (charityId: string) => { export const useCharityTxns = (charityId: string) => {
const [txns, setTxns] = useState<Txn[]>([]) const [txns, setTxns] = useState<Txn[]>([])
@ -14,13 +11,3 @@ export const useCharityTxns = (charityId: string) => {
return txns return txns
} }
export const useAllCharityTxns = () => {
const [txns, setTxns] = useState<Txn[]>([])
useEffect(() => {
return listenForAllCharityTxns(setTxns)
}, [])
return txns
}

View File

@ -3,7 +3,7 @@ import _ from 'lodash'
import { Txn } from '../../../common/txn' import { Txn } from '../../../common/txn'
import { db } from './init' import { db } from './init'
import { listenForValues } from './utils' import { getValues, listenForValues } from './utils'
const txnCollection = collection(db, 'txns') const txnCollection = collection(db, 'txns')
@ -24,6 +24,6 @@ export function listenForCharityTxns(
const charitiesQuery = query(txnCollection, where('toType', '==', 'CHARITY')) const charitiesQuery = query(txnCollection, where('toType', '==', 'CHARITY'))
export function listenForAllCharityTxns(setTxns: (txns: Txn[]) => void) { export function getAllCharityTxns() {
return listenForValues<Txn>(charitiesQuery, setTxns) return getValues<Txn>(charitiesQuery)
} }

View File

@ -1,37 +1,49 @@
import _ from 'lodash' import _ from 'lodash'
import { useState, useMemo } from 'react' import { useState, useMemo } from 'react'
import { charities } from '../../../common/charity' import { charities, Charity as CharityType } from '../../../common/charity'
import { CharityCard } from '../../components/charity/charity-card' import { CharityCard } from '../../components/charity/charity-card'
import { Col } from '../../components/layout/col' import { Col } from '../../components/layout/col'
import { Spacer } from '../../components/layout/spacer' import { Spacer } from '../../components/layout/spacer'
import { Page } from '../../components/page' import { Page } from '../../components/page'
import { SiteLink } from '../../components/site-link' import { SiteLink } from '../../components/site-link'
import { Title } from '../../components/title' import { Title } from '../../components/title'
import { useAllCharityTxns } from '../../hooks/use-charity-txns' import { getAllCharityTxns } from '../../lib/firebase/txns'
export default function Charity() { export async function getStaticProps() {
const allCharityTxn = useAllCharityTxns() const txns = await getAllCharityTxns()
const totals = _.mapValues(_.groupBy(allCharityTxn, 'toId'), (txns) => const totals = _.mapValues(_.groupBy(txns, 'toId'), (txns) =>
_.sumBy(txns, (txn) => txn.amount) _.sumBy(txns, (txn) => txn.amount)
) )
const totalRaised = _.sum(Object.values(totals)) const totalRaised = _.sum(Object.values(totals))
// TODO: show loading state while totals are calculating
const sortedCharities = _.sortBy(charities, [ const sortedCharities = _.sortBy(charities, [
(charity) => (charity.tags?.includes('Featured') ? 0 : 1), (charity) => (charity.tags?.includes('Featured') ? 0 : 1),
(charity) => -totals[charity.id], (charity) => -totals[charity.id],
]) ])
return {
props: {
totalRaised,
charities: sortedCharities,
},
revalidate: 60,
}
}
export default function Charity(props: {
totalRaised: number
charities: CharityType[]
}) {
const { totalRaised, charities } = props
const [query, setQuery] = useState('') const [query, setQuery] = useState('')
const debouncedQuery = _.debounce(setQuery, 50) const debouncedQuery = _.debounce(setQuery, 50)
const filterCharities = useMemo( const filterCharities = useMemo(
() => () =>
sortedCharities.filter((charity) => charities.filter((charity) =>
charity.name.toLowerCase().includes(query.toLowerCase()) charity.name.toLowerCase().includes(query.toLowerCase())
), ),
[query, sortedCharities] [query]
) )
return ( return (