import { Order } from "@medusajs/medusa" import { Controller, useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import Button from "../../../../components/fundamentals/button" import CrossIcon from "../../../../components/fundamentals/icons/cross-icon" import EditIcon from "../../../../components/fundamentals/icons/edit-icon" import { AmountInput } from "../../../../components/molecules/amount-input" import { NestedForm } from "../../../../utils/nested-form" import { formatAmountWithSymbol } from "../../../../utils/prices" type Props = { form: NestedForm order: Order initialValue?: number } export type RefundAmountFormType = { amount?: number } const RefundAmountForm = ({ form, initialValue = 0, order }: Props) => { const { t } = useTranslation() const { control, path, setValue, clearErrors, formState: { errors }, } = form const refundAmount = useWatch({ control, name: path("amount"), }) const enableEdit = () => { setValue(path("amount"), initialValue) } const disableEdit = () => { setValue(path("amount"), undefined) clearErrors(path("amount")) } return (
{refundAmount !== undefined ? ( ) : ( )}
{refundAmount !== undefined ? ( { if (value === undefined || !(value >= 0)) { return t( "refund-amount-form-the-refund-amount-must-be-at-least-0", "The refund amount must be at least 0" ) } }, }} render={({ field: { value, onChange, name } }) => { return ( ) }} /> ) : (

{formatAmountWithSymbol({ amount: initialValue, currency: order.currency_code, })}

)}
) } export default RefundAmountForm