Submit new bids for React sim

This commit is contained in:
Austin Chen 2021-12-06 12:03:12 -08:00
parent acd523b61b
commit e8cefdabbe

View File

@ -11,10 +11,11 @@ import {
} from 'chart.js' } from 'chart.js'
import { ChartData } from 'chart.js' import { ChartData } from 'chart.js'
import { Line } from 'react-chartjs-2' import { Line } from 'react-chartjs-2'
import { bids } from './sample-bids' import { bids as sampleBids } from './sample-bids'
import { Entry, makeEntries } from './entries' import { Entry, makeEntries } from './entries'
// Auto import doesn't work for some reason... // Auto import doesn't work for some reason...
// So we manually register ChartJS components instead:
Chart.register( Chart.register(
CategoryScale, CategoryScale,
LinearScale, LinearScale,
@ -110,14 +111,90 @@ function toRowEnd(entry: Entry) {
} }
} }
function newBidTable(
steps: number,
newBid: number,
setNewBid: (newBid: number) => void,
submitBid: () => void
) {
return (
<table className="table table-compact my-8 w-full">
<thead>
<tr>
<th>Order #</th>
<th>Type</th>
<th>Bid</th>
<th>Weight</th>
<th>Probability</th>
<th>Payout</th>
<th>Return</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>{steps + 1}</th>
<td>
<div
className={
`badge clickable ` + ('YES' ? 'badge-success' : 'badge-ghost')
}
>
YES
</div>
<br />
<div
className={
`badge clickable ` + ('NO' ? 'badge-error' : 'badge-ghost')
}
>
NO
</div>
</td>
<td>
<input
type="number"
placeholder="0"
className="input input-bordered"
value={newBid}
onChange={(e) => setNewBid(parseInt(e.target.value) || 0)}
onKeyUp={(e) => {
if (e.key === 'Enter') {
submitBid()
}
}}
onFocus={(e) => e.target.select()}
/>
</td>
{/* <EntryRow :entry="nextEntry" /> */}
<td>
<button
className="btn btn-primary"
onClick={() => submitBid()}
disabled={newBid <= 0}
>
Submit
</button>
</td>
</tr>
</tbody>
</table>
)
}
// Show a hello world React page // Show a hello world React page
export default function Simulator() { export default function Simulator() {
const [steps, setSteps] = useState(10) 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]) const probs = useMemo(() => entries.map((entry) => entry.prob), [entries])
// Set up chart
const [chartData, setChartData] = useState({ datasets: [] } as ChartData) const [chartData, setChartData] = useState({ datasets: [] } as ChartData)
useEffect(() => { useEffect(() => {
@ -133,6 +210,26 @@ export default function Simulator() {
}) })
}, [steps]) }, [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 ( return (
<div className="overflow-x-auto px-12 mt-8 text-center"> <div className="overflow-x-auto px-12 mt-8 text-center">
<div className="grid grid-cols-1 xl:grid-cols-2 gap-4"> <div className="grid grid-cols-1 xl:grid-cols-2 gap-4">
@ -152,6 +249,10 @@ export default function Simulator() {
onChange={(e) => setSteps(parseInt(e.target.value))} onChange={(e) => setSteps(parseInt(e.target.value))}
/> />
{/* New bid table */}
{newBidTable(steps, newBid, setNewBid, submitBid)}
{/* History of bids */}
<div className="overflow-x-auto"> <div className="overflow-x-auto">
<table className="table w-full"> <table className="table w-full">
<thead> <thead>