wisesplit/components/ReceiptItem.tsx

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-10-25 18:26:48 +00:00
import { PrimitiveAtom, useAtom } from "jotai";
2022-10-24 18:27:34 +00:00
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
2022-10-25 18:26:48 +00:00
export type Receipt = PrimitiveAtom<IReceiptItem>[];
2022-10-24 07:10:52 +00:00
2022-10-25 18:26:48 +00:00
export interface IReceiptItem {
name: PrimitiveAtom<string>;
price: PrimitiveAtom<number>;
splitBetween: PrimitiveAtom<PrimitiveAtom<IPerson>[]>;
2022-10-24 07:10:52 +00:00
}
2022-10-24 18:27:34 +00:00
export interface Props {
2022-10-25 18:26:48 +00:00
itemAtom: PrimitiveAtom<IReceiptItem>;
2022-10-24 18:27:34 +00:00
}
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);
2022-10-25 18:26:48 +00:00
const removeSelf = (_: any) => {
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>
2022-10-25 18:26:48 +00:00
<NumberEditBox
valueAtom={item.price}
formatter={moneyFormatter.format}
/>
2022-10-24 18:27:34 +00:00
<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
);
}