985cdd2537
* Loan backend: Add loanAmount field to Bet, manage loans up to max loan amount per market -- buy, sell, and resolve. * Loan frontend: show your loan amount in bet panel, answer bet panel * Resolve emails include full payout not subtracting loan * Exclude sold bets from current loan amount * Handle bets table for loans. Sell dialog explains how you will repay your loan. * Floor remaining balance * Fix layout of create answer bet info * Clean up Sell popup UI * Fix bug where listen query was not updating data. * Reword loan copy * Adjust bet panel width * Fix loan calc on front end * Add comment for includeMetadataChanges. Co-authored-by: Austin Chen <akrolsmir@gmail.com>
47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
import { db } from './init'
|
|
import {
|
|
getDoc,
|
|
getDocs,
|
|
onSnapshot,
|
|
Query,
|
|
DocumentReference,
|
|
} from 'firebase/firestore'
|
|
|
|
export const getValue = async <T>(doc: DocumentReference) => {
|
|
const snap = await getDoc(doc)
|
|
return snap.exists() ? (snap.data() as T) : null
|
|
}
|
|
|
|
export const getValues = async <T>(query: Query) => {
|
|
const snap = await getDocs(query)
|
|
return snap.docs.map((doc) => doc.data() as T)
|
|
}
|
|
|
|
export function listenForValue<T>(
|
|
docRef: DocumentReference,
|
|
setValue: (value: T | null) => void
|
|
) {
|
|
// Exclude cached snapshots so we only trigger on fresh data.
|
|
// includeMetadataChanges ensures listener is called even when server data is the same as cached data.
|
|
return onSnapshot(docRef, { includeMetadataChanges: true }, (snapshot) => {
|
|
if (snapshot.metadata.fromCache) return
|
|
|
|
const value = snapshot.exists() ? (snapshot.data() as T) : null
|
|
setValue(value)
|
|
})
|
|
}
|
|
|
|
export function listenForValues<T>(
|
|
query: Query,
|
|
setValues: (values: T[]) => void
|
|
) {
|
|
// Exclude cached snapshots so we only trigger on fresh data.
|
|
// includeMetadataChanges ensures listener is called even when server data is the same as cached data.
|
|
return onSnapshot(query, { includeMetadataChanges: true }, (snapshot) => {
|
|
if (snapshot.metadata.fromCache) return
|
|
|
|
const values = snapshot.docs.map((doc) => doc.data() as T)
|
|
setValues(values)
|
|
})
|
|
}
|