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";
|
2022-10-24 08:49:22 +00:00
|
|
|
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) {
|
2022-10-24 08:49:22 +00:00
|
|
|
const [receipt, setReceipt] = useAtom(receiptAtom);
|
2022-10-24 07:10:52 +00:00
|
|
|
const [item, _] = useAtom(itemAtom);
|
2022-10-24 08:49:22 +00:00
|
|
|
|
|
|
|
const removeSelf = (_) => {
|
|
|
|
setReceipt([...receipt.filter((x) => x != itemAtom)]);
|
|
|
|
};
|
|
|
|
|
2022-10-24 07:10:52 +00:00
|
|
|
return (
|
2022-10-24 08:49:22 +00:00
|
|
|
<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" }}
|
|
|
|
>
|
|
|
|
×
|
|
|
|
</Badge>
|
|
|
|
</span>
|
|
|
|
</Card.Title>
|
|
|
|
</Card.Header>
|
|
|
|
|
2022-10-24 08:49:22 +00:00
|
|
|
<Card.Body>
|
|
|
|
<SplitBetween splitBetweenAtom={item.splitBetween} />
|
|
|
|
</Card.Body>
|
|
|
|
</Card>
|
2022-10-24 07:10:52 +00:00
|
|
|
);
|
|
|
|
}
|