fix(dashboard): refund form input (#11993)
**What** - fix refund form input issues
This commit is contained in:
1 parent
bf882b5aff
commit
69bb775f28
1 file changed
+22
-25
+22
-25
@@ -8,7 +8,7 @@ import {
|
|||||||
Textarea,
|
Textarea,
|
||||||
toast,
|
toast,
|
||||||
} from "@medusajs/ui"
|
} from "@medusajs/ui"
|
||||||
import { useEffect } from "react"
|
import { useEffect, useMemo } from "react"
|
||||||
import { useForm } from "react-hook-form"
|
import { useForm } from "react-hook-form"
|
||||||
import { useTranslation } from "react-i18next"
|
import { useTranslation } from "react-i18next"
|
||||||
import { useNavigate, useSearchParams } from "react-router-dom"
|
import { useNavigate, useSearchParams } from "react-router-dom"
|
||||||
@@ -17,10 +17,11 @@ import { Form } from "../../../../../components/common/form"
|
|||||||
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
|
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
|
||||||
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
|
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
|
||||||
import { useRefundPayment } from "../../../../../hooks/api"
|
import { useRefundPayment } from "../../../../../hooks/api"
|
||||||
import { getCurrencySymbol } from "../../../../../lib/data/currencies"
|
import { currencies } from "../../../../../lib/data/currencies"
|
||||||
import { formatCurrency } from "../../../../../lib/format-currency"
|
import { formatCurrency } from "../../../../../lib/format-currency"
|
||||||
import { getLocaleAmount } from "../../../../../lib/money-amount-helpers"
|
import { getLocaleAmount } from "../../../../../lib/money-amount-helpers"
|
||||||
import { getPaymentsFromOrder } from "../../../order-detail/components/order-payment-section"
|
import { getPaymentsFromOrder } from "../../../order-detail/components/order-payment-section"
|
||||||
|
import { formatValue } from "react-currency-input-field"
|
||||||
|
|
||||||
type CreateRefundFormProps = {
|
type CreateRefundFormProps = {
|
||||||
order: HttpTypes.AdminOrder
|
order: HttpTypes.AdminOrder
|
||||||
@@ -28,7 +29,7 @@ type CreateRefundFormProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const CreateRefundSchema = zod.object({
|
const CreateRefundSchema = zod.object({
|
||||||
amount: zod.number(),
|
amount: zod.string().or(zod.number()),
|
||||||
refund_reason_id: zod.string().nullish(),
|
refund_reason_id: zod.string().nullish(),
|
||||||
note: zod.string().optional(),
|
note: zod.string().optional(),
|
||||||
})
|
})
|
||||||
@@ -45,6 +46,12 @@ export const CreateRefundForm = ({
|
|||||||
const payments = getPaymentsFromOrder(order)
|
const payments = getPaymentsFromOrder(order)
|
||||||
const payment = payments.find((p) => p.id === paymentId)!
|
const payment = payments.find((p) => p.id === paymentId)!
|
||||||
const paymentAmount = payment?.amount || 0
|
const paymentAmount = payment?.amount || 0
|
||||||
|
|
||||||
|
const currency = useMemo(
|
||||||
|
() => currencies[order.currency_code.toUpperCase()],
|
||||||
|
[order.currency_code]
|
||||||
|
)
|
||||||
|
|
||||||
const form = useForm<zod.infer<typeof CreateRefundSchema>>({
|
const form = useForm<zod.infer<typeof CreateRefundSchema>>({
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
amount: paymentAmount,
|
amount: paymentAmount,
|
||||||
@@ -72,7 +79,7 @@ export const CreateRefundForm = ({
|
|||||||
const handleSubmit = form.handleSubmit(async (data) => {
|
const handleSubmit = form.handleSubmit(async (data) => {
|
||||||
await mutateAsync(
|
await mutateAsync(
|
||||||
{
|
{
|
||||||
amount: data.amount,
|
amount: parseFloat(data.amount as string),
|
||||||
refund_reason_id: data.refund_reason_id,
|
refund_reason_id: data.refund_reason_id,
|
||||||
note: data.note,
|
note: data.note,
|
||||||
},
|
},
|
||||||
@@ -166,28 +173,18 @@ export const CreateRefundForm = ({
|
|||||||
<CurrencyInput
|
<CurrencyInput
|
||||||
{...field}
|
{...field}
|
||||||
min={0}
|
min={0}
|
||||||
onValueChange={(value) => {
|
placeholder={formatValue({
|
||||||
const fieldValue = value ? parseInt(value) : ""
|
value: "0",
|
||||||
|
decimalScale: currency.decimal_digits,
|
||||||
if (fieldValue && !isNaN(fieldValue)) {
|
})}
|
||||||
if (fieldValue < 0 || fieldValue > paymentAmount) {
|
decimalScale={currency.decimal_digits}
|
||||||
form.setError(`amount`, {
|
symbol={currency.symbol_native}
|
||||||
type: "manual",
|
code={currency.code}
|
||||||
message: t(
|
|
||||||
"orders.payment.createRefundWrongQuantity",
|
|
||||||
{ number: paymentAmount }
|
|
||||||
),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
form.clearErrors(`amount`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onChange(fieldValue)
|
|
||||||
}}
|
|
||||||
code={order.currency_code}
|
|
||||||
symbol={getCurrencySymbol(order.currency_code)}
|
|
||||||
value={field.value}
|
value={field.value}
|
||||||
|
onValueChange={(_value, _name, values) =>
|
||||||
|
onChange(values?.value ? values?.value : "")
|
||||||
|
}
|
||||||
|
autoFocus
|
||||||
/>
|
/>
|
||||||
</Form.Control>
|
</Form.Control>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user