Skip to content

Commit

Permalink
feat(dashboard): cancel return request (#8761)
Browse files Browse the repository at this point in the history
* wip: cancel return request

* fix: refactor

* feat: correct endpoint, add hook, fix types

* feat: add prompt
  • Loading branch information
fPolic authored Aug 27, 2024
1 parent c11ef01 commit 9197bdd
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
37 changes: 33 additions & 4 deletions packages/admin-next/dashboard/src/hooks/api/returns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ export const useReturns = (
return { ...data, ...rest }
}

/**
* REQUEST RETURN
*/

export const useInitiateReturn = (
orderId: string,
options?: UseMutationOptions<
Expand Down Expand Up @@ -86,6 +82,39 @@ export const useInitiateReturn = (
})
}

export const useCancelReturn = (
id: string,
orderId: string,
options?: UseMutationOptions<HttpTypes.AdminReturnResponse, Error>
) => {
return useMutation({
mutationFn: () => sdk.admin.return.cancel(id),
onSuccess: (data: any, variables: any, context: any) => {
queryClient.invalidateQueries({
queryKey: ordersQueryKeys.details(),
})

queryClient.invalidateQueries({
queryKey: ordersQueryKeys.preview(orderId),
refetchType: "all",
})

queryClient.invalidateQueries({
queryKey: returnsQueryKeys.details(),
})
queryClient.invalidateQueries({
queryKey: returnsQueryKeys.lists(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}

/**
* REQUEST RETURN
*/

export const useConfirmReturnRequest = (
id: string,
orderId: string,
Expand Down
7 changes: 6 additions & 1 deletion packages/admin-next/dashboard/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,10 @@
"returnRequestedInfo": "{{requestedItemsCount}}x item return requested",
"returnReceivedInfo": "{{requestedItemsCount}}x item return received",
"activeChangeError": "There is an active order change in progress on this order. Please finish or discard the change first.",
"cancel": {
"title": "Cancel Return",
"description": "Are you sure you want to cancel the return request?"
},
"receive": {
"action": "Receive items",
"receive": "Return {{label}}",
Expand Down Expand Up @@ -1095,7 +1099,8 @@
"items_other": "{{count}} items"
},
"return": {
"created": "Return #{{returnId}} created",
"created": "Return #{{returnId}} requested",
"canceled": "Return #{{returnId}} canceled",
"received": "Return #{{returnId}} received",
"items_one": "{{count}} item returned",
"items_other": "{{count}} items returned"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IconButton, Text, Tooltip, clx, usePrompt } from "@medusajs/ui"
import { IconButton, Text, Tooltip, clx, usePrompt, Button } from "@medusajs/ui"
import * as Collapsible from "@radix-ui/react-collapsible"

import { PropsWithChildren, ReactNode, useMemo, useState } from "react"
Expand All @@ -16,10 +16,10 @@ import { useTranslation } from "react-i18next"

import { useClaims } from "../../../../../hooks/api/claims"
import { useExchanges } from "../../../../../hooks/api/exchanges"
import { useReturns } from "../../../../../hooks/api/returns"
import { useDate } from "../../../../../hooks/use-date"
import { useCancelReturn, useReturns } from "../../../../../hooks/api/returns"
import { getStylizedAmount } from "../../../../../lib/money-amount-helpers"
import { getPaymentsFromOrder } from "../order-payment-section"
import { useDate } from "../../../../../hooks/use-date"

type OrderTimelineProps = {
order: AdminOrder
Expand Down Expand Up @@ -224,9 +224,18 @@ const useActivityItems = (order: AdminOrder) => {
returnId: ret.id.slice(-7),
}),
timestamp: ret.created_at,
children: <ReturnBody orderReturn={ret} />,
children: <ReturnBody orderReturn={ret} isCreated={!ret.canceled_at} />,
})

if (ret.canceled_at) {
items.push({
title: t("orders.activity.events.return.canceled", {
returnId: ret.id.slice(-7),
}),
timestamp: ret.canceled_at,
})
}

if (ret.status === "received" || ret.status === "partially_received") {
items.push({
title: t("orders.activity.events.return.received", {
Expand Down Expand Up @@ -486,20 +495,60 @@ const FulfillmentCreatedBody = ({
)
}

const ReturnBody = ({ orderReturn }: { orderReturn: AdminReturn }) => {
const ReturnBody = ({
orderReturn,
isCreated,
}: {
orderReturn: AdminReturn
isCreated: boolean
}) => {
const prompt = usePrompt()
const { t } = useTranslation()

const { mutateAsync: cancelReturnRequest } = useCancelReturn(
orderReturn.id,
orderReturn.order_id
)

const onCancel = async () => {
const res = await prompt({
title: t("orders.returns.cancel.title"),
description: t("orders.returns.cancel.description"),
confirmText: t("actions.confirm"),
cancelText: t("actions.cancel"),
})

if (!res) {
return
}

await cancelReturnRequest()
}

const numberOfItems = orderReturn.items.reduce((acc, item) => {
return acc + item.quantity
}, 0)

return (
<div>
<div className="flex items-start gap-1">
<Text size="small" className="text-ui-fg-subtle">
{t("orders.activity.events.return.items", {
count: numberOfItems,
})}
</Text>
{isCreated && (
<>
<div className="mt-[2px] flex items-center leading-none"></div>
<Button
onClick={onCancel}
className="text-ui-fg-subtle h-auto px-0 leading-none hover:bg-transparent"
variant="transparent"
size="small"
>
{t("actions.cancel")}
</Button>
</>
)}
</div>
)
}
Expand Down
15 changes: 15 additions & 0 deletions packages/core/js-sdk/src/admin/return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ export class Return {
)
}

async cancel(
id: string,
query?: HttpTypes.SelectParams,
headers?: ClientHeaders
) {
return await this.client.fetch<HttpTypes.AdminReturnResponse>(
`/admin/returns/${id}/cancel`,
{
method: "POST",
headers,
query,
}
)
}

async cancelRequest(
id: string,
query?: HttpTypes.SelectParams,
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/src/http/return/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface BaseReturn {
items: BaseReturnItem[]
received_at: string
created_at: string
canceled_at: string
}

0 comments on commit 9197bdd

Please sign in to comment.