wisesplit/components/ReceiptItem.tsx

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-10-24 18:27:34 +00:00
import { Atom, useAtom } from "jotai";
import { Badge, Card } from "react-bootstrap";
2022-10-24 19:00:16 +00:00
import { moneyFormatter } from "../lib/formatter";
import { receiptAtom } from "../lib/state";
2022-10-24 07:10:52 +00:00
import EditBox from "./EditBox";
2022-10-24 19:00:16 +00:00
import NumberEditBox from "./NumberEditBox";
2022-10-24 18:27:34 +00:00
import { IPerson } from "./Person";
import SplitBetween from "./SplitBetween";
2022-10-24 07:10:52 +00:00
export interface IReceiptItem {
2022-10-24 19:00:16 +00:00
name: Atom<string>;
2022-10-24 07:10:52 +00:00
price: Atom<number>;
2022-10-24 07:40:14 +00:00
splitBetween: Atom<Atom<IPerson>[]>;
2022-10-24 07:10:52 +00:00
}
function Price({ priceAtom }) {
2022-10-24 19:00:16 +00:00
return (
<NumberEditBox valueAtom={priceAtom} formatter={moneyFormatter.format} />
);
2022-10-24 07:10:52 +00:00
}
2022-10-24 18:27:34 +00:00
export interface Props {
itemAtom: Atom<IReceiptItem>;
}
2022-10-24 07:10:52 +00:00
export default function ReceiptItem({ itemAtom }: Props) {
const [receipt, setReceipt] = useAtom(receiptAtom);
2022-10-24 07:10:52 +00:00
const [item, _] = useAtom(itemAtom);
const removeSelf = (_) => {
setReceipt([...receipt.filter((x) => x != itemAtom)]);
};
2022-10-24 07:10:52 +00:00
return (
<Card>
2022-10-24 18:27:34 +00:00
<Card.Header>
<Card.Title className="d-flex justify-content-between align-items-center">
2022-10-24 19:00:16 +00:00
<h3>
<EditBox valueAtom={item.name} validator={(s) => s} />
</h3>
2022-10-24 18:27:34 +00:00
<span>
<Price priceAtom={item.price} />
<Badge
bg="danger"
pill
onClick={removeSelf}
style={{ cursor: "pointer" }}
>
&times;
</Badge>
</span>
</Card.Title>
</Card.Header>
<Card.Body>
<SplitBetween splitBetweenAtom={item.splitBetween} />
</Card.Body>
</Card>
2022-10-24 07:10:52 +00:00
);
}