diff --git a/web/hooks/use-fold.ts b/web/hooks/use-fold.ts index 2484c213..f64694df 100644 --- a/web/hooks/use-fold.ts +++ b/web/hooks/use-fold.ts @@ -1,6 +1,6 @@ import { useEffect, useState } from 'react' import { Fold } from '../../common/fold' -import { listenForFold } from '../lib/firebase/folds' +import { listenForFold, listenForFolds } from '../lib/firebase/folds' export const useFold = (foldId: string) => { const [fold, setFold] = useState() @@ -11,3 +11,13 @@ export const useFold = (foldId: string) => { return fold } + +export const useFolds = () => { + const [folds, setFolds] = useState() + + useEffect(() => { + return listenForFolds(setFolds) + }, []) + + return folds +} diff --git a/web/lib/firebase/folds.ts b/web/lib/firebase/folds.ts index a339b340..8d2b850a 100644 --- a/web/lib/firebase/folds.ts +++ b/web/lib/firebase/folds.ts @@ -2,7 +2,7 @@ import { collection, doc, query, updateDoc, where } from 'firebase/firestore' import { Fold } from '../../../common/fold' import { Contract, contractCollection } from './contracts' import { db } from './init' -import { getValues, listenForValue } from './utils' +import { getValues, listenForValue, listenForValues } from './utils' const foldCollection = collection(db, 'folds') @@ -21,6 +21,10 @@ export async function listAllFolds() { return getValues(foldCollection) } +export function listenForFolds(setFolds: (folds: Fold[]) => void) { + return listenForValues(foldCollection, setFolds) +} + export async function getFoldBySlug(slug: string) { const q = query(foldCollection, where('slug', '==', slug)) const folds = await getValues(q) diff --git a/web/pages/folds.tsx b/web/pages/folds.tsx index dd013852..993b1b3d 100644 --- a/web/pages/folds.tsx +++ b/web/pages/folds.tsx @@ -1,7 +1,7 @@ import clsx from 'clsx' import _ from 'lodash' import { useRouter } from 'next/router' -import { useState } from 'react' +import { useEffect, useState } from 'react' import { Fold } from '../../common/fold' import { parseWordsAsTags } from '../../common/util/parse' import { ConfirmationButton } from '../components/confirmation-button' @@ -13,6 +13,7 @@ import { SiteLink } from '../components/site-link' import { TagsList } from '../components/tags-list' import { Title } from '../components/title' import { UserLink } from '../components/user-page' +import { useFolds } from '../hooks/use-fold' import { useUser } from '../hooks/use-user' import { createFold } from '../lib/firebase/api-call' import { foldPath, listAllFolds } from '../lib/firebase/folds' @@ -25,22 +26,44 @@ export async function getStaticProps() { const curators = await Promise.all( folds.map((fold) => getUser(fold.curatorId)) ) + const curatorsDict = _.fromPairs( + curators.map((curator) => [curator.id, curator]) + ) return { props: { folds, - curators, + curatorsDict, }, revalidate: 60, // regenerate after a minute } } -export default function Folds(props: { folds: Fold[]; curators: User[] }) { - const { folds, curators } = props +export default function Folds(props: { + folds: Fold[] + curatorsDict: _.Dictionary +}) { + const [curatorsDict, setCuratorsDict] = useState(props.curatorsDict) + const folds = useFolds() ?? props.folds const user = useUser() + useEffect(() => { + // Load User object for curator of new Folds. + const newFolds = folds.filter(({ curatorId }) => !curatorsDict[curatorId]) + if (newFolds.length > 0) { + Promise.all(newFolds.map(({ curatorId }) => getUser(curatorId))).then( + (newUsers) => { + const newUsersDict = _.fromPairs( + newUsers.map((user) => [user.id, user]) + ) + setCuratorsDict({ ...curatorsDict, ...newUsersDict }) + } + ) + } + }) + return ( @@ -51,7 +74,7 @@ export default function Folds(props: { folds: Fold[]; curators: User[] }) { - {folds.map((fold, index) => ( + {folds.map((fold) => ( {fold.name}
@@ -61,8 +84,8 @@ export default function Folds(props: { folds: Fold[]; curators: User[] }) {
Curated by
@@ -94,10 +117,15 @@ function CreateFoldButton() { tags: parseWordsAsTags(tags), }).then((r) => r.data || {}) - if (result.fold) await router.push(foldPath(result.fold)) - else console.log(result.status, result.message) - - setIsSubmitting(false) + if (result.fold) { + await router.push(foldPath(result.fold)).catch((e) => { + console.log(e) + setIsSubmitting(false) + }) + } else { + console.log(result.status, result.message) + setIsSubmitting(false) + } } return ( @@ -125,7 +153,7 @@ function CreateFoldButton() { -
+
) }