Skip to content

Commit 5e827ec

Browse files
authored
feat(admin-shared,dashboard,js-sdk,types): refund reasons in dashboard (medusajs#13575)
CLOSES CORE-1209 This PR just adds the stuff necessary to support refund reasons in the dashboard. It adds the option in the settings tab and allows viewing, creating, editing and deleting refund reasons. I hate to open such a big PR but most of it is copy pasted from the return reasons. Major difference is only the fact that refund reasons don't have a `value` field
1 parent 543c9f7 commit 5e827ec

File tree

39 files changed

+1187
-128
lines changed

39 files changed

+1187
-128
lines changed

.changeset/quick-rice-deny.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@medusajs/admin-shared": patch
3+
"@medusajs/dashboard": patch
4+
"@medusajs/js-sdk": patch
5+
"@medusajs/types": patch
6+
"@medusajs/payment": patch
7+
---
8+
9+
feat(admin-shared,dashboard,js-sdk,types,payment): refund reasons in dashboard

packages/admin/admin-shared/src/extensions/widgets/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ const RETURN_REASON_INJECTION_ZONES = [
189189
"return_reason.list.after",
190190
] as const
191191

192+
const REFUND_REASON_INJECTION_ZONES = [
193+
"refund_reason.list.before",
194+
"refund_reason.list.after",
195+
] as const
196+
192197
const INVENTORY_ITEM_INJECTION_ZONES = [
193198
"inventory_item.details.before",
194199
"inventory_item.details.after",
@@ -229,5 +234,6 @@ export const INJECTION_ZONES = [
229234
...CAMPAIGN_INJECTION_ZONES,
230235
...TAX_INJECTION_ZONES,
231236
...RETURN_REASON_INJECTION_ZONES,
237+
...REFUND_REASON_INJECTION_ZONES,
232238
...INVENTORY_ITEM_INJECTION_ZONES,
233239
] as const

packages/admin/dashboard/src/components/layout/settings-layout/settings-layout.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ const useSettingRoutes = (): INavItem[] => {
4343
label: t("returnReasons.domain"),
4444
to: "/settings/return-reasons",
4545
},
46+
{
47+
label: t("refundReasons.domain"),
48+
to: "/settings/refund-reasons",
49+
},
4650
{
4751
label: t("salesChannels.domain"),
4852
to: "/settings/sales-channels",

packages/admin/dashboard/src/dashboard-app/routes/get-route.map.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,42 @@ export function getRouteMap({
17801780
},
17811781
],
17821782
},
1783+
{
1784+
path: "refund-reasons",
1785+
element: <Outlet />,
1786+
handle: {
1787+
breadcrumb: () => t("refundReasons.domain"),
1788+
},
1789+
children: [
1790+
{
1791+
path: "",
1792+
lazy: () =>
1793+
import("../../routes/refund-reasons/refund-reason-list"),
1794+
children: [
1795+
{
1796+
path: "create",
1797+
lazy: () =>
1798+
import(
1799+
"../../routes/refund-reasons/refund-reason-create"
1800+
),
1801+
},
1802+
1803+
{
1804+
path: ":id",
1805+
children: [
1806+
{
1807+
path: "edit",
1808+
lazy: () =>
1809+
import(
1810+
"../../routes/refund-reasons/refund-reason-edit"
1811+
),
1812+
},
1813+
],
1814+
},
1815+
],
1816+
},
1817+
],
1818+
},
17831819
...(settingsRoutes?.[0]?.children || []),
17841820
],
17851821
},
Lines changed: 105 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,124 @@
11
import { HttpTypes } from "@medusajs/types"
2-
import { QueryKey, useQuery, UseQueryOptions } from "@tanstack/react-query"
2+
import {
3+
useMutation,
4+
UseMutationOptions,
5+
useQuery,
6+
UseQueryOptions,
7+
} from "@tanstack/react-query"
8+
9+
import { FetchError } from "@medusajs/js-sdk"
310
import { sdk } from "../../lib/client"
11+
import { queryClient } from "../../lib/query-client"
412
import { queryKeysFactory } from "../../lib/query-key-factory"
5-
import { FetchError } from "@medusajs/js-sdk"
613

7-
const REFUND_REASON_QUERY_KEY = "refund-reason" as const
8-
export const refundReasonQueryKeys = queryKeysFactory(REFUND_REASON_QUERY_KEY)
14+
const REFUND_REASONS_QUERY_KEY = "refund_reasons" as const
15+
export const refundReasonsQueryKeys = queryKeysFactory(REFUND_REASONS_QUERY_KEY)
916

1017
export const useRefundReasons = (
11-
query?: HttpTypes.RefundReasonFilters,
18+
query?: HttpTypes.AdminRefundReasonListParams,
1219
options?: Omit<
1320
UseQueryOptions<
14-
HttpTypes.RefundReasonsResponse,
21+
HttpTypes.AdminRefundReasonListResponse,
1522
FetchError,
16-
HttpTypes.RefundReasonsResponse,
17-
QueryKey
23+
HttpTypes.AdminRefundReasonListResponse
1824
>,
19-
"queryKey" | "queryFn"
25+
"queryFn" | "queryKey"
2026
>
2127
) => {
2228
const { data, ...rest } = useQuery({
2329
queryFn: () => sdk.admin.refundReason.list(query),
24-
queryKey: [],
30+
queryKey: refundReasonsQueryKeys.list(query),
2531
...options,
2632
})
2733

2834
return { ...data, ...rest }
2935
}
36+
37+
export const useRefundReason = (
38+
id: string,
39+
query?: HttpTypes.AdminRefundReasonParams,
40+
options?: Omit<
41+
UseQueryOptions<
42+
HttpTypes.AdminRefundReasonResponse,
43+
FetchError,
44+
HttpTypes.AdminRefundReasonResponse
45+
>,
46+
"queryFn" | "queryKey"
47+
>
48+
) => {
49+
const { data, ...rest } = useQuery({
50+
queryFn: () => sdk.admin.refundReason.retrieve(id, query),
51+
queryKey: refundReasonsQueryKeys.detail(id),
52+
...options,
53+
})
54+
55+
return { ...data, ...rest }
56+
}
57+
58+
export const useCreateRefundReason = (
59+
query?: HttpTypes.AdminRefundReasonParams,
60+
options?: UseMutationOptions<
61+
HttpTypes.RefundReasonResponse,
62+
FetchError,
63+
HttpTypes.AdminCreateRefundReason
64+
>
65+
) => {
66+
return useMutation({
67+
mutationFn: async (data) => sdk.admin.refundReason.create(data, query),
68+
onSuccess: (data, variables, context) => {
69+
queryClient.invalidateQueries({
70+
queryKey: refundReasonsQueryKeys.lists(),
71+
})
72+
73+
options?.onSuccess?.(data, variables, context)
74+
},
75+
...options,
76+
})
77+
}
78+
79+
export const useUpdateRefundReason = (
80+
id: string,
81+
options?: UseMutationOptions<
82+
HttpTypes.AdminRefundReasonResponse,
83+
FetchError,
84+
HttpTypes.AdminUpdateRefundReason
85+
>
86+
) => {
87+
return useMutation({
88+
mutationFn: async (data) => sdk.admin.refundReason.update(id, data),
89+
onSuccess: (data, variables, context) => {
90+
queryClient.invalidateQueries({
91+
queryKey: refundReasonsQueryKeys.lists(),
92+
})
93+
queryClient.invalidateQueries({
94+
queryKey: refundReasonsQueryKeys.detail(data.refund_reason.id),
95+
})
96+
97+
options?.onSuccess?.(data, variables, context)
98+
},
99+
...options,
100+
})
101+
}
102+
103+
export const useDeleteRefundReasonLazy = (
104+
options?: UseMutationOptions<
105+
HttpTypes.AdminRefundReasonDeleteResponse,
106+
FetchError,
107+
string
108+
>
109+
) => {
110+
return useMutation({
111+
mutationFn: (id: string) => sdk.admin.refundReason.delete(id),
112+
onSuccess: (data, variables, context) => {
113+
queryClient.invalidateQueries({
114+
queryKey: refundReasonsQueryKeys.lists(),
115+
})
116+
queryClient.invalidateQueries({
117+
queryKey: refundReasonsQueryKeys.details(),
118+
})
119+
120+
options?.onSuccess?.(data, variables, context)
121+
},
122+
...options,
123+
})
124+
}

packages/admin/dashboard/src/hooks/table/columns/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./use-order-table-columns"
66
export * from "./use-product-table-columns"
77
export * from "./use-product-tag-table-columns"
88
export * from "./use-product-type-table-columns"
9+
export * from "./use-refund-reason-table-columns"
910
export * from "./use-region-table-columns"
1011
export * from "./use-return-reason-table-columns"
1112
export * from "./use-tax-rates-table-columns"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { HttpTypes } from "@medusajs/types"
2+
import { useMemo } from "react"
3+
import { useTranslation } from "react-i18next"
4+
import { createDataTableColumnHelper } from "@medusajs/ui"
5+
import { DescriptionCell } from "../../../components/table/table-cells/sales-channel/description-cell"
6+
7+
const columnHelper = createDataTableColumnHelper<HttpTypes.AdminRefundReason>()
8+
9+
export const useRefundReasonTableColumns = () => {
10+
const { t } = useTranslation()
11+
12+
return useMemo(
13+
() => [
14+
columnHelper.accessor("label", {
15+
header: () => t("fields.label"),
16+
enableSorting: true,
17+
sortLabel: t("fields.label"),
18+
sortAscLabel: t("filters.sorting.alphabeticallyAsc"),
19+
sortDescLabel: t("filters.sorting.alphabeticallyDesc"),
20+
}),
21+
columnHelper.accessor("description", {
22+
header: () => t("fields.description"),
23+
cell: ({ getValue }) => <DescriptionCell description={getValue()} />,
24+
enableSorting: true,
25+
sortLabel: t("fields.description"),
26+
sortAscLabel: t("filters.sorting.alphabeticallyAsc"),
27+
sortDescLabel: t("filters.sorting.alphabeticallyDesc"),
28+
}),
29+
],
30+
[t]
31+
)
32+
}

packages/admin/dashboard/src/hooks/table/query/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./use-order-table-query"
66
export * from "./use-product-table-query"
77
export * from "./use-product-tag-table-query"
88
export * from "./use-product-type-table-query"
9+
export * from "./use-refund-reason-table-query"
910
export * from "./use-region-table-query"
1011
export * from "./use-return-reason-table-query"
1112
export * from "./use-shipping-option-table-query"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { HttpTypes } from "@medusajs/types"
2+
import { useQueryParams } from "../../use-query-params"
3+
4+
type UseRefundReasonTableQueryProps = {
5+
prefix?: string
6+
pageSize?: number
7+
}
8+
9+
export const useRefundReasonTableQuery = ({
10+
prefix,
11+
pageSize = 20,
12+
}: UseRefundReasonTableQueryProps) => {
13+
const queryObject = useQueryParams(
14+
["offset", "q", "order", "created_at", "updated_at"],
15+
prefix
16+
)
17+
18+
const { offset, q, order, created_at, updated_at } = queryObject
19+
const searchParams: HttpTypes.AdminRefundReasonListParams = {
20+
limit: pageSize,
21+
offset: offset ? Number(offset) : 0,
22+
order,
23+
created_at: created_at ? JSON.parse(created_at) : undefined,
24+
updated_at: updated_at ? JSON.parse(updated_at) : undefined,
25+
q,
26+
}
27+
28+
return {
29+
searchParams,
30+
raw: queryObject,
31+
}
32+
}

0 commit comments

Comments
 (0)