import React, {useEffect, useMemo, useRef} from 'react'; import {Box, CloseButton, Input, Stack, Table} from "@chakra-ui/react"; import {useAppStore} from "../appstore"; import {B2BQOMOrderListItem} from "../types/b2bqom"; import {Tooltip} from "../components/tooltip"; import {LuInfo, LuTriangleAlert} from "react-icons/lu"; import {PasteZone} from "./pasteZone"; type OrderListProps = {} function countListItemTotal(listItem: B2BQOMOrderListItem) { return (listItem.price * listItem.quantity).toFixed(2); } export function OrderList({}: OrderListProps) { const { orderList, updateInOrderList, removeFromOrderList, clearOrderList } = useAppStore(); const listRef = useRef(null); useEffect(() => { const el = listRef.current; if (!el) return; const handleCopy = (e: ClipboardEvent) => { if (!orderList.length) return; e.preventDefault(); const tsv = orderList.map(item => `${item.sku}\t${item.quantity}`).join("\n"); e.clipboardData?.setData("text/plain", tsv); }; el.addEventListener("copy", handleCopy); return () => { el.removeEventListener("copy", handleCopy); }; }, [orderList]); const subTotalAmount = useMemo(() => { const total = orderList.reduce((sum, oli) => sum + oli.quantity * oli.price, 0) || 0; return total.toFixed(2); }, [orderList]); const orderListEmpty = orderList.length === 0; return ( { orderListEmpty ? : # SKU Product In stock Price Quantity Item Total clearOrderList()}/> {orderList.map((item, index) => { return ( {index + 1} {item.sku} {item.name} {item.stock_quantity ?? 0} {item.stock_quantity != null && item.quantity > item.stock_quantity && ( )} {item.price} item.stock_quantity ? "orange.400" : undefined} bg={item.stock_quantity != null && item.quantity > item.stock_quantity ? "orange.50" : undefined} _hover={{ borderColor: item.stock_quantity != null && item.quantity > item.stock_quantity ? "orange.500" : undefined }} onBlur={(e) => { let value = Number.parseInt(e.target.value) if (isNaN(value) || value < 0) { value = 0; } e.target.value = `${Math.trunc(value)}`; updateInOrderList({ ...item, quantity: Math.trunc(value), }) }} /> {countListItemTotal(item) || 0} { removeFromOrderList(item.id); }}/> ); })} Subtotal: {subTotalAmount}   } ); }