From c6ab33bc4d6c227a079559adb3e5bc5366fdcf34 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Tue, 7 Dec 2021 10:34:18 -0800 Subject: [PATCH] Code cleanup: move hooks to where they're used --- web/pages/simulator/index.tsx | 89 +++++++++++++++-------------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/web/pages/simulator/index.tsx b/web/pages/simulator/index.tsx index c1b5fefd..48bdeee7 100644 --- a/web/pages/simulator/index.tsx +++ b/web/pages/simulator/index.tsx @@ -113,13 +113,44 @@ function toRowEnd(entry: Entry | null) { function newBidTable( steps: number, - newBid: number, - nextEntryElement: JSX.Element, - newBidType: String, - toggleBidType: () => void, - setNewBid: (newBid: number) => void, - submitBid: () => void + bids: any[], + setSteps: (steps: number) => void, + setBids: (bids: any[]) => void ) { + // 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) + } + + function toggleBidType() { + setNewBidType(newBidType === 'YES' ? 'NO' : 'YES') + } + + const nextEntry = useMemo(() => { + if (newBid) { + const nextBid = makeBid(newBidType, newBid) + const fakeBids = [...bids.slice(0, steps), nextBid] + const entries = makeEntries(fakeBids) + return entries[entries.length - 1] + } + return null + }, [newBid, newBidType, steps]) + return ( @@ -173,7 +204,7 @@ function newBidTable( onFocus={(e) => e.target.select()} /> - {nextEntryElement} + {toRowEnd(nextEntry)}