refactor(order): move auto-dispatch to store with role-based action visibility
This commit is contained in:
@@ -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,42 +107,43 @@ export default function OrderActions({
|
||||
|
||||
{status === "pending_accept" && (
|
||||
<>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = cancelPreAccept(orderId)
|
||||
handleDecision("订单已取消", result)
|
||||
}}
|
||||
>
|
||||
<XCircle className="mr-1 h-4 w-4" />
|
||||
取消订单
|
||||
</Button>
|
||||
{order?.shopId && dispatchMode === "auto" ? (
|
||||
<Button disabled>
|
||||
<Clock className="mr-1 h-4 w-4" />
|
||||
系统派单中
|
||||
</Button>
|
||||
) : (
|
||||
{isConsumer && (
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = acceptOrder(orderId)
|
||||
handleDecision("已接单", result)
|
||||
const result = cancelPreAccept(orderId)
|
||||
handleDecision("订单已取消", result)
|
||||
}}
|
||||
>
|
||||
<CheckCircle2 className="mr-1 h-4 w-4" />
|
||||
接单
|
||||
<XCircle className="mr-1 h-4 w-4" />
|
||||
取消订单
|
||||
</Button>
|
||||
)}
|
||||
{isPlayer &&
|
||||
(order?.shopId && dispatchMode === "auto" ? (
|
||||
<Button disabled>
|
||||
<Clock className="mr-1 h-4 w-4" />
|
||||
系统派单中
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
const result = acceptOrder(orderId)
|
||||
handleDecision("已接单", result)
|
||||
}}
|
||||
>
|
||||
<CheckCircle2 className="mr-1 h-4 w-4" />
|
||||
接单
|
||||
</Button>
|
||||
))}
|
||||
</>
|
||||
)}
|
||||
|
||||
@@ -202,19 +182,20 @@ export default function OrderActions({
|
||||
|
||||
{status === "pending_close" && (
|
||||
<>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = confirmClose(orderId)
|
||||
handleDecision("已确认结单", result)
|
||||
}}
|
||||
>
|
||||
确认结单
|
||||
</Button>
|
||||
{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" />
|
||||
|
||||
Reference in New Issue
Block a user