import { useAtom } from "jotai"; import type { NextPage } from "next"; import { useEffect, useRef } from "react"; import { SyntheticEvent, useState } from "react"; import { Form } from "react-bootstrap"; import NumberEditBox from "../components/NumberEditBox"; import ReceiptItem from "../components/ReceiptItem"; import { moneyFormatter } from "../lib/formatter"; import { ParsedInputDisplay } from "../lib/parseInput"; import { addLine, receiptAtom, receiptTotalAtom, totalAtom, receiptAtomToJSON, } from "../lib/state"; const Home: NextPage = () => { const [receipt, setReceipt] = useAtom(receiptAtom); const [input, setInput] = useState(""); const [total] = useAtom(totalAtom); const [calculated] = useAtom(receiptTotalAtom); const [receiptJson] = useAtom(receiptAtomToJSON); const isAddCalled = useRef(false); const formatter = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", }); const add = async (e: SyntheticEvent) => { e.preventDefault(); isAddCalled.current = true; addLine(input, receipt, setReceipt); setInput(""); return false; }; const receiptJSONString = JSON.stringify(receiptJson); useEffect(() => { const updateDb = async () => { const response = await fetch("/api/updateReceipt", { method: "POST", body: JSON.stringify({ receipts: receiptJson }), }); }; if (isAddCalled.current) { updateDb(); } }, [receiptJSONString, receiptJson]); return (

Items

setInput(e.currentTarget.value)} value={input} style={{ padding: "8px 16px", fontSize: "1.5em" }} /> {receipt.map((itemAtom, i) => { return ; })}
Receipt Total:

{calculated.totalMap.size > 0 && ( <>

Weighted Breakdown

    {[...calculated.totalMap.entries()].map(([person, value], i) => (
  • {person}: {formatter.format(value)}
  • ))}
)}
); }; export default Home;