From e8cefdabbe4b5c3f585d0d3edfcfab7cd5793506 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Mon, 6 Dec 2021 12:03:12 -0800 Subject: [PATCH] Submit new bids for React sim --- web/pages/simulator/index.tsx | 105 +++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/web/pages/simulator/index.tsx b/web/pages/simulator/index.tsx index a94d9626..eda62127 100644 --- a/web/pages/simulator/index.tsx +++ b/web/pages/simulator/index.tsx @@ -11,10 +11,11 @@ import { } from 'chart.js' import { ChartData } from 'chart.js' import { Line } from 'react-chartjs-2' -import { bids } from './sample-bids' +import { bids as sampleBids } from './sample-bids' import { Entry, makeEntries } from './entries' // Auto import doesn't work for some reason... +// So we manually register ChartJS components instead: Chart.register( CategoryScale, LinearScale, @@ -110,14 +111,90 @@ function toRowEnd(entry: Entry) { } } +function newBidTable( + steps: number, + newBid: number, + setNewBid: (newBid: number) => void, + submitBid: () => void +) { + return ( + + + + + + + + + + + + + + + + + + + {/* */} + + + +
Order #TypeBidWeightProbabilityPayoutReturn
{steps + 1} +
+ YES +
+
+
+ NO +
+
+ setNewBid(parseInt(e.target.value) || 0)} + onKeyUp={(e) => { + if (e.key === 'Enter') { + submitBid() + } + }} + onFocus={(e) => e.target.select()} + /> + + +
+ ) +} + // Show a hello world React page export default function Simulator() { const [steps, setSteps] = useState(10) + const [bids, setBids] = useState(sampleBids) - const entries = makeEntries(bids.slice(0, steps)) + const entries = useMemo( + () => makeEntries(bids.slice(0, steps)), + [bids, steps] + ) const probs = useMemo(() => entries.map((entry) => entry.prob), [entries]) + // Set up chart const [chartData, setChartData] = useState({ datasets: [] } as ChartData) useEffect(() => { @@ -133,6 +210,26 @@ export default function Simulator() { }) }, [steps]) + // Prepare for new bids + const [newBid, setNewBid] = useState(0) + const [newBidType, setNewBidType] = useState('YES') + + function makeBid(type: string, bid: number) { + return { + yesBid: type == 'YES' ? bid : 0, + noBid: type == 'YES' ? 0 : bid, + } + } + + function submitBid() { + if (newBid <= 0) return + const bid = makeBid(newBidType, newBid) + bids.splice(steps, 0, bid) + setBids(bids) + setSteps(steps + 1) + setNewBid(0) + } + return (
@@ -152,6 +249,10 @@ export default function Simulator() { onChange={(e) => setSteps(parseInt(e.target.value))} /> + {/* New bid table */} + {newBidTable(steps, newBid, setNewBid, submitBid)} + + {/* History of bids */}