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 { Txn } from '../../common/txn'
import {
listenForAllCharityTxns,
listenForCharityTxns,
} from '../lib/firebase/txns'
import { listenForCharityTxns } from '../lib/firebase/txns'
export const useCharityTxns = (charityId: string) => {
const [txns, setTxns] = useState<Txn[]>([])
@ -14,13 +11,3 @@ export const useCharityTxns = (charityId: string) => {
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 { db } from './init'
import { listenForValues } from './utils'
import { getValues, listenForValues } from './utils'
const txnCollection = collection(db, 'txns')
@ -24,6 +24,6 @@ export function listenForCharityTxns(
const charitiesQuery = query(txnCollection, where('toType', '==', 'CHARITY'))
export function listenForAllCharityTxns(setTxns: (txns: Txn[]) => void) {
return listenForValues<Txn>(charitiesQuery, setTxns)
export function getAllCharityTxns() {
return getValues<Txn>(charitiesQuery)
}

View File

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