From a93b0252334c40ae0841713614a3dc334ad555dc Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Thu, 8 Aug 2024 23:19:08 +0200 Subject: [PATCH] feat(dashboard,js-sdk,types): ability to add refund reason and note (#8466) what: - adds ability to add refund reason and note to a refund Screenshot 2024-08-06 at 13 06 59 Screenshot 2024-08-06 at 12 57 18 Screenshot 2024-08-06 at 13 08 46 --- .../dashboard/src/hooks/api/index.ts | 1 + .../src/hooks/api/refund-reasons.tsx | 28 +++++++++ .../dashboard/src/i18n/translations/en.json | 1 + .../create-refund-form/create-refund-form.tsx | 57 ++++++++++++++++++- .../order-create-refund.tsx | 16 +++++- .../order-payment-section.tsx | 35 ++++++------ .../routes/orders/order-detail/constants.ts | 1 + packages/core/js-sdk/src/admin/index.ts | 3 + .../core/js-sdk/src/admin/refund-reasons.ts | 20 +++++++ packages/core/types/src/http/payment/admin.ts | 18 ++++++ 10 files changed, 160 insertions(+), 20 deletions(-) create mode 100644 packages/admin-next/dashboard/src/hooks/api/refund-reasons.tsx create mode 100644 packages/core/js-sdk/src/admin/refund-reasons.ts diff --git a/packages/admin-next/dashboard/src/hooks/api/index.ts b/packages/admin-next/dashboard/src/hooks/api/index.ts index 1c90b4192b..4d0cda2c9c 100644 --- a/packages/admin-next/dashboard/src/hooks/api/index.ts +++ b/packages/admin-next/dashboard/src/hooks/api/index.ts @@ -19,6 +19,7 @@ export * from "./product-types" export * from "./product-variants" export * from "./products" export * from "./promotions" +export * from "./refund-reasons" export * from "./regions" export * from "./reservations" export * from "./sales-channels" diff --git a/packages/admin-next/dashboard/src/hooks/api/refund-reasons.tsx b/packages/admin-next/dashboard/src/hooks/api/refund-reasons.tsx new file mode 100644 index 0000000000..6dd8992066 --- /dev/null +++ b/packages/admin-next/dashboard/src/hooks/api/refund-reasons.tsx @@ -0,0 +1,28 @@ +import { HttpTypes } from "@medusajs/types" +import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query" +import { sdk } from "../../lib/client" +import { queryKeysFactory } from "../../lib/query-key-factory" + +const REFUND_REASON_QUERY_KEY = "refund-reason" as const +export const paymentQueryKeys = queryKeysFactory(REFUND_REASON_QUERY_KEY) + +export const useRefundReasons = ( + query?: HttpTypes.RefundReasonFilters, + options?: Omit< + UseQueryOptions< + HttpTypes.RefundReasonsResponse, + Error, + HttpTypes.RefundReasonsResponse, + QueryKey + >, + "queryKey" | "queryFn" + > +) => { + const { data, ...rest } = useQuery({ + queryFn: () => sdk.admin.refundReason.list(query), + queryKey: [], + ...options, + }) + + return { ...data, ...rest } +} diff --git a/packages/admin-next/dashboard/src/i18n/translations/en.json b/packages/admin-next/dashboard/src/i18n/translations/en.json index c92b3c1dd0..6d3cebdbfd 100644 --- a/packages/admin-next/dashboard/src/i18n/translations/en.json +++ b/packages/admin-next/dashboard/src/i18n/translations/en.json @@ -2336,6 +2336,7 @@ "totalRedemptions": "Total Redemptions", "countries": "Countries", "paymentProviders": "Payment Providers", + "refundReason": "Refund Reason", "fulfillmentProviders": "Fulfillment Providers", "fulfillmentProvider": "Fulfillment Provider", "providers": "Providers", diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx index 5c4415c0ab..e35165a173 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx @@ -1,10 +1,12 @@ import { zodResolver } from "@hookform/resolvers/zod" import { HttpTypes } from "@medusajs/types" -import { Button, CurrencyInput, toast } from "@medusajs/ui" +import { Button, CurrencyInput, Textarea, toast } from "@medusajs/ui" import { useForm } from "react-hook-form" import { useTranslation } from "react-i18next" import * as zod from "zod" +import { upperCaseFirst } from "../../../../../../../../core/utils/src/common/upper-case-first" import { Form } from "../../../../../components/common/form" +import { Combobox } from "../../../../../components/inputs/combobox" import { RouteDrawer, useRouteModal } from "../../../../../components/modals" import { useRefundPayment } from "../../../../../hooks/api" import { getCurrencySymbol } from "../../../../../lib/data/currencies" @@ -12,13 +14,19 @@ import { formatCurrency } from "../../../../../lib/format-currency" type CreateRefundFormProps = { payment: HttpTypes.AdminPayment + refundReasons: HttpTypes.AdminRefundReason[] } const CreateRefundSchema = zod.object({ amount: zod.number(), + refund_reason_id: zod.string().nullish(), + note: zod.string().optional(), }) -export const CreateRefundForm = ({ payment }: CreateRefundFormProps) => { +export const CreateRefundForm = ({ + payment, + refundReasons, +}: CreateRefundFormProps) => { const { t } = useTranslation() const { handleSuccess } = useRouteModal() const paymentAmount = payment.amount as unknown as number @@ -26,6 +34,7 @@ export const CreateRefundForm = ({ payment }: CreateRefundFormProps) => { const form = useForm>({ defaultValues: { amount: paymentAmount, + note: "", }, resolver: zodResolver(CreateRefundSchema), }) @@ -36,6 +45,8 @@ export const CreateRefundForm = ({ payment }: CreateRefundFormProps) => { await mutateAsync( { amount: data.amount, + refund_reason_id: data.refund_reason_id, + note: data.note, }, { onSuccess: () => { @@ -109,6 +120,48 @@ export const CreateRefundForm = ({ payment }: CreateRefundFormProps) => { ) }} /> + + {/* TODO: Bring this back when we have a refund reason management UI */} + {/* { + return ( + + {t("fields.refundReason")} + + + ({ + label: upperCaseFirst(pp.label), + value: pp.id, + }))} + /> + + + + ) + }} + /> */} + + { + return ( + + {t("fields.note")} + + +