Skip to content

Commit

Permalink
feat(types,dashboard): ability to set shipping methods for claim (#8533)
Browse files Browse the repository at this point in the history
  • Loading branch information
riqwan authored Aug 9, 2024
1 parent fdc94f6 commit 3fe5848
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 89 deletions.
3 changes: 3 additions & 0 deletions packages/admin-next/dashboard/src/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@
"cancelClaim": {
"successToast": "Claim was successfully canceled."
}
},
"tooltips": {
"onlyReturnShippingOptions": "This list will consist of only return shipping options."
}
},
"reservations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNavigate, useParams } from "react-router-dom"
import { RouteFocusModal } from "../../../components/modals"
import { useClaim, useCreateClaim } from "../../../hooks/api/claims"
import { useOrder, useOrderPreview } from "../../../hooks/api/orders"
import { useReturn } from "../../../hooks/api/returns"
import { DEFAULT_FIELDS } from "../order-detail/constants"
import { ClaimCreateForm } from "./components/claim-create-form"

Expand All @@ -27,6 +28,9 @@ export const ClaimCreate = () => {
const { claim } = useClaim(activeClaimId!, undefined, {
enabled: !!activeClaimId,
})
const { return: orderReturn } = useReturn(claim?.return_id!, undefined, {
enabled: !!claim?.return_id,
})

useEffect(() => {
async function run() {
Expand Down Expand Up @@ -68,7 +72,12 @@ export const ClaimCreate = () => {
return (
<RouteFocusModal>
{claim && preview && order && (
<ClaimCreateForm order={order} claim={claim} preview={preview} />
<ClaimCreateForm
order={order}
claim={claim}
preview={preview}
orderReturn={orderReturn}
/>
)}
</RouteFocusModal>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { AddClaimItemsTable } from "../add-claim-items-table"
import { ClaimInboundItem } from "./claim-inbound-item.tsx"
import { ClaimCreateSchema, CreateClaimSchemaType } from "./schema"

import { AdminReturn } from "@medusajs/types"
import {
useAddClaimInboundItems,
useAddClaimInboundShipping,
Expand All @@ -45,6 +46,7 @@ import {
useUpdateClaimInboundItem,
useUpdateClaimInboundShipping,
} from "../../../../../hooks/api/claims"
import { useUpdateReturn } from "../../../../../hooks/api/returns.tsx"
import { sdk } from "../../../../../lib/client"
import { currencies } from "../../../../../lib/data/currencies"
import { ClaimOutboundSection } from "./claim-outbound-section"
Expand All @@ -54,6 +56,7 @@ type ReturnCreateFormProps = {
order: AdminOrder
claim: AdminClaim
preview: AdminOrderPreview
orderReturn?: AdminReturn
}

let itemsToAdd: string[] = []
Expand All @@ -64,6 +67,7 @@ export const ClaimCreateForm = ({
order,
preview,
claim,
orderReturn,
}: ReturnCreateFormProps) => {
const { t } = useTranslation()
const { handleSuccess } = useRouteModal()
Expand All @@ -78,18 +82,6 @@ export const ClaimCreateForm = ({
Record<string, InventoryLevelDTO[]>
>({})

/**
* HOOKS
*/
const { stock_locations = [] } = useStockLocations({ limit: 999 })
const { shipping_options = [] } = useShippingOptions({
limit: 999,
fields: "*prices,+service_zone.fulfillment_set.location.id",
/**
* TODO: this should accept filter for location_id
*/
})

/**
* MUTATIONS
*/
Expand All @@ -100,7 +92,11 @@ export const ClaimCreateForm = ({
useCancelClaimRequest(claim.id, order.id)

// TODO: implement update claim request
const { mutateAsync: updateClaimRequest, isPending: isUpdating } = {} // useUpdateClaim(claim.id, order.id)

const { mutateAsync: updateReturn, isPending: isUpdating } = useUpdateReturn(
claim.return_id!,
order.id
)

const {
mutateAsync: addInboundShipping,
Expand Down Expand Up @@ -166,9 +162,17 @@ export const ClaimCreateForm = ({
*/
const form = useForm<CreateClaimSchemaType>({
defaultValues: () => {
const method = preview.shipping_methods.find(
(s) => !!s.actions?.find((a) => a.action === "SHIPPING_ADD")
)
const inboundShippingMethod = preview.shipping_methods.find((s) => {
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")

return !!action?.return?.id
})

const outboundShippingMethod = preview.shipping_methods.find((s) => {
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")

return action && !!!action?.return?.id
})

return Promise.resolve({
inbound_items: inboundPreviewItems.map((i) => {
Expand All @@ -189,16 +193,43 @@ export const ClaimCreateForm = ({
variant_id: i.variant_id,
quantity: i.detail.quantity,
})),
inbound_option_id: method ? method.shipping_option_id : "",
// TODO: pick up shipping method for outbound when available
outbound_option_id: method ? method.shipping_option_id : "",
location_id: "",
inbound_option_id: inboundShippingMethod
? inboundShippingMethod.shipping_option_id
: "",
outbound_option_id: outboundShippingMethod
? outboundShippingMethod.shipping_option_id
: "",
location_id: orderReturn?.location_id,
send_notification: false,
})
},
resolver: zodResolver(ClaimCreateSchema),
})

const locationId = form.watch("location_id")

/**
* HOOKS
*/
const { stock_locations = [] } = useStockLocations({ limit: 999 })
const { shipping_options = [] } = useShippingOptions(
{
limit: 999,
fields: "*prices,+service_zone.fulfillment_set.location.id",
stock_location_id: locationId,
},
{
enabled: !!locationId,
}
)

const inboundShippingOptions = shipping_options.filter(
(shippingOption) =>
!!shippingOption.rules.find(
(r) => r.attribute === "is_return" && r.value === "true"
)
)

const {
fields: inboundItems,
append,
Expand Down Expand Up @@ -257,8 +288,11 @@ export const ClaimCreateForm = ({
}
}, [preview.shipping_methods])

useEffect(() => {
form.setValue("location_id", orderReturn?.location_id)
}, [orderReturn])

const showInboundItemsPlaceholder = !inboundItems.length
const locationId = form.watch("location_id")
const shippingOptionId = form.watch("inbound_option_id")

const handleSubmit = form.handleSubmit(async (data) => {
Expand Down Expand Up @@ -307,7 +341,7 @@ export const ClaimCreateForm = ({
}

const onLocationChange = async (selectedLocationId?: string | null) => {
await updateClaimRequest({ location_id: selectedLocationId })
await updateReturn({ location_id: selectedLocationId })
}

const onShippingOptionChange = async (selectedOptionId: string) => {
Expand Down Expand Up @@ -379,7 +413,7 @@ export const ClaimCreateForm = ({
).variants

variants.forEach((variant) => {
ret[variant.id] = variant.inventory[0]?.location_levels || []
ret[variant.id] = variant.inventory?.[0]?.location_levels || []
})

return ret
Expand Down Expand Up @@ -549,12 +583,12 @@ export const ClaimCreateForm = ({
<Form.Item>
<Form.Control>
<Combobox
value={value}
{...field}
value={value ?? undefined}
onChange={(v) => {
onChange(v)
onLocationChange(v)
}}
{...field}
options={(stock_locations ?? []).map(
(stockLocation) => ({
label: stockLocation.name,
Expand All @@ -572,9 +606,14 @@ export const ClaimCreateForm = ({
{/*INBOUND SHIPPING*/}
<div className="grid grid-cols-1 gap-2 md:grid-cols-2">
<div>
<Form.Label>
<Form.Label
tooltip={t(
"orders.claims.tooltips.onlyReturnShippingOptions"
)}
>
{t("orders.returns.inboundShipping")}
</Form.Label>

<Form.Hint className="!mt-1">
{t("orders.returns.inboundShippingHint")}
</Form.Hint>
Expand All @@ -595,23 +634,10 @@ export const ClaimCreateForm = ({
val && onShippingOptionChange(val)
}}
{...field}
options={(shipping_options ?? [])
.filter(
(so) =>
(locationId
? so.service_zone.fulfillment_set!
.location.id === locationId
: true) &&
!!so.rules.find(
(r) =>
r.attribute === "is_return" &&
r.value === "true"
)
)
.map((so) => ({
label: so.name,
value: so.id,
}))}
options={inboundShippingOptions.map((so) => ({
label: so.name,
value: so.id,
}))}
disabled={!locationId}
/>
</Form.Control>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
useUpdateClaimOutboundItems,
} from "../../../../../hooks/api/claims"
import { useShippingOptions } from "../../../../../hooks/api/shipping-options"
import { useStockLocations } from "../../../../../hooks/api/stock-locations"
import { sdk } from "../../../../../lib/client"
import { AddClaimOutboundItemsTable } from "../add-claim-outbound-items-table"
import { ClaimOutboundItem } from "./claim-outbound-item"
Expand Down Expand Up @@ -57,13 +56,9 @@ export const ClaimOutboundSection = ({
/**
* HOOKS
*/
const { stock_locations = [] } = useStockLocations({ limit: 999 })
const { shipping_options = [] } = useShippingOptions({
limit: 999,
fields: "*prices,+service_zone.fulfillment_set.location.id",
/**
* TODO: this should accept filter for location_id
*/
})

const { mutateAsync: addOutboundShipping } = useAddClaimOutboundShipping(
Expand Down Expand Up @@ -154,17 +149,6 @@ export const ClaimOutboundSection = ({
})
}, [previewOutboundItems])

useEffect(() => {
// TODO: Pick the shipping methods from actions where return_id is null for outbound
const method = preview.shipping_methods.find(
(s) => !!s.actions?.find((a) => a.action === "SHIPPING_ADD")
)

if (method) {
form.setValue("outbound_option_id", method.shipping_option_id)
}
}, [preview.shipping_methods])

const locationId = form.watch("location_id")
const showOutboundItemsPlaceholder = !outboundItems.length

Expand Down Expand Up @@ -201,17 +185,16 @@ export const ClaimOutboundSection = ({
setIsOpen("outbound-items", false)
}

// TODO: implement outbound shipping
const { mutateAsync: updateClaimRequest, isPending: isUpdating } = {} // useUpdateClaim(claim.id, order.id)
const onLocationChange = async (selectedLocationId?: string | null) => {
await updateClaimRequest({ location_id: selectedLocationId })
}

const onShippingOptionChange = async (selectedOptionId: string) => {
const promises = preview.shipping_methods
.map((s) => s.actions?.find((a) => a.action === "SHIPPING_ADD")?.id)
const outboundShippingMethods = preview.shipping_methods.filter((s) => {
const action = s.actions?.find((a) => a.action === "SHIPPING_ADD")

return action && !!!action?.return?.id
})

const promises = outboundShippingMethods
.filter(Boolean)
.map(deleteOutboundShipping)
.map((action) => deleteOutboundShipping(action.id))

await Promise.all(promises)

Expand Down Expand Up @@ -270,7 +253,7 @@ export const ClaimOutboundSection = ({
).variants

variants.forEach((variant) => {
ret[variant.id] = variant.inventory[0]?.location_levels || []
ret[variant.id] = variant.inventory?.[0]?.location_levels || []
})

return ret
Expand Down Expand Up @@ -403,24 +386,11 @@ export const ClaimOutboundSection = ({
val && onShippingOptionChange(val)
}}
{...field}
options={(shipping_options ?? [])
.filter(
(so) =>
(locationId
? so.service_zone.fulfillment_set!.location
.id === locationId
: true) &&
!!so.rules.find(
(r) =>
r.attribute === "is_return" &&
r.value === "true"
)
)
.map((so) => ({
label: so.name,
value: so.id,
}))}
disabled={!locationId}
options={shipping_options.map((so) => ({
label: so.name,
value: so.id,
}))}
disabled={!shipping_options.length}
/>
</Form.Control>
</Form.Item>
Expand Down
3 changes: 2 additions & 1 deletion packages/core/types/src/http/return/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface AdminReturn {
order_id: string
status?: string
exchange_id?: string
location_id?: string
claim_id?: string
order_version: number
display_id: number
Expand Down Expand Up @@ -79,7 +80,7 @@ export interface AdminConfirmReturnRequest {
}

export interface AdminUpdateReturnRequest {
location_id?: string
location_id?: string | null
no_notification?: boolean
metadata?: Record<string, unknown> | null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface AdminShippingOptionListParams extends FindParams {
id?: string | string[]
q?: string
service_zone_id?: string
stock_location_id?: string | string[]
shipping_profile_id?: string
provider_id?: string
shipping_option_type_id?: string
Expand Down

0 comments on commit 3fe5848

Please sign in to comment.