import { createColumnHelper } from "@tanstack/react-table" import { useCallback, useMemo } from "react" import { useTranslation } from "react-i18next" import CopyToClipboard from "../../../../components/atoms/copy-to-clipboard" import Thumbnail from "../../../../components/atoms/thumbnail" import Tooltip from "../../../../components/atoms/tooltip" import Button from "../../../../components/fundamentals/button" import TrashIcon from "../../../../components/fundamentals/icons/trash-icon" import { NestedForm } from "../../../../utils/nested-form" import { formatAmountWithSymbol } from "../../../../utils/prices" import TableQuantitySelector from "../table-quantity-selector" import { AdditionalItemObject, ItemsToSendFormType } from "./index" const columnHelper = createColumnHelper() type AdditionalItemsColumnProps = { form: NestedForm orderCurrency: string removeItem: (index: number) => void } export const useAdditionalItemsColumns = ({ form, orderCurrency, removeItem, }: AdditionalItemsColumnProps) => { const { t } = useTranslation() const { control, setValue, getValues, path } = form const updateQuantity = useCallback( (index: number, change: number) => { const pathToQuantity = path(`items.${index}.quantity`) const selectedQuantity = getValues(pathToQuantity) setValue(pathToQuantity, selectedQuantity + change) }, [getValues, path, setValue] ) const columns = useMemo(() => { return [ columnHelper.display({ id: "product_display", header: t("items-to-send-form-product", "Product"), cell: ({ row: { original: { thumbnail, product_title, variant_title, sku }, }, }) => { return (

{product_title}

{variant_title && (

({variant_title})

)}
{sku && ( )}
) }, }), columnHelper.display({ id: "quantity", header: () => (

{t("items-to-send-form-quantity", "Quantity")}

), maxSize: 50, cell: ({ row: { index, original: { in_stock }, }, }) => { return ( ) }, }), columnHelper.accessor("price", { maxSize: 50, header: () => (

{t("items-to-send-form-price", "Price")}

), cell: ({ getValue, row: { original: { original_price }, }, }) => { const price = getValue() return (
{original_price !== price && (

{formatAmountWithSymbol({ amount: original_price, currency: orderCurrency, })}

)}

{formatAmountWithSymbol({ amount: price, currency: orderCurrency, })}

) }, }), columnHelper.display({ id: "order_currency", maxSize: 20, cell: () => { return (

{orderCurrency.toUpperCase()}

) }, }), columnHelper.display({ id: "remove", maxSize: 28, cell: ({ row: { index } }) => { return (
) }, }), ] }, [control, orderCurrency, path, removeItem, updateQuantity]) return columns }