Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): claims first implementation #8468

Merged
merged 10 commits into from
Aug 7, 2024
13 changes: 11 additions & 2 deletions packages/admin-next/dashboard/src/hooks/api/claims.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export const useClaims = (
export const useCreateClaim = (
orderId: string,
options?: UseMutationOptions<
HttpTypes.AdminClaimResponse,
{
claim: HttpTypes.AdminClaimResponse
order: HttpTypes.AdminOrderResponse
},
Error,
HttpTypes.AdminCreateClaim
>
Expand All @@ -75,6 +78,11 @@ export const useCreateClaim = (
queryClient.invalidateQueries({
queryKey: ordersQueryKeys.preview(orderId),
})

queryClient.invalidateQueries({
queryKey: claimsQueryKeys.lists(),
})

options?.onSuccess?.(data, variables, context)
},
...options,
Expand Down Expand Up @@ -103,6 +111,7 @@ export const useCancelClaim = (
queryClient.invalidateQueries({
queryKey: claimsQueryKeys.details(),
})

queryClient.invalidateQueries({
queryKey: claimsQueryKeys.lists(),
})
Expand Down Expand Up @@ -231,7 +240,7 @@ export const useAddClaimInboundItems = (
})
}

export const useUpdateClaimInboundItems = (
export const useUpdateClaimInboundItem = (
id: string,
orderId: string,
options?: UseMutationOptions<
Expand Down
6 changes: 6 additions & 0 deletions packages/admin-next/dashboard/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@
}
}
},
"claims": {
"create": "Create Claim",
"outbound": "Outbound",
"refundAmount": "Estimated difference",
"activeChangeError": "There is an active order change on this order. Please finish or discard the previous change."
},
"reservations": {
"allocatedLabel": "Allocated",
"notAllocatedLabel": "Not allocated"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ export const RouteMap: RouteObject[] = [
lazy: () =>
import("../../routes/orders/order-create-return"),
},
{
path: "claims",
lazy: () =>
import("../../routes/orders/order-create-claim"),
},
{
path: "payments/:paymentId/refund",
lazy: () =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { useEffect, useMemo, useState } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate, useParams } from "react-router-dom"

import { toast } from "@medusajs/ui"

import { RouteFocusModal } from "../../../components/modals"
import { ClaimCreateForm } from "./components/claim-create-form"

import { useOrder, useOrderPreview } from "../../../hooks/api/orders"
import { useClaims, useCreateClaim } from "../../../hooks/api/claims"
import { DEFAULT_FIELDS } from "../order-detail/constants"

let IS_REQUEST_RUNNING = false

export const ClaimCreate = () => {
const { id } = useParams()
const navigate = useNavigate()
const { t } = useTranslation()

const { order } = useOrder(id!, {
fields: DEFAULT_FIELDS,
})

const { order: preview } = useOrderPreview(id!)

const [activeClaimId, setActiveClaimId] = useState()

const { mutateAsync: createClaim } = useCreateClaim(order.id)

// TODO: GET /claims/:id is not implemented
// const { claim } = useClaim(activeClaimId, undefined, {
// enabled: !!activeClaimId,
// })
Comment on lines +31 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo: This was introduced yesterday. Maybe just replace it in a follow-up PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its already replaced here


// TEMP HACK: until the endpoint above is implemented
const { claims } = useClaims(undefined, {
enabled: !!activeClaimId,
limit: 999,
})

const claim = useMemo(() => {
if (claims) {
return claims.find((c) => c.id === activeClaimId)
}
}, [claims, activeClaimId])

useEffect(() => {
async function run() {
if (IS_REQUEST_RUNNING || !preview) {
return
}

if (preview.order_change) {
if (preview.order_change.change_type === "claim") {
setActiveClaimId(preview.order_change.claim_id)
} else {
navigate(`/orders/${preview.id}`, { replace: true })
toast.error(t("orders.claims.activeChangeError"))
}

return
}

IS_REQUEST_RUNNING = true

try {
const { claim } = await createClaim({
order_id: preview.id,
type: "replace",
})
setActiveClaimId(claim.id)
} catch (e) {
navigate(`/orders/${preview.id}`, { replace: true })
toast.error(e.message)
} finally {
IS_REQUEST_RUNNING = false
}
}

run()
}, [preview])

return (
<RouteFocusModal>
{claim && preview && order && (
<ClaimCreateForm order={order} claim={claim} preview={preview} />
)}
</RouteFocusModal>
)
}
Loading
Loading