2022-06-23 08:07:52 +00:00
|
|
|
import { ManalinkTxn, DonationTxn, TipTxn } from 'common/txn'
|
2022-06-18 03:28:16 +00:00
|
|
|
import { collection, orderBy, query, where } from 'firebase/firestore'
|
2022-04-29 23:35:56 +00:00
|
|
|
import { db } from './init'
|
2022-05-03 17:25:14 +00:00
|
|
|
import { getValues, listenForValues } from './utils'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import { orderBy as _orderBy } from 'lodash'
|
2022-04-29 23:35:56 +00:00
|
|
|
|
|
|
|
const txnCollection = collection(db, 'txns')
|
|
|
|
|
|
|
|
const getCharityQuery = (charityId: string) =>
|
|
|
|
query(
|
|
|
|
txnCollection,
|
|
|
|
where('toType', '==', 'CHARITY'),
|
|
|
|
where('toId', '==', charityId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForCharityTxns(
|
|
|
|
charityId: string,
|
2022-06-18 03:28:16 +00:00
|
|
|
setTxns: (txns: DonationTxn[]) => void
|
2022-04-29 23:35:56 +00:00
|
|
|
) {
|
2022-06-18 03:28:16 +00:00
|
|
|
return listenForValues<DonationTxn>(getCharityQuery(charityId), setTxns)
|
2022-04-29 23:35:56 +00:00
|
|
|
}
|
2022-05-02 17:55:40 +00:00
|
|
|
|
|
|
|
const charitiesQuery = query(txnCollection, where('toType', '==', 'CHARITY'))
|
|
|
|
|
2022-05-03 17:25:14 +00:00
|
|
|
export function getAllCharityTxns() {
|
2022-06-18 03:28:16 +00:00
|
|
|
return getValues<DonationTxn>(charitiesQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
const getTipsQuery = (contractId: string) =>
|
|
|
|
query(
|
|
|
|
txnCollection,
|
|
|
|
where('category', '==', 'TIP'),
|
|
|
|
where('data.contractId', '==', contractId)
|
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForTipTxns(
|
|
|
|
contractId: string,
|
|
|
|
setTxns: (txns: TipTxn[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<TipTxn>(getTipsQuery(contractId), setTxns)
|
2022-05-02 17:55:40 +00:00
|
|
|
}
|
2022-06-23 08:07:52 +00:00
|
|
|
|
|
|
|
// Find all manalink Txns that are from or to this user
|
|
|
|
export function useManalinkTxns(userId: string) {
|
|
|
|
const [fromTxns, setFromTxns] = useState<ManalinkTxn[]>([])
|
|
|
|
const [toTxns, setToTxns] = useState<ManalinkTxn[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// TODO: Need to instantiate these indexes too
|
|
|
|
const fromQuery = query(
|
|
|
|
txnCollection,
|
|
|
|
where('fromId', '==', userId),
|
|
|
|
where('category', '==', 'MANALINK'),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
const toQuery = query(
|
|
|
|
txnCollection,
|
|
|
|
where('toId', '==', userId),
|
|
|
|
where('category', '==', 'MANALINK'),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
listenForValues(fromQuery, setFromTxns)
|
|
|
|
listenForValues(toQuery, setToTxns)
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return _orderBy([...fromTxns, ...toTxns], ['createdTime'], ['desc'])
|
|
|
|
}
|