Add sort options for /trades. Can sort by
profit or newest!
This commit is contained in:
parent
e66564cd1f
commit
b0a1da62d2
|
@ -31,12 +31,16 @@ import { sellBet } from '../lib/firebase/api-call'
|
||||||
import { ConfirmationButton } from './confirmation-button'
|
import { ConfirmationButton } from './confirmation-button'
|
||||||
import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label'
|
import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label'
|
||||||
|
|
||||||
|
type BetSort = 'newest' | 'profit'
|
||||||
|
|
||||||
export function BetsList(props: { user: User }) {
|
export function BetsList(props: { user: User }) {
|
||||||
const { user } = props
|
const { user } = props
|
||||||
const bets = useUserBets(user.id)
|
const bets = useUserBets(user.id)
|
||||||
|
|
||||||
const [contracts, setContracts] = useState<Contract[]>([])
|
const [contracts, setContracts] = useState<Contract[]>([])
|
||||||
|
|
||||||
|
const [sort, setSort] = useState<BetSort>('profit')
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadedBets = bets ? bets : []
|
const loadedBets = bets ? bets : []
|
||||||
const contractIds = _.uniq(loadedBets.map((bet) => bet.contractId))
|
const contractIds = _.uniq(loadedBets.map((bet) => bet.contractId))
|
||||||
|
@ -74,37 +78,70 @@ export function BetsList(props: { user: User }) {
|
||||||
|
|
||||||
const contractBets = _.groupBy(bets, 'contractId')
|
const contractBets = _.groupBy(bets, 'contractId')
|
||||||
|
|
||||||
const [resolved, unresolved] = _.partition(
|
const contractsCurrentValue = _.mapValues(
|
||||||
contracts,
|
contractBets,
|
||||||
(contract) => contract.isResolved
|
(bets, contractId) => {
|
||||||
)
|
return _.sumBy(bets, (bet) => {
|
||||||
|
if (bet.isSold || bet.sale) return 0
|
||||||
|
|
||||||
const currentBets = _.sumBy(unresolved, (contract) =>
|
const contract = contracts.find((c) => c.id === contractId)
|
||||||
_.sumBy(contractBets[contract.id], (bet) => {
|
return contract ? calculatePayout(contract, bet, 'MKT') : 0
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
const contractsInvestment = _.mapValues(contractBets, (bets) => {
|
||||||
|
return _.sumBy(bets, (bet) => {
|
||||||
if (bet.isSold || bet.sale) return 0
|
if (bet.isSold || bet.sale) return 0
|
||||||
return bet.amount
|
return bet.amount
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
let sortedContracts = contracts
|
||||||
|
if (sort === 'profit') {
|
||||||
|
sortedContracts = _.sortBy(
|
||||||
|
contracts,
|
||||||
|
(c) => -1 * (contractsCurrentValue[c.id] - contractsInvestment[c.id])
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const [resolved, unresolved] = _.partition(
|
||||||
|
sortedContracts,
|
||||||
|
(c) => c.isResolved
|
||||||
)
|
)
|
||||||
|
|
||||||
const currentBetsValue = _.sumBy(unresolved, (contract) =>
|
const currentInvestment = _.sumBy(
|
||||||
_.sumBy(contractBets[contract.id], (bet) => {
|
unresolved,
|
||||||
if (bet.isSold || bet.sale) return 0
|
(c) => contractsInvestment[c.id]
|
||||||
return calculatePayout(contract, bet, 'MKT')
|
)
|
||||||
})
|
|
||||||
|
const currentBetsValue = _.sumBy(
|
||||||
|
unresolved,
|
||||||
|
(c) => contractsCurrentValue[c.id]
|
||||||
)
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className="mt-6 gap-6">
|
<Col className="mt-6 gap-6">
|
||||||
<Row className="mx-4 md:mx-0 gap-8">
|
<Col className="mx-4 md:mx-0 sm:flex-row sm:justify-between gap-4">
|
||||||
<Col>
|
<Row className="gap-8">
|
||||||
<div className="text-sm text-gray-500">Currently invested</div>
|
<Col>
|
||||||
<div>{formatMoney(currentBets)}</div>
|
<div className="text-sm text-gray-500">Currently invested</div>
|
||||||
</Col>
|
<div>{formatMoney(currentInvestment)}</div>
|
||||||
<Col>
|
</Col>
|
||||||
<div className="text-sm text-gray-500">Current value</div>
|
<Col>
|
||||||
<div>{formatMoney(currentBetsValue)}</div>
|
<div className="text-sm text-gray-500">Current value</div>
|
||||||
</Col>
|
<div>{formatMoney(currentBetsValue)}</div>
|
||||||
</Row>
|
</Col>
|
||||||
|
</Row>
|
||||||
|
|
||||||
|
<select
|
||||||
|
className="select select-bordered self-start"
|
||||||
|
value={sort}
|
||||||
|
onChange={(e) => setSort(e.target.value as BetSort)}
|
||||||
|
>
|
||||||
|
<option value="profit">By profit</option>
|
||||||
|
<option value="newest">Newest</option>
|
||||||
|
</select>
|
||||||
|
</Col>
|
||||||
|
|
||||||
{[...unresolved, ...resolved].map((contract) => (
|
{[...unresolved, ...resolved].map((contract) => (
|
||||||
<MyContractBets
|
<MyContractBets
|
||||||
|
|
Loading…
Reference in New Issue
Block a user