wisesplit/pages/index.tsx

85 lines
2 KiB
TypeScript
Raw Normal View History

2022-10-24 07:40:14 +00:00
import { atom, useAtom } from "jotai";
2022-10-24 07:10:52 +00:00
import type { NextPage } from "next";
import { useState } from "react";
2022-10-24 07:40:14 +00:00
import { Form } from "react-bootstrap";
2022-10-24 07:10:52 +00:00
import EditBox from "../components/EditBox";
import ReceiptItem, { IReceiptItem } from "../components/ReceiptItem";
import parseInput, { ParsedInputDisplay } from "../lib/parseInput";
2022-10-24 07:40:14 +00:00
import {
addLine,
receiptAtom,
receiptTotalAtom,
totalAtom,
} from "../lib/state";
2022-10-24 07:10:52 +00:00
const Home: NextPage = () => {
const [receipt, setReceipt] = useAtom(receiptAtom);
const [total] = useAtom(receiptTotalAtom);
const [input, setInput] = useState("");
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
const add = (e) => {
e.preventDefault();
2022-10-24 07:40:14 +00:00
addLine(input, setReceipt);
2022-10-24 07:10:52 +00:00
setInput("");
return false;
};
return (
<main>
2022-10-24 07:40:14 +00:00
<h1>Items</h1>
2022-10-24 07:10:52 +00:00
2022-10-24 07:40:14 +00:00
<Form onSubmit={add}>
2022-10-24 07:10:52 +00:00
<ParsedInputDisplay input={input} />
2022-10-24 07:40:14 +00:00
<Form.Control
2022-10-24 07:18:22 +00:00
autoFocus={true}
2022-10-24 07:10:52 +00:00
type="text"
placeholder="Add item..."
onInput={(e) => setInput(e.target.value)}
value={input}
2022-10-24 07:18:22 +00:00
style={{ padding: "8px", fontSize: "1.5em" }}
2022-10-24 07:10:52 +00:00
/>
2022-10-24 07:40:14 +00:00
</Form>
2022-10-24 07:10:52 +00:00
2022-10-24 07:18:22 +00:00
<div>
Receipt Total:
<EditBox valueAtom={totalAtom} />
</div>
2022-10-24 07:10:52 +00:00
<ul>
{receipt.map((itemAtom, i) => {
return (
<li key={`receiptItem-${i}`}>
<ReceiptItem itemAtom={itemAtom} />
</li>
);
})}
</ul>
<div>
Total breakdown:
<ul>
{[...total.entries()].map(([person, value], i) => (
<li key={`breakdown-${i}`}>
<b>{person}</b>: {formatter.format(value)}
</li>
))}
</ul>
</div>
2022-10-24 07:40:14 +00:00
<small>
<a href="https://github.com/iptq/wisesplit/">[source]</a>
&middot;
<a href="https://www.gnu.org/licenses/agpl-3.0.txt">[license]</a>
</small>
2022-10-24 07:10:52 +00:00
</main>
);
};
export default Home;