From 0da9445d80c7da570135b9f6cfa9fcdde7e7991e Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Wed, 21 Aug 2024 16:23:49 +0200 Subject: [PATCH] fix(dashboard): bust order preview cache to reset fulfilled quantity (#8687) --- .../dashboard/src/hooks/api/claims.tsx | 90 +++++++++++++++---- .../dashboard/src/hooks/api/exchanges.tsx | 19 +--- .../dashboard/src/hooks/api/fulfillment.tsx | 2 +- .../dashboard/src/hooks/api/orders.tsx | 21 ++++- .../src/hooks/api/payment-collections.tsx | 21 ++++- .../dashboard/src/hooks/api/payments.tsx | 14 ++- .../dashboard/src/hooks/api/returns.tsx | 88 ++++++++++++++---- .../dashboard/src/lib/query-key-factory.ts | 2 +- .../order-create-fulfillment-form.tsx | 73 +++++++-------- .../create-refund-form/create-refund-form.tsx | 2 +- .../order-general-section.tsx | 2 +- .../order-payment-section.tsx | 2 +- .../order-summary-section.tsx | 1 + 13 files changed, 235 insertions(+), 102 deletions(-) diff --git a/packages/admin-next/dashboard/src/hooks/api/claims.tsx b/packages/admin-next/dashboard/src/hooks/api/claims.tsx index 7851ca366ca01..cf39c8cd3f07f 100644 --- a/packages/admin-next/dashboard/src/hooks/api/claims.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/claims.tsx @@ -74,9 +74,6 @@ export const useCreateClaim = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -103,9 +100,6 @@ export const useCancelClaim = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -135,9 +129,6 @@ export const useDeleteClaim = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -168,9 +159,14 @@ export const useAddClaimItems = ( mutationFn: (payload: HttpTypes.AdminAddClaimItems) => sdk.admin.claim.addItems(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -194,9 +190,14 @@ export const useUpdateClaimItems = ( return sdk.admin.claim.updateItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -212,9 +213,14 @@ export const useRemoveClaimItem = ( mutationFn: (actionId: string) => sdk.admin.return.removeReturnItem(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -234,9 +240,14 @@ export const useAddClaimInboundItems = ( mutationFn: (payload: HttpTypes.AdminAddClaimInboundItems) => sdk.admin.claim.addInboundItems(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -260,9 +271,14 @@ export const useUpdateClaimInboundItem = ( return sdk.admin.claim.updateInboundItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -279,10 +295,11 @@ export const useRemoveClaimInboundItem = ( sdk.admin.claim.removeInboundItem(id, actionId), onSuccess: (data: any, variables: any, context: any) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.preview(orderId), + queryKey: ordersQueryKeys.details(), }) + queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.preview(orderId), }) options?.onSuccess?.(data, variables, context) @@ -304,9 +321,14 @@ export const useAddClaimInboundShipping = ( mutationFn: (payload: HttpTypes.AdminClaimAddInboundShipping) => sdk.admin.claim.addInboundShipping(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -329,9 +351,14 @@ export const useUpdateClaimInboundShipping = ( }: HttpTypes.AdminClaimUpdateInboundShipping & { actionId: string }) => sdk.admin.claim.updateInboundShipping(id, actionId, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -347,9 +374,14 @@ export const useDeleteClaimInboundShipping = ( mutationFn: (actionId: string) => sdk.admin.claim.deleteInboundShipping(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -369,9 +401,14 @@ export const useAddClaimOutboundItems = ( mutationFn: (payload: HttpTypes.AdminAddClaimOutboundItems) => sdk.admin.claim.addOutboundItems(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -395,9 +432,14 @@ export const useUpdateClaimOutboundItems = ( return sdk.admin.claim.updateOutboundItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -413,9 +455,14 @@ export const useRemoveClaimOutboundItem = ( mutationFn: (actionId: string) => sdk.admin.claim.removeOutboundItem(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -435,9 +482,14 @@ export const useAddClaimOutboundShipping = ( mutationFn: (payload: HttpTypes.AdminClaimAddOutboundShipping) => sdk.admin.claim.addOutboundShipping(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -460,9 +512,14 @@ export const useUpdateClaimOutboundShipping = ( }: HttpTypes.AdminClaimUpdateOutboundShipping & { actionId: string }) => sdk.admin.claim.updateOutboundShipping(id, actionId, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -478,9 +535,14 @@ export const useDeleteClaimOutboundShipping = ( mutationFn: (actionId: string) => sdk.admin.claim.deleteOutboundShipping(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -507,9 +569,6 @@ export const useClaimConfirmRequest = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -536,9 +595,6 @@ export const useCancelClaimRequest = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), diff --git a/packages/admin-next/dashboard/src/hooks/api/exchanges.tsx b/packages/admin-next/dashboard/src/hooks/api/exchanges.tsx index 372a7c76a19ec..7ad5c63bf16dc 100644 --- a/packages/admin-next/dashboard/src/hooks/api/exchanges.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/exchanges.tsx @@ -75,10 +75,6 @@ export const useCreateExchange = ( queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) - queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) @@ -104,9 +100,6 @@ export const useCancelExchange = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -136,9 +129,6 @@ export const useDeleteExchange = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -283,7 +273,7 @@ export const useRemoveExchangeInboundItem = ( queryKey: ordersQueryKeys.preview(orderId), }) queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), }) options?.onSuccess?.(data, variables, context) @@ -509,10 +499,6 @@ export const useExchangeConfirmRequest = ( queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) - queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) @@ -538,9 +524,6 @@ export const useCancelExchangeRequest = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), diff --git a/packages/admin-next/dashboard/src/hooks/api/fulfillment.tsx b/packages/admin-next/dashboard/src/hooks/api/fulfillment.tsx index f5dd586c1f54f..77ff34bbac64a 100644 --- a/packages/admin-next/dashboard/src/hooks/api/fulfillment.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/fulfillment.tsx @@ -2,10 +2,10 @@ import { useMutation, UseMutationOptions } from "@tanstack/react-query" import { queryKeysFactory } from "../../lib/query-key-factory" +import { HttpTypes } from "@medusajs/types" import { client, sdk } from "../../lib/client" import { queryClient } from "../../lib/query-client" import { ordersQueryKeys } from "./orders" -import { HttpTypes } from "@medusajs/types" const FULFILLMENTS_QUERY_KEY = "fulfillments" as const export const fulfillmentsQueryKeys = queryKeysFactory(FULFILLMENTS_QUERY_KEY) diff --git a/packages/admin-next/dashboard/src/hooks/api/orders.tsx b/packages/admin-next/dashboard/src/hooks/api/orders.tsx index 533658be4ccdc..767515d0e715a 100644 --- a/packages/admin-next/dashboard/src/hooks/api/orders.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/orders.tsx @@ -9,10 +9,16 @@ import { import { HttpTypes } from "@medusajs/types" import { sdk } from "../../lib/client" import { queryClient } from "../../lib/query-client" -import { queryKeysFactory } from "../../lib/query-key-factory" +import { queryKeysFactory, TQueryKey } from "../../lib/query-key-factory" const ORDERS_QUERY_KEY = "orders" as const -const _orderKeys = queryKeysFactory(ORDERS_QUERY_KEY) +const _orderKeys = queryKeysFactory(ORDERS_QUERY_KEY) as TQueryKey< + "orders", + any, + string +> & { + preview: (orderId: string) => any +} _orderKeys.preview = function (id: string) { return [this.detail(id), "preview"] @@ -94,6 +100,10 @@ export const useCreateOrderFulfillment = ( queryKey: ordersQueryKeys.preview(orderId), }) + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), + }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -151,13 +161,18 @@ export const useCreateOrderShipment = ( } export const useCancelOrder = ( + orderId: string, options?: UseMutationOptions ) => { return useMutation({ mutationFn: (id) => sdk.admin.order.cancel(id), onSuccess: (data: any, variables: any, context: any) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) options?.onSuccess?.(data, variables, context) diff --git a/packages/admin-next/dashboard/src/hooks/api/payment-collections.tsx b/packages/admin-next/dashboard/src/hooks/api/payment-collections.tsx index 557ff4d457fa9..c8ca4b56a259c 100644 --- a/packages/admin-next/dashboard/src/hooks/api/payment-collections.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/payment-collections.tsx @@ -12,6 +12,7 @@ export const paymentCollectionQueryKeys = queryKeysFactory( ) export const useCreatePaymentCollection = ( + orderId: string, options?: UseMutationOptions< HttpTypes.AdminPaymentCollectionResponse, Error, @@ -22,7 +23,11 @@ export const useCreatePaymentCollection = ( mutationFn: (payload) => sdk.admin.paymentCollection.create(payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) queryClient.invalidateQueries({ @@ -36,6 +41,7 @@ export const useCreatePaymentCollection = ( } export const useMarkPaymentCollectionAsPaid = ( + orderId: string, paymentCollectionId: string, options?: UseMutationOptions< HttpTypes.AdminPaymentCollectionResponse, @@ -48,7 +54,11 @@ export const useMarkPaymentCollectionAsPaid = ( sdk.admin.paymentCollection.markAsPaid(paymentCollectionId, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) queryClient.invalidateQueries({ @@ -62,6 +72,7 @@ export const useMarkPaymentCollectionAsPaid = ( } export const useDeletePaymentCollection = ( + orderId: string, options?: Omit< UseMutationOptions< HttpTypes.AdminDeletePaymentCollectionResponse, @@ -75,7 +86,11 @@ export const useDeletePaymentCollection = ( mutationFn: (id: string) => sdk.admin.paymentCollection.delete(id), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) queryClient.invalidateQueries({ diff --git a/packages/admin-next/dashboard/src/hooks/api/payments.tsx b/packages/admin-next/dashboard/src/hooks/api/payments.tsx index 6fb272de96a16..7f42733fb3f84 100644 --- a/packages/admin-next/dashboard/src/hooks/api/payments.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/payments.tsx @@ -59,6 +59,7 @@ export const usePayment = ( } export const useCapturePayment = ( + orderId: string, paymentId: string, options?: UseMutationOptions< HttpTypes.AdminPaymentResponse, @@ -70,7 +71,11 @@ export const useCapturePayment = ( mutationFn: (payload) => sdk.admin.payment.capture(paymentId, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) options?.onSuccess?.(data, variables, context) @@ -80,6 +85,7 @@ export const useCapturePayment = ( } export const useRefundPayment = ( + orderId: string, paymentId: string, options?: UseMutationOptions< HttpTypes.AdminPaymentResponse, @@ -91,7 +97,11 @@ export const useRefundPayment = ( mutationFn: (payload) => sdk.admin.payment.refund(paymentId, payload), onSuccess: (data, variables, context) => { queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.all, + queryKey: ordersQueryKeys.details(), + }) + + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.preview(orderId), }) options?.onSuccess?.(data, variables, context) diff --git a/packages/admin-next/dashboard/src/hooks/api/returns.tsx b/packages/admin-next/dashboard/src/hooks/api/returns.tsx index a0d39fc8325ee..3242f4ea77771 100644 --- a/packages/admin-next/dashboard/src/hooks/api/returns.tsx +++ b/packages/admin-next/dashboard/src/hooks/api/returns.tsx @@ -1,3 +1,4 @@ +import { HttpTypes } from "@medusajs/types" import { QueryKey, useMutation, @@ -5,12 +6,11 @@ import { useQuery, UseQueryOptions, } from "@tanstack/react-query" -import { HttpTypes } from "@medusajs/types" import { sdk } from "../../lib/client" import { queryClient } from "../../lib/query-client" -import { ordersQueryKeys } from "./orders" import { queryKeysFactory } from "../../lib/query-key-factory" +import { ordersQueryKeys } from "./orders" const RETURNS_QUERY_KEY = "returns" as const export const returnsQueryKeys = queryKeysFactory(RETURNS_QUERY_KEY) @@ -72,8 +72,9 @@ export const useInitiateReturn = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) + queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), + queryKey: ordersQueryKeys.preview(orderId), }) queryClient.invalidateQueries({ @@ -101,9 +102,6 @@ export const useConfirmReturnRequest = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -132,9 +130,6 @@ export const useCancelReturnRequest = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -166,9 +161,14 @@ export const useAddReturnItem = ( mutationFn: (payload: HttpTypes.AdminAddReturnItems) => sdk.admin.return.addReturnItem(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -192,9 +192,14 @@ export const useUpdateReturnItem = ( return sdk.admin.return.updateReturnItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -210,9 +215,14 @@ export const useRemoveReturnItem = ( mutationFn: (actionId: string) => sdk.admin.return.removeReturnItem(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -233,9 +243,14 @@ export const useUpdateReturn = ( return sdk.admin.return.updateRequest(id, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -255,9 +270,14 @@ export const useAddReturnShipping = ( mutationFn: (payload: HttpTypes.AdminAddReturnShipping) => sdk.admin.return.addReturnShipping(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -280,9 +300,14 @@ export const useUpdateReturnShipping = ( }: HttpTypes.AdminAddReturnShipping & { actionId: string }) => sdk.admin.return.updateReturnShipping(id, actionId, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -298,9 +323,14 @@ export const useDeleteReturnShipping = ( mutationFn: (actionId: string) => sdk.admin.return.deleteReturnShipping(id, actionId), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -327,13 +357,11 @@ export const useInitiateReceiveReturn = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -353,9 +381,14 @@ export const useAddReceiveItems = ( mutationFn: (payload: HttpTypes.AdminReceiveItems) => sdk.admin.return.receiveItems(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -379,9 +412,14 @@ export const useUpdateReceiveItem = ( return sdk.admin.return.updateReceiveItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -398,9 +436,14 @@ export const useRemoveReceiveItems = ( return sdk.admin.return.removeReceiveItem(id, actionId) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -420,9 +463,14 @@ export const useAddDismissItems = ( mutationFn: (payload: HttpTypes.AdminDismissItems) => sdk.admin.return.dismissItems(id, payload), onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -446,9 +494,14 @@ export const useUpdateDismissItem = ( return sdk.admin.return.updateDismissItem(id, actionId, payload) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -465,9 +518,14 @@ export const useRemoveDismissItem = ( return sdk.admin.return.removeDismissItem(id, actionId) }, onSuccess: (data: any, variables: any, context: any) => { + queryClient.invalidateQueries({ + queryKey: ordersQueryKeys.details(), + }) + queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), }) + options?.onSuccess?.(data, variables, context) }, ...options, @@ -490,9 +548,6 @@ export const useConfirmReturnReceive = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), @@ -521,9 +576,6 @@ export const useCancelReceiveReturn = ( queryClient.invalidateQueries({ queryKey: ordersQueryKeys.details(), }) - queryClient.invalidateQueries({ - queryKey: ordersQueryKeys.lists(), - }) queryClient.invalidateQueries({ queryKey: ordersQueryKeys.preview(orderId), diff --git a/packages/admin-next/dashboard/src/lib/query-key-factory.ts b/packages/admin-next/dashboard/src/lib/query-key-factory.ts index 7b3970df1da31..cf477d387a74a 100644 --- a/packages/admin-next/dashboard/src/lib/query-key-factory.ts +++ b/packages/admin-next/dashboard/src/lib/query-key-factory.ts @@ -1,6 +1,6 @@ import { QueryKey, UseQueryOptions } from "@tanstack/react-query" -type TQueryKey = { +export type TQueryKey = { all: readonly [TKey] lists: () => readonly [...TQueryKey["all"], "list"] list: ( diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-fulfillment/components/order-create-fulfillment-form/order-create-fulfillment-form.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-fulfillment/components/order-create-fulfillment-form/order-create-fulfillment-form.tsx index 5148559fa119c..6bd6eba336b3b 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-fulfillment/components/order-create-fulfillment-form/order-create-fulfillment-form.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-fulfillment/components/order-create-fulfillment-form/order-create-fulfillment-form.tsx @@ -13,7 +13,6 @@ import { RouteFocusModal, useRouteModal, } from "../../../../../components/modals" -import { useFulfillmentProviders } from "../../../../../hooks/api/fulfillment-providers" import { useCreateOrderFulfillment } from "../../../../../hooks/api/orders" import { useStockLocations } from "../../../../../hooks/api/stock-locations" import { getFulfillableQuantity } from "../../../../../lib/order-item" @@ -33,12 +32,8 @@ export function OrderCreateFulfillmentForm({ const { mutateAsync: createOrderFulfillment, isPending: isMutating } = useCreateOrderFulfillment(order.id) - const { fulfillment_providers } = useFulfillmentProviders({ - region_id: order.region_id, - }) - const [fulfillableItems, setFulfillableItems] = useState(() => - order.items.filter((item) => getFulfillableQuantity(item) > 0) + (order.items || []).filter((item) => getFulfillableQuantity(item) > 0) ) const form = useForm>({ @@ -89,7 +84,9 @@ export function OrderCreateFulfillmentForm({ } const resetItems = () => { - const items = order.items.filter((item) => getFulfillableQuantity(item) > 0) + const items = (order.items || []).filter( + (item) => getFulfillableQuantity(item) > 0 + ) setFulfillableItems(items) items.forEach((i) => @@ -103,34 +100,35 @@ export function OrderCreateFulfillmentForm({ control: form.control, }) + const fulfilledQuantityArray = (order.items || []).map( + (item) => item.detail.fulfilled_quantity + ) + useEffect(() => { - if (!fulfillableItems.length) { + const itemsToFulfill = + order?.items?.filter((item) => getFulfillableQuantity(item) > 0) || [] + + setFulfillableItems(itemsToFulfill) + + if (itemsToFulfill.length) { + form.clearErrors("root") + } else { form.setError("root", { type: "manual", message: t("orders.fulfillment.error.noItems"), }) } - }, [fulfillableItems.length]) - useEffect(() => { - const itemsToFulfill = order?.items?.filter( - (item) => getFulfillableQuantity(item) > 0 + const quantityMap = itemsToFulfill.reduce( + (acc, item) => { + acc[item.id] = getFulfillableQuantity(item as OrderLineItemDTO) + return acc + }, + {} as Record ) - if (itemsToFulfill?.length) { - setFulfillableItems(itemsToFulfill) - - const quantityMap = fulfillableItems.reduce( - (acc, item) => { - acc[item.id] = getFulfillableQuantity(item as OrderLineItemDTO) - return acc - }, - {} as Record - ) - - form.setValue("quantity", quantityMap) - } - }, [order.items?.length]) + form.setValue("quantity", quantityMap) + }, [...fulfilledQuantityArray]) return ( @@ -150,6 +148,7 @@ export function OrderCreateFulfillmentForm({ +
@@ -197,16 +196,18 @@ export function OrderCreateFulfillmentForm({
- {fulfillableItems.map((item) => ( - - ))} + {fulfillableItems.map((item) => { + return ( + + ) + })}
{form.formState.errors.root && ( diff --git a/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx b/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx index 4e9a1b788ac2f..bc183d54faeee 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-create-refund/components/create-refund-form/create-refund-form.tsx @@ -67,7 +67,7 @@ export const CreateRefundForm = ({ form.setValue("amount", normalizedAmount as number) }, [payment]) - const { mutateAsync, isPending } = useRefundPayment(payment?.id!) + const { mutateAsync, isPending } = useRefundPayment(order.id, payment?.id!) const handleSubmit = form.handleSubmit(async (data) => { await mutateAsync( diff --git a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-general-section/order-general-section.tsx b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-general-section/order-general-section.tsx index 3e7c8cdf87450..e4dd017e91a79 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-general-section/order-general-section.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-general-section/order-general-section.tsx @@ -25,7 +25,7 @@ export const OrderGeneralSection = ({ order }: OrderGeneralSectionProps) => { const { t } = useTranslation() const prompt = usePrompt() - const { mutateAsync: cancelOrder } = useCancelOrder() + const { mutateAsync: cancelOrder } = useCancelOrder(order.id) const handleCancel = async () => { const res = await prompt({ diff --git a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-payment-section/order-payment-section.tsx b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-payment-section/order-payment-section.tsx index 97478f3663dfe..997ebf36571d0 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-payment-section/order-payment-section.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-payment-section/order-payment-section.tsx @@ -142,7 +142,7 @@ const Payment = ({ }) => { const { t } = useTranslation() const prompt = usePrompt() - const { mutateAsync } = useCapturePayment(payment.id) + const { mutateAsync } = useCapturePayment(order.id, payment.id) const handleCapture = async () => { const res = await prompt({ diff --git a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-summary-section/order-summary-section.tsx b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-summary-section/order-summary-section.tsx index 5d6e5a8e4856a..475cbc9819f9e 100644 --- a/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-summary-section/order-summary-section.tsx +++ b/packages/admin-next/dashboard/src/routes/orders/order-detail/components/order-summary-section/order-summary-section.tsx @@ -111,6 +111,7 @@ export const OrderSummarySection = ({ order }: OrderSummarySectionProps) => { ) const { mutateAsync: markAsPaid } = useMarkPaymentCollectionAsPaid( + order.id, unpaidPaymentCollection?.id! )