From 0d30b03b9374a68507cf9eb9d363766a453a10b0 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 24 Oct 2022 03:49:22 -0500 Subject: [PATCH] add some more data manipulation buttons --- components/EditBox.tsx | 19 ++++++++- components/Person.tsx | 25 ++++++++++-- components/ReceiptItem.tsx | 80 +++++++++++++++++++++++++++++++------- pages/index.tsx | 37 +++++++++--------- 4 files changed, 123 insertions(+), 38 deletions(-) diff --git a/components/EditBox.tsx b/components/EditBox.tsx index 5444653..f762c27 100644 --- a/components/EditBox.tsx +++ b/components/EditBox.tsx @@ -8,11 +8,27 @@ export interface Props { } const ClickableContainer = styled.span` + display: inline-block; + padding: 4px 10px; + margin: 4px; + border: 1px solid #eee; + border-radius: 5px; + width: 120px; + &:hover { background-color: #eee; } `; +const EditingBox = styled.input` + display: inline-block; + padding: 4px 10px; + margin: 4px; + border: 1px solid #eee; + border-radius: 5px; + width: 120px; +`; + export default function EditBox({ valueAtom }: Props) { const [value, setValue] = useAtom(valueAtom); const [valueInput, setValueInput] = useState(""); @@ -32,6 +48,7 @@ export default function EditBox({ valueAtom }: Props) { e.preventDefault(); try { const n = parseFloat(valueInput); + if (isNaN(n) || !isFinite(n)) return; setValue(n); setEditing(false); } catch (e) { @@ -42,7 +59,7 @@ export default function EditBox({ valueAtom }: Props) { if (editing) { return (
- ; } -export default function Person({ personAtom }: Props) { - const [person, _] = useAtom(personAtom); - return {person.name}; +export default function Person({ personAtom, splitBetweenAtom }: Props) { + const [person] = useAtom(personAtom); + const [splitBetween, setSplitBetween] = useAtom(splitBetweenAtom); + + const removeSelf = (_) => { + setSplitBetween([...splitBetween.filter((x) => x != personAtom)]); + }; + + return ( + + {person.name} + + × + + + ); } diff --git a/components/ReceiptItem.tsx b/components/ReceiptItem.tsx index aecb5d9..624c9cb 100644 --- a/components/ReceiptItem.tsx +++ b/components/ReceiptItem.tsx @@ -1,4 +1,7 @@ -import { Atom, useAtom } from "jotai"; +import { atom, Atom, useAtom, useAtomValue } from "jotai"; +import { useState } from "react"; +import { Badge, Card, ListGroup } from "react-bootstrap"; +import { receiptAtom } from "../lib/state"; import EditBox from "./EditBox"; import Person, { IPerson } from "./Person"; @@ -13,16 +16,49 @@ export interface Props { } function SplitBetween({ splitBetweenAtom }) { - const [splitBetween, _] = useAtom(splitBetweenAtom); - return splitBetween.length > 0 ? ( + const [splitBetween, setSplitBetween] = useAtom(splitBetweenAtom); + const [input, setInput] = useState(""); + const [editing, setEditing] = useState(false); + + const startEditing = (_) => { + setInput(""); + setEditing(true); + }; + + const addPerson = (e) => { + e.preventDefault(); + setSplitBetween([...splitBetween, atom({ name: input })]); + setEditing(false); + }; + + return (
Split between ({splitBetween.length}): - {splitBetween.map((a, i) => ( - - ))} + + {splitBetween.map((a, i) => ( + + ))} + + {editing ? ( + + setEditing(false)} + onInput={(e) => setInput(e.target.value)} + /> + + ) : ( + "[+]" + )} + +
- ) : ( - <> ); } @@ -31,14 +67,28 @@ function Price({ priceAtom }) { } export default function ReceiptItem({ itemAtom }: Props) { + const [receipt, setReceipt] = useAtom(receiptAtom); const [item, _] = useAtom(itemAtom); + + const removeSelf = (_) => { + setReceipt([...receipt.filter((x) => x != itemAtom)]); + }; + return ( - <> - - {item.name} - - () - - + + + {item.name} + Item price: + + × + + + + ); } diff --git a/pages/index.tsx b/pages/index.tsx index 34bf1f9..3c0027c 100644 --- a/pages/index.tsx +++ b/pages/index.tsx @@ -51,26 +51,25 @@ const Home: NextPage = () => { -
    - {receipt.map((itemAtom, i) => { - return ( -
  • - -
  • - ); - })} -
+ {receipt.map((itemAtom, i) => { + return ; + })} -
- Total breakdown: -
    - {[...total.entries()].map(([person, value], i) => ( -
  • - {person}: {formatter.format(value)} -
  • - ))} -
-
+ {total.size > 0 && ( + <> +

Weighted Breakdown

+ +
+
    + {[...total.entries()].map(([person, value], i) => ( +
  • + {person}: {formatter.format(value)} +
  • + ))} +
+
+ + )} [source]