From 746534d4d316523e8da978fb32ad3541a10ec638 Mon Sep 17 00:00:00 2001 From: Ava Gaiety W Date: Sat, 14 Jun 2025 15:52:33 -0600 Subject: [PATCH] total calc --- src/BandForm.js | 53 +++++++++++++++++++++++++++++++++++++++++++------ src/index.css | 7 +++++++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/BandForm.js b/src/BandForm.js index 21cff14..54ab1dc 100644 --- a/src/BandForm.js +++ b/src/BandForm.js @@ -1,28 +1,69 @@ +import { useState, useRef } from 'react'; + +const CentsInADollar = 100; +const DefaultTotal = 0; + function costToReadable(cost) { - const centsInADollar = 100; - return cost / centsInADollar; + return cost / CentsInADollar; } function BandForm({ band }) { + const formRef = useRef(null); + const [total, setTotal] = useState(DefaultTotal); + const [formIsPending, setFormIsPending] = useState(false); + + function purchase(formData) { + console.log(formData); + setFormIsPending(true); + } + + function ticketQuantityChanged() { + const formData = new FormData(formRef.current); + const ticketQuantities = formData.getAll('ticketQuantity'); + + + setTotal( + ticketQuantities.reduce((total, quantity, index) => { + return total += (band.ticketTypes[index].cost * quantity); + }, 0) + ) + } + return ( -
+

Select Tickets

- {band.ticketTypes.map(({type, name, description, cost}) => ( + {band.ticketTypes.map(({type, name, description, cost}, index) => (
-

{name}

+

{name}

{description}

${costToReadable(cost)}

- + {/* TODO: describedby cost and description */} +

))} + +
+

Total

+

${costToReadable(total)}

+
+ +
); } diff --git a/src/index.css b/src/index.css index 83b9a12..8f6f0e6 100644 --- a/src/index.css +++ b/src/index.css @@ -84,4 +84,11 @@ h4 { flex-direction: column; gap: var(--spacing-md); } + + .total { + display: flex; + flex-direction: row; + justify-content: space-between; + font-size: var(--font-lg); + } }