Allow sell row to show just a button

This is nice for the feed and on a bet's mobile interface.
This commit is contained in:
Ian Philips 2022-04-19 15:04:49 -06:00
parent d0cb24329e
commit 8905bfbfc8
2 changed files with 100 additions and 44 deletions

View File

@ -1,11 +1,14 @@
import clsx from 'clsx' import clsx from 'clsx'
import { useState } from 'react' import { useState } from 'react'
import { BetPanelSwitcher } from './bet-panel' import { BetPanelSwitcher, useSaveShares } from './bet-panel'
import { Row } from './layout/row' import { Row } from './layout/row'
import { YesNoSelector } from './yes-no-selector' import { YesNoSelector } from './yes-no-selector'
import { Binary, CPMM, DPM, FullContract } from '../../common/contract' import { Binary, CPMM, DPM, FullContract } from '../../common/contract'
import { Modal } from './layout/modal' import { Modal } from './layout/modal'
import { SellRow } from './sell-row'
import { useUser } from '../hooks/use-user'
import { useUserContractBets } from '../hooks/use-user-bets'
// Inline version of a bet panel. Opens BetPanel in a new modal. // Inline version of a bet panel. Opens BetPanel in a new modal.
export default function BetRow(props: { export default function BetRow(props: {
@ -18,11 +21,17 @@ export default function BetRow(props: {
const [betChoice, setBetChoice] = useState<'YES' | 'NO' | undefined>( const [betChoice, setBetChoice] = useState<'YES' | 'NO' | undefined>(
undefined undefined
) )
const user = useUser()
const userBets = useUserContractBets(user?.id, props.contract.id)
const { yesFloorShares, noFloorShares } = useSaveShares(
props.contract,
userBets
)
return ( return (
<> <>
<div className={className}> <div className={className}>
<Row className="items-center justify-end gap-2"> <Row className="mt-2 justify-end space-x-3">
{/* <div className={clsx('mr-2 text-gray-400', labelClassName)}> {/* <div className={clsx('mr-2 text-gray-400', labelClassName)}>
Place a trade Place a trade
</div> */} </div> */}
@ -32,6 +41,24 @@ export default function BetRow(props: {
setOpen(true) setOpen(true)
setBetChoice(choice) setBetChoice(choice)
}} }}
replaceNoButton={
yesFloorShares > noFloorShares && yesFloorShares > 0 ? (
<SellRow
contract={props.contract}
user={user}
buttonOnly={true}
/>
) : undefined
}
replaceYesButton={
noFloorShares > yesFloorShares && noFloorShares > 0 ? (
<SellRow
contract={props.contract}
user={user}
buttonOnly={true}
/>
) : undefined
}
/> />
</Row> </Row>
<Modal open={open} setOpen={setOpen}> <Modal open={open} setOpen={setOpen}>

View File

@ -10,11 +10,13 @@ import { Modal } from './layout/modal'
import { Title } from './title' import { Title } from './title'
import { SellPanel, useSaveShares } from './bet-panel' import { SellPanel, useSaveShares } from './bet-panel'
import { useUserContractBets } from '../hooks/use-user-bets' import { useUserContractBets } from '../hooks/use-user-bets'
import clsx from 'clsx'
export function SellRow(props: { export function SellRow(props: {
contract: FullContract<DPM | CPMM, Binary> contract: FullContract<DPM | CPMM, Binary>
user: User | null | undefined user: User | null | undefined
className?: string className?: string
buttonOnly?: boolean
}) { }) {
const { className } = props const { className } = props
@ -22,54 +24,81 @@ export function SellRow(props: {
const [showSellModal, setShowSellModal] = useState(false) const [showSellModal, setShowSellModal] = useState(false)
const { mechanism } = props.contract const { mechanism } = props.contract
const { yesShares, noShares } = useSaveShares(props.contract, userBets) const { yesFloorShares, noFloorShares } = useSaveShares(
props.contract,
const shares = yesShares || noShares userBets
const sharesOutcome = yesShares ? 'YES' : noShares ? 'NO' : undefined )
const shares = yesFloorShares || noFloorShares
return ( const sharesOutcome = yesFloorShares
<div> ? 'YES'
{sharesOutcome && props.user && mechanism === 'cpmm-1' && ( : noFloorShares
<Col className={className}> ? 'NO'
<Row className="items-center justify-between gap-2 "> : undefined
<div>
You have {formatWithCommas(Math.floor(shares))}{' '}
<OutcomeLabel
outcome={sharesOutcome}
contract={props.contract}
truncate={'short'}
/>{' '}
shares
</div>
if (sharesOutcome && props.user && mechanism === 'cpmm-1') {
return (
<div>
{props.buttonOnly ? (
<Col className={'items-center'}>
<button <button
className="btn btn-sm" className={clsx(
style={{ 'btn-sm w-24 gap-1',
backgroundColor: 'white', // from the yes-no-selector:
border: '2px solid', 'flex inline-flex flex-row items-center justify-center rounded-3xl border-2 p-2',
color: '#3D4451', sharesOutcome === 'NO'
}} ? 'hover:bg-primary-focus border-primary hover:border-primary-focus text-primary hover:text-white'
: 'border-red-400 text-red-500 hover:border-red-500 hover:bg-red-500 hover:text-white'
)}
onClick={() => setShowSellModal(true)} onClick={() => setShowSellModal(true)}
> >
Sell {'Sell ' + sharesOutcome}
</button> </button>
<div className={'w-24 text-center text-sm text-gray-500'}>
{'(' + shares + ' shares)'}
</div>
</Col>
) : (
<Col className={className}>
<Row className="items-center justify-between gap-2 ">
<div>
You have {formatWithCommas(shares)}{' '}
<OutcomeLabel
outcome={sharesOutcome}
contract={props.contract}
truncate={'short'}
/>{' '}
shares
</div>
{showSellModal && ( <button
<SellSharesModal className="btn btn-sm"
contract={props.contract as FullContract<CPMM, Binary>} style={{
user={props.user} backgroundColor: 'white',
userBets={userBets ?? []} border: '2px solid',
shares={shares} color: '#3D4451',
sharesOutcome={sharesOutcome} }}
setOpen={setShowSellModal} onClick={() => setShowSellModal(true)}
/> >
)} Sell
</Row> </button>
</Col> </Row>
// </div> </Col>
)} )}
</div> {showSellModal && (
) <SellSharesModal
contract={props.contract as FullContract<CPMM, Binary>}
user={props.user}
userBets={userBets ?? []}
shares={shares}
sharesOutcome={sharesOutcome}
setOpen={setShowSellModal}
/>
)}
</div>
)
}
return <div></div>
} }
function SellSharesModal(props: { function SellSharesModal(props: {