feat(orders): migrate orders to backend API

This commit is contained in:
zetaloop
2026-02-28 18:13:42 +08:00
parent e94a7e68ff
commit 9739c94bdc
9 changed files with 263 additions and 130 deletions
+30 -15
View File
@@ -10,7 +10,7 @@ import {
} from "@/lib/api/orders"
import type { ApiDecision } from "@/lib/errors"
import { notifyInfo, notifySuccess } from "@/lib/toast"
import type { OrderStatus } from "@/lib/types"
import type { Order, OrderStatus } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
import { useChatStore } from "@/store/chat"
import { useOrderStore } from "@/store/orders"
@@ -29,6 +29,8 @@ import { useCallback } from "react"
interface OrderActionsProps {
orderId: string
order?: Order
onOrderChange?: (order: Order) => void
initialStatus: OrderStatus
chatSessionId?: string
serviceId: string
@@ -40,12 +42,15 @@ function showFeedback(message: string) {
export default function OrderActions({
orderId,
order: orderProp,
onOrderChange,
initialStatus,
chatSessionId,
serviceId,
}: OrderActionsProps) {
const currentUserId = useAuthStore((state) => state.user?.id)
const order = useOrderStore((state) => state.orders.find((item) => item.id === orderId))
const storeOrder = useOrderStore((state) => state.orders.find((item) => item.id === orderId))
const order = orderProp ?? storeOrder
const sessions = useChatStore((state) => state.sessions)
const dispatchMode = useShopStore((state) => {
if (!order?.shopId) return "manual"
@@ -60,7 +65,9 @@ export default function OrderActions({
const isConsumer = order?.consumerId === currentUserId
const isPlayer = order?.playerId === currentUserId
const handleDecision = useCallback((okMessage: string, result: { decision: ApiDecision }) => {
type ActionResult = { decision: ApiDecision; order?: Order }
const handleDecision = useCallback((okMessage: string, result: ActionResult) => {
if (result.decision.ok) {
showFeedback(okMessage)
return
@@ -69,6 +76,20 @@ export default function OrderActions({
notifyInfo(result.decision.error.msg)
}, [])
const runAction = useCallback(
(okMessage: string, actionCall: ActionResult | Promise<ActionResult>) => {
Promise.resolve(actionCall)
.then((result) => {
handleDecision(okMessage, result)
if (result.order) onOrderChange?.(result.order)
})
.catch(() => {
notifyInfo("操作失败")
})
},
[handleDecision, onOrderChange],
)
return (
<div className="flex gap-2 flex-wrap">
{status === "pending_payment" && isConsumer && (
@@ -80,8 +101,7 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = cancelPreAccept(orderId)
handleDecision("订单已取消", result)
runAction("订单已取消", cancelPreAccept(orderId))
}}
>
<XCircle className="mr-1 h-4 w-4" />
@@ -93,8 +113,7 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = payOrder(orderId)
handleDecision("订单支付成功", result)
runAction("订单支付成功", payOrder(orderId))
}}
>
<CheckCircle2 className="mr-1 h-4 w-4" />
@@ -113,8 +132,7 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = cancelPreAccept(orderId)
handleDecision("订单已取消", result)
runAction("订单已取消", cancelPreAccept(orderId))
}}
>
<XCircle className="mr-1 h-4 w-4" />
@@ -134,8 +152,7 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = acceptOrder(orderId)
handleDecision("已接单", result)
runAction("已接单", acceptOrder(orderId))
}}
>
<CheckCircle2 className="mr-1 h-4 w-4" />
@@ -163,8 +180,7 @@ export default function OrderActions({
return
}
const result = requestClose(orderId)
handleDecision("已发起结单", result)
runAction("已发起结单", requestClose(orderId))
}}
>
@@ -187,8 +203,7 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = confirmClose(orderId)
handleDecision("已确认结单", result)
runAction("已确认结单", confirmClose(orderId))
}}
>