Skip to content

Commit eb4b624

Browse files
committed
fix: refund forms and format currency util
1 parent 474e97c commit eb4b624

3 files changed

Lines changed: 86 additions & 36 deletions

File tree

packages/admin/dashboard/src/lib/format-currency.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const formatCurrency = (amount: number, currency: string) => {
2-
return new Intl.NumberFormat("en-US", {
2+
return new Intl.NumberFormat(undefined, {
33
style: "currency",
44
currency,
55
signDisplay: "auto",

packages/admin/dashboard/src/routes/orders/order-balance-settlement/components/order-balance-settlement-form/order-balance-settlement-form.tsx

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
clx,
66
CurrencyInput,
77
Divider,
8-
Input,
98
Label,
109
RadioGroup,
1110
Select,
@@ -15,8 +14,10 @@ import {
1514
import { useEffect, useMemo, useState } from "react"
1615
import { formatValue } from "react-currency-input-field"
1716
import { useForm } from "react-hook-form"
17+
import { useSearchParams } from "react-router-dom"
1818
import { useTranslation } from "react-i18next"
1919
import * as zod from "zod"
20+
2021
import { Form } from "../../../../../components/common/form"
2122
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
2223
import { KeyboundForm } from "../../../../../components/utilities/keybound-form"
@@ -33,13 +34,19 @@ const OrderBalanceSettlementSchema = zod.object({
3334
settlement_type: zod.enum(["credit_line", "refund"]),
3435
refund: zod
3536
.object({
36-
amount: zod.string().or(zod.number()).optional(),
37+
amount: zod.object({
38+
value: zod.string().or(zod.number()).optional(),
39+
float: zod.number().or(zod.null()),
40+
}),
3741
note: zod.string().optional(),
3842
})
3943
.optional(),
4044
credit_line: zod
4145
.object({
42-
amount: zod.string().or(zod.number()).optional(),
46+
amount: zod.object({
47+
value: zod.string().or(zod.number()).optional(),
48+
float: zod.number().or(zod.null()),
49+
}),
4350
note: zod.string().optional(),
4451
})
4552
.optional(),
@@ -51,19 +58,30 @@ export const OrderBalanceSettlementForm = ({
5158
order: AdminOrder
5259
}) => {
5360
const { t } = useTranslation()
61+
const [searchParams] = useSearchParams()
5462
const { handleSuccess } = useRouteModal()
55-
const [activePayment, setActivePayment] = useState<AdminPayment | null>(null)
63+
const paymentId = searchParams.get("paymentId")
5664
const payments = getPaymentsFromOrder(order)
5765
const pendingDifference = order.summary.pending_difference * -1
5866

67+
const [activePayment, setActivePayment] = useState<AdminPayment | null>(
68+
paymentId ? payments.find((p) => p.id === paymentId) || null : null
69+
)
70+
5971
const form = useForm<zod.infer<typeof OrderBalanceSettlementSchema>>({
6072
defaultValues: {
6173
settlement_type: "refund",
6274
refund: {
63-
amount: 0,
75+
amount: {
76+
value: "",
77+
float: null,
78+
},
6479
},
6580
credit_line: {
66-
amount: 0,
81+
amount: {
82+
value: "",
83+
float: null,
84+
},
6785
},
6886
},
6987
resolver: zodResolver(OrderBalanceSettlementSchema),
@@ -79,9 +97,14 @@ export const OrderBalanceSettlementForm = ({
7997

8098
const handleSubmit = form.handleSubmit(async (data) => {
8199
if (data.settlement_type === "credit_line") {
100+
if (data.credit_line?.amount.float === null) {
101+
return
102+
}
82103
await createCreditLine(
83104
{
84-
amount: parseFloat(data.credit_line!.amount! as string) * -1,
105+
amount: data.credit_line!.amount.float! * -1,
106+
reference: "refund",
107+
reference_id: order.id,
85108
},
86109
{
87110
onSuccess: () => {
@@ -97,17 +120,20 @@ export const OrderBalanceSettlementForm = ({
97120
}
98121

99122
if (data.settlement_type === "refund") {
123+
if (data.refund?.amount.float === null) {
124+
return
125+
}
100126
await createRefund(
101127
{
102-
amount: parseFloat(data.refund!.amount! as string),
128+
amount: data.refund!.amount!.float!,
103129
note: data.refund!.note,
104130
},
105131
{
106132
onSuccess: () => {
107133
toast.success(
108134
t("orders.payment.refundPaymentSuccess", {
109135
amount: formatCurrency(
110-
parseFloat(data.refund!.amount! as string),
136+
data.refund!.amount!.float!,
111137
order.currency_code!
112138
),
113139
})
@@ -131,18 +157,23 @@ export const OrderBalanceSettlementForm = ({
131157
useEffect(() => {
132158
form.clearErrors()
133159

134-
const minimum = activePayment?.amount
160+
const _minimum = activePayment?.amount
135161
? Math.min(pendingDifference, activePayment.amount)
136162
: pendingDifference
137163

164+
const minimum = {
165+
value: _minimum.toFixed(currency.decimal_digits),
166+
float: _minimum,
167+
}
168+
138169
if (settlementType === "refund") {
139-
form.setValue("refund.amount", activePayment ? minimum : 0)
170+
form.setValue("refund.amount", minimum)
140171
}
141172

142173
if (settlementType === "credit_line") {
143174
form.setValue("credit_line.amount", minimum)
144175
}
145-
}, [settlementType, activePayment, pendingDifference, form])
176+
}, [settlementType, activePayment, pendingDifference, form, currency])
146177

147178
return (
148179
<RouteDrawer.Form form={form}>
@@ -194,6 +225,7 @@ export const OrderBalanceSettlementForm = ({
194225
<>
195226
<div className="flex flex-col gap-y-4">
196227
<Select
228+
defaultValue={activePayment?.id}
197229
onValueChange={(value) => {
198230
setActivePayment(payments.find((p) => p.id === value)!)
199231
}}
@@ -260,9 +292,12 @@ export const OrderBalanceSettlementForm = ({
260292
decimalScale={currency.decimal_digits}
261293
symbol={currency.symbol_native}
262294
code={currency.code}
263-
value={field.value}
295+
value={field.value.value}
264296
onValueChange={(_value, _name, values) =>
265-
onChange(values?.value ? values?.value : "")
297+
onChange({
298+
value: values?.value,
299+
float: values?.float || null,
300+
})
266301
}
267302
autoFocus
268303
/>
@@ -315,10 +350,13 @@ export const OrderBalanceSettlementForm = ({
315350
decimalScale={currency.decimal_digits}
316351
symbol={currency.symbol_native}
317352
code={currency.code}
318-
value={field.value}
319-
onValueChange={(_value, _name, values) =>
320-
onChange(values?.value ? values?.value : "")
321-
}
353+
value={field.value.value}
354+
onValueChange={(_value, _name, values) => {
355+
onChange({
356+
value: values?.value,
357+
float: values?.float || null,
358+
})
359+
}}
322360
autoFocus
323361
/>
324362
</Form.Control>

packages/admin/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import {
88
Textarea,
99
toast,
1010
} from "@medusajs/ui"
11-
import { useEffect, useMemo } from "react"
11+
import { useEffect, useMemo, useState } from "react"
1212
import { formatValue } from "react-currency-input-field"
1313
import { useForm } from "react-hook-form"
1414
import { useTranslation } from "react-i18next"
15-
import { useNavigate, useSearchParams } from "react-router-dom"
15+
import { useSearchParams } from "react-router-dom"
1616
import * as zod from "zod"
1717
import { Form } from "../../../../../components/common/form"
1818
import { RouteDrawer, useRouteModal } from "../../../../../components/modals"
@@ -29,16 +29,21 @@ type CreateRefundFormProps = {
2929
}
3030

3131
const CreateRefundSchema = zod.object({
32-
amount: zod.string().or(zod.number()),
32+
amount: zod.object({
33+
value: zod.string().or(zod.number()),
34+
float: zod.number().or(zod.null()),
35+
}),
3336
note: zod.string().optional(),
3437
})
3538

3639
export const CreateRefundForm = ({ order }: CreateRefundFormProps) => {
3740
const { t } = useTranslation()
3841
const { handleSuccess } = useRouteModal()
39-
const navigate = useNavigate()
42+
4043
const [searchParams] = useSearchParams()
41-
const paymentId = searchParams.get("paymentId")
44+
const [paymentId, setPaymentId] = useState<string | undefined>(
45+
searchParams.get("paymentId") || undefined
46+
)
4247
const payments = getPaymentsFromOrder(order)
4348
const payment = payments.find((p) => p.id === paymentId)!
4449
const paymentAmount = payment?.amount || 0
@@ -50,7 +55,10 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => {
5055

5156
const form = useForm<zod.infer<typeof CreateRefundSchema>>({
5257
defaultValues: {
53-
amount: paymentAmount,
58+
amount: {
59+
value: paymentAmount.toFixed(currency.decimal_digits),
60+
float: paymentAmount,
61+
},
5462
note: "",
5563
},
5664
resolver: zodResolver(CreateRefundSchema),
@@ -61,29 +69,32 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => {
6169
const paymentAmount = (payment?.amount || 0) as number
6270
const pendingAmount =
6371
pendingDifference < 0
64-
? Math.min(pendingDifference, paymentAmount)
72+
? Math.min(Math.abs(pendingDifference), paymentAmount)
6573
: paymentAmount
6674

6775
const normalizedAmount =
6876
pendingAmount < 0 ? pendingAmount * -1 : pendingAmount
6977

70-
form.setValue("amount", normalizedAmount as number)
71-
}, [payment])
78+
form.setValue("amount", {
79+
value: normalizedAmount.toFixed(currency.decimal_digits),
80+
float: normalizedAmount,
81+
})
82+
}, [payment?.id || ""])
7283

7384
const { mutateAsync, isPending } = useRefundPayment(order.id, payment?.id!)
7485

7586
const handleSubmit = form.handleSubmit(async (data) => {
7687
await mutateAsync(
7788
{
78-
amount: parseFloat(data.amount as string),
89+
amount: data.amount.float!,
7990
note: data.note,
8091
},
8192
{
8293
onSuccess: () => {
8394
toast.success(
8495
t("orders.payment.refundPaymentSuccess", {
8596
amount: formatCurrency(
86-
data.amount as number,
97+
data.amount.float!,
8798
payment?.currency_code!
8899
),
89100
})
@@ -107,11 +118,9 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => {
107118
<RouteDrawer.Body className="flex-1 overflow-auto">
108119
<div className="flex flex-col gap-y-4">
109120
<Select
110-
value={payment?.id}
121+
value={paymentId}
111122
onValueChange={(value) => {
112-
navigate(`/orders/${order.id}/refund?paymentId=${value}`, {
113-
replace: true,
114-
})
123+
setPaymentId(value)
115124
}}
116125
>
117126
<Label className="txt-compact-small mb-[-6px] font-sans font-medium">
@@ -179,9 +188,12 @@ export const CreateRefundForm = ({ order }: CreateRefundFormProps) => {
179188
decimalScale={currency.decimal_digits}
180189
symbol={currency.symbol_native}
181190
code={currency.code}
182-
value={field.value}
191+
value={field.value.value}
183192
onValueChange={(_value, _name, values) =>
184-
onChange(values?.value ? values?.value : "")
193+
onChange({
194+
value: values?.value,
195+
float: values?.float || null,
196+
})
185197
}
186198
autoFocus
187199
/>

0 commit comments

Comments
 (0)