import { LineItem, Order } from "@medusajs/medusa" import clsx from "clsx" import React, { Fragment, useContext } from "react" import { useTranslation } from "react-i18next" import RMAReturnReasonSubModal from "../../../domain/orders/details/rma-sub-modals/return-reasons" import Medusa from "../../../services/api" import { isLineItemCanceled } from "../../../utils/is-line-item" import { formatAmountWithSymbol } from "../../../utils/prices" import CopyToClipboard from "../../atoms/copy-to-clipboard" import Button from "../../fundamentals/button" import CheckIcon from "../../fundamentals/icons/check-icon" import MinusIcon from "../../fundamentals/icons/minus-icon" import PlusIcon from "../../fundamentals/icons/plus-icon" import { LayeredModalContext } from "../../molecules/modal/layered-modal" import Table from "../../molecules/table" type RMASelectProductTableProps = { order: Omit allItems: Omit[] toReturn: any setToReturn: (items: any) => void customReturnOptions?: any[] imagesOnReturns?: any isSwapOrClaim?: boolean } const RMASelectProductTable: React.FC = ({ order, allItems, toReturn, customReturnOptions = undefined, imagesOnReturns = false, setToReturn, isSwapOrClaim = false, }) => { const { t } = useTranslation() const { push, pop } = useContext(LayeredModalContext) const handleQuantity = (change, item) => { if ( (item.quantity - item.returned_quantity === toReturn[item.id].quantity && change > 0) || (toReturn[item.id].quantity === 1 && change < 0) ) { return } const newReturns = { ...toReturn, [item.id]: { ...toReturn[item.id], quantity: (toReturn[item.id]?.quantity || 0) + change, }, } setToReturn(newReturns) } const handleReturnToggle = (item) => { const id = item.id const newReturns = { ...toReturn } if (id in toReturn) { delete newReturns[id] } else { newReturns[id] = { images: imagesOnReturns ? [] : null, reason: null, note: "", quantity: item.quantity - item.returned_quantity, } } setToReturn(newReturns) } const handleAddImages = async (files) => { return Medusa.uploads .create(files) .then(({ data }) => data.uploads.map(({ url }) => url)) } const setReturnReason = (reason, note, files, id) => { let newReturns = {} if (imagesOnReturns && files?.length) { handleAddImages(files).then((res) => { newReturns = { ...toReturn, [id]: { ...toReturn[id], reason: reason, note: note, images: [...(toReturn[id].images || []), ...res], }, } setToReturn(newReturns) }) } else { newReturns = { ...toReturn, [id]: { ...toReturn[id], reason: reason, note: note, }, } setToReturn(newReturns) } } return ( {t("rma-select-product-table-product-details", "Product Details")} {t("rma-select-product-table-quantity", "Quantity")} {t("rma-select-product-table-refundable", "Refundable")} {allItems.map((item) => { // Only show items that have not been returned, // and aren't canceled if ( item.returned_quantity === item.quantity || isLineItemCanceled(item, order) ) { return } const checked = item.id in toReturn return (
handleReturnToggle(item)} className={`text-grey-0 border-grey-30 rounded-base mr-4 flex h-5 w-5 cursor-pointer justify-center border ${ checked && "bg-violet-60" }`} > {checked && }
handleReturnToggle(item)} type="checkbox" />
{item.title}
{item?.variant?.title && ( {item.variant.title} )} {item?.variant?.sku && ( )}
{item.id in toReturn ? (
handleQuantity(-1, item)} className="hover:bg-grey-20 mr-2 flex h-5 w-5 cursor-pointer items-center justify-center rounded" > {toReturn[item.id].quantity || ""} handleQuantity(1, item)} className={clsx( "hover:bg-grey-20 ml-2 flex h-5 w-5 cursor-pointer items-center justify-center rounded" )} >
) : ( {item.quantity - item.returned_quantity} )}
{formatAmountWithSymbol({ currency: order.currency_code, amount: item.refundable || 0, })} {order.currency_code.toUpperCase()}
{checked && !isSwapOrClaim && (
{toReturn[item.id]?.reason && ( {toReturn[item.id]?.reason.label} {toReturn[item.id]?.note || ""} {toReturn[item.id]?.images?.length > 0 && ( <> {t( "rma-select-product-table-images-witch-count", "{{count}}", { count: toReturn[item.id]?.images?.length, } )} )} )}
)}
) })}
) } const ReturnReasonScreen = ( pop, reason, note, customReturnOptions, imagesOnReturns, setReturnReason ) => { return { title: "Return Reasons", onBack: () => pop(), view: ( ), } } export default RMASelectProductTable