-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/receive-return-e2e
- Loading branch information
Showing
45 changed files
with
1,414 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...rc/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { zodResolver } from "@hookform/resolvers/zod" | ||
import { HttpTypes } from "@medusajs/types" | ||
import { Button, CurrencyInput, toast } from "@medusajs/ui" | ||
import { useForm } from "react-hook-form" | ||
import { useTranslation } from "react-i18next" | ||
import * as zod from "zod" | ||
import { Form } from "../../../../../components/common/form" | ||
import { RouteDrawer, useRouteModal } from "../../../../../components/modals" | ||
import { useRefundPayment } from "../../../../../hooks/api" | ||
import { getCurrencySymbol } from "../../../../../lib/data/currencies" | ||
import { formatCurrency } from "../../../../../lib/format-currency" | ||
|
||
type CreateRefundFormProps = { | ||
payment: HttpTypes.AdminPayment | ||
} | ||
|
||
const CreateRefundSchema = zod.object({ | ||
amount: zod.number(), | ||
}) | ||
|
||
export const CreateRefundForm = ({ payment }: CreateRefundFormProps) => { | ||
const { t } = useTranslation() | ||
const { handleSuccess } = useRouteModal() | ||
const paymentAmount = payment.amount as unknown as number | ||
|
||
const form = useForm<zod.infer<typeof CreateRefundSchema>>({ | ||
defaultValues: { | ||
amount: paymentAmount, | ||
}, | ||
resolver: zodResolver(CreateRefundSchema), | ||
}) | ||
|
||
const { mutateAsync, isPending } = useRefundPayment(payment.id) | ||
|
||
const handleSubmit = form.handleSubmit(async (data) => { | ||
await mutateAsync( | ||
{ | ||
amount: data.amount, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
toast.success( | ||
t("orders.payment.refundPaymentSuccess", { | ||
amount: formatCurrency(data.amount, payment.currency_code), | ||
}) | ||
) | ||
|
||
handleSuccess() | ||
}, | ||
onError: (error) => { | ||
toast.error(error.message) | ||
}, | ||
} | ||
) | ||
}) | ||
|
||
return ( | ||
<RouteDrawer.Form form={form}> | ||
<form onSubmit={handleSubmit} className="flex flex-1 flex-col"> | ||
<RouteDrawer.Body> | ||
<div className="flex flex-col gap-y-4"> | ||
<Form.Field | ||
control={form.control} | ||
name="amount" | ||
rules={{ | ||
required: true, | ||
min: 0, | ||
max: paymentAmount, | ||
}} | ||
render={({ field: { onChange, ...field } }) => { | ||
return ( | ||
<Form.Item> | ||
<Form.Label>{t("fields.amount")}</Form.Label> | ||
|
||
<Form.Control> | ||
<CurrencyInput | ||
{...field} | ||
min={0} | ||
onChange={(e) => { | ||
const val = | ||
e.target.value === "" | ||
? null | ||
: Number(e.target.value) | ||
|
||
onChange(val) | ||
|
||
if (val && !isNaN(val)) { | ||
if (val < 0 || val > paymentAmount) { | ||
form.setError(`amount`, { | ||
type: "manual", | ||
message: t( | ||
"orders.payment.createRefundWrongQuantity", | ||
{ number: paymentAmount } | ||
), | ||
}) | ||
} else { | ||
form.clearErrors(`amount`) | ||
} | ||
} | ||
}} | ||
code={payment.currency_code} | ||
symbol={getCurrencySymbol(payment.currency_code)} | ||
value={field.value} | ||
/> | ||
</Form.Control> | ||
|
||
<Form.ErrorMessage /> | ||
</Form.Item> | ||
) | ||
}} | ||
/> | ||
</div> | ||
</RouteDrawer.Body> | ||
|
||
<RouteDrawer.Footer> | ||
<div className="flex items-center justify-end gap-x-2"> | ||
<RouteDrawer.Close asChild> | ||
<Button variant="secondary" size="small"> | ||
{t("actions.cancel")} | ||
</Button> | ||
</RouteDrawer.Close> | ||
|
||
<Button | ||
isLoading={isPending} | ||
type="submit" | ||
variant="primary" | ||
size="small" | ||
disabled={!!Object.keys(form.formState.errors || {}).length} | ||
> | ||
{t("actions.save")} | ||
</Button> | ||
</div> | ||
</RouteDrawer.Footer> | ||
</form> | ||
</RouteDrawer.Form> | ||
) | ||
} |
1 change: 1 addition & 0 deletions
1
...xt/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./create-refund-form" |
1 change: 1 addition & 0 deletions
1
packages/admin-next/dashboard/src/routes/orders/order-create-refund/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { OrderCreateRefund as Component } from "./order-create-refund" |
26 changes: 26 additions & 0 deletions
26
packages/admin-next/dashboard/src/routes/orders/order-create-refund/order-create-refund.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Heading } from "@medusajs/ui" | ||
import { useTranslation } from "react-i18next" | ||
import { useParams } from "react-router-dom" | ||
import { RouteDrawer } from "../../../components/modals" | ||
import { usePayment } from "../../../hooks/api" | ||
import { CreateRefundForm } from "./components/create-refund-form" | ||
|
||
export const OrderCreateRefund = () => { | ||
const { t } = useTranslation() | ||
const params = useParams() | ||
const { payment, isLoading, isError, error } = usePayment(params.paymentId!) | ||
|
||
if (isError) { | ||
throw error | ||
} | ||
|
||
return ( | ||
<RouteDrawer> | ||
<RouteDrawer.Header> | ||
<Heading>{t("orders.payment.createRefund")}</Heading> | ||
</RouteDrawer.Header> | ||
|
||
{!isLoading && payment && <CreateRefundForm payment={payment} />} | ||
</RouteDrawer> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.