refactor(order): move auto-dispatch to store with role-based action visibility

This commit is contained in:
zetaloop
2026-02-23 11:10:20 +08:00
parent 90f1245ff1
commit 629a121272
+11 -30
View File
@@ -10,17 +10,15 @@ import {
XCircle,
} from "lucide-react"
import Link from "next/link"
import { useCallback, useEffect } from "react"
import { useCallback } from "react"
import { Button } from "@/components/ui/button"
import {
acceptOrder,
acceptOrderAsActor,
cancelPreAccept,
confirmClose,
payOrder,
requestClose,
} from "@/lib/api/orders"
import type { Actor } from "@/lib/policy/actor"
import { notifyInfo, notifySuccess } from "@/lib/toast"
import type { OrderStatus } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
@@ -58,6 +56,8 @@ export default function OrderActions({
sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id
const status = order?.status ?? initialStatus
const isConsumer = order?.consumerId === currentUserId
const isPlayer = order?.playerId === currentUserId
const handleDecision = useCallback(
(okMessage: string, result: { decision: { ok: boolean; message?: string } }) => {
@@ -71,28 +71,9 @@ export default function OrderActions({
[],
)
useEffect(() => {
if (!order) return
if (order.status !== "pending_accept") return
if (!order.shopId) return
if (dispatchMode !== "auto") return
const timer = setTimeout(() => {
const systemActor: Actor = {
userId: order.playerId,
role: "player",
shopId: order.shopId,
}
const result = acceptOrderAsActor(orderId, systemActor)
handleDecision("系统已自动派单", result)
}, 3000)
return () => clearTimeout(timer)
}, [dispatchMode, handleDecision, order, orderId])
return (
<div className="flex gap-2 flex-wrap">
{status === "pending_payment" && (
{status === "pending_payment" && isConsumer && (
<>
<Button
variant="outline"
@@ -101,7 +82,6 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = cancelPreAccept(orderId)
handleDecision("订单已取消", result)
}}
@@ -115,7 +95,6 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = payOrder(orderId)
handleDecision("订单支付成功", result)
}}
@@ -128,6 +107,7 @@ export default function OrderActions({
{status === "pending_accept" && (
<>
{isConsumer && (
<Button
variant="outline"
onClick={() => {
@@ -135,7 +115,6 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = cancelPreAccept(orderId)
handleDecision("订单已取消", result)
}}
@@ -143,7 +122,9 @@ export default function OrderActions({
<XCircle className="mr-1 h-4 w-4" />
</Button>
{order?.shopId && dispatchMode === "auto" ? (
)}
{isPlayer &&
(order?.shopId && dispatchMode === "auto" ? (
<Button disabled>
<Clock className="mr-1 h-4 w-4" />
@@ -155,7 +136,6 @@ export default function OrderActions({
notifyInfo("请先登录")
return
}
const result = acceptOrder(orderId)
handleDecision("已接单", result)
}}
@@ -163,7 +143,7 @@ export default function OrderActions({
<CheckCircle2 className="mr-1 h-4 w-4" />
</Button>
)}
))}
</>
)}
@@ -202,19 +182,20 @@ export default function OrderActions({
{status === "pending_close" && (
<>
{isConsumer && (
<Button
onClick={() => {
if (!currentUserId) {
notifyInfo("请先登录")
return
}
const result = confirmClose(orderId)
handleDecision("已确认结单", result)
}}
>
</Button>
)}
<Button variant="destructive" asChild>
<Link href={`/dispute/${orderId}`}>
<AlertTriangle className="mr-1 h-4 w-4" />