fix(dashboard, medusa): mark shipped flow (#8065)

* fix: mark shipped routing

* fix: naming
This commit is contained in:
Frane Polić
2024-07-10 19:37:12 +02:00
committed by GitHub
parent e778870c68
commit b289510b46
11 changed files with 87 additions and 19 deletions
@@ -43,10 +43,10 @@ export const useCancelFulfillment = (
})
}
export const useCreateShipment = (
export const useCreateFulfillmentShipment = (
fulfillmentId: string,
options?: UseMutationOptions<
{ order: HttpTypes.AdminOrder },
{ fulfillment: HttpTypes.AdminFulfillment },
Error,
HttpTypes.AdminCreateFulfillmentShipment
>
@@ -9,6 +9,7 @@ import {
import { queryClient } from "../../lib/query-client"
import { queryKeysFactory } from "../../lib/query-key-factory"
import { sdk } from "../../lib/client"
import { AdminCreateOrderShipment, HttpTypes } from "@medusajs/types"
const ORDERS_QUERY_KEY = "orders" as const
export const ordersQueryKeys = queryKeysFactory(ORDERS_QUERY_KEY)
@@ -81,6 +82,28 @@ export const useCancelOrderFulfillment = (
})
}
export const useCreateOrderShipment = (
orderId: string,
fulfillmentId: string,
options?: UseMutationOptions<
{ order: HttpTypes.AdminOrder },
Error,
HttpTypes.AdminCreateOrderShipment
>
) => {
return useMutation({
mutationFn: (payload: HttpTypes.AdminCreateOrderShipment) =>
sdk.admin.order.createShipment(orderId, fulfillmentId, payload),
onSuccess: (data: any, variables: any, context: any) => {
queryClient.invalidateQueries({
queryKey: ordersQueryKeys.details(),
})
options?.onSuccess?.(data, variables, context)
},
...options,
})
}
export const useCancelOrder = (
orderId: string,
options?: UseMutationOptions<any, Error, any>
@@ -1,15 +1,13 @@
import { z } from "zod"
export const CreateShipmentSchema = z.object({
labels: z
.array(
z.object({
tracking_number: z.string(),
// TODO: this 2 are not optional in the API
tracking_url: z.string().optional(),
label_url: z.string().optional(),
})
)
.min(1),
labels: z.array(
z.object({
tracking_number: z.string(),
// TODO: this 2 are not optional in the API
tracking_url: z.string().optional(),
label_url: z.string().optional(),
})
),
send_notification: z.boolean().optional(),
})
@@ -11,8 +11,8 @@ import {
RouteFocusModal,
useRouteModal,
} from "../../../../../components/modals"
import { useCreateShipment } from "../../../../../hooks/api/fulfillment.tsx"
import { CreateShipmentSchema } from "./constants"
import { useCreateOrderShipment } from "../../../../../hooks/api"
type OrderCreateFulfillmentFormProps = {
order: AdminOrder
@@ -27,12 +27,11 @@ export function OrderCreateShipmentForm({
const { handleSuccess } = useRouteModal()
const { mutateAsync: createShipment, isPending: isMutating } =
useCreateShipment(fulfillment.id)
useCreateOrderShipment(order.id, fulfillment.id)
const form = useForm<zod.infer<typeof CreateShipmentSchema>>({
defaultValues: {
labels: [{ tracking_number: "" }],
send_notification: !order.no_notification, // TODO: not supported in the API
send_notification: !order.no_notification,
},
resolver: zodResolver(CreateShipmentSchema),
})
@@ -45,6 +44,10 @@ export function OrderCreateShipmentForm({
const handleSubmit = form.handleSubmit(async (data) => {
await createShipment(
{
items: fulfillment.items.map((i) => ({
id: i.line_item_id,
quantity: i.quantity,
})),
labels: data.labels
.filter((l) => !!l.tracking_number)
.map((l) => ({
@@ -52,7 +55,7 @@ export function OrderCreateShipmentForm({
tracking_url: "#",
label_url: "#",
})),
// no_notification: !data.send_notification,
no_notification: !data.send_notification,
},
{
onSuccess: () => {
@@ -8,7 +8,7 @@ export function OrderCreateShipment() {
const { id, f_id } = useParams()
const { order, isLoading, isError, error } = useOrder(id!, {
fields: "*fulfillments",
fields: "*fulfillments,*fulfillments.items",
})
if (isError) {