refactor(order): move auto-dispatch to store with role-based action visibility
This commit is contained in:
@@ -10,17 +10,15 @@ import {
|
|||||||
XCircle,
|
XCircle,
|
||||||
} from "lucide-react"
|
} from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
import { useCallback, useEffect } from "react"
|
import { useCallback } from "react"
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import {
|
import {
|
||||||
acceptOrder,
|
acceptOrder,
|
||||||
acceptOrderAsActor,
|
|
||||||
cancelPreAccept,
|
cancelPreAccept,
|
||||||
confirmClose,
|
confirmClose,
|
||||||
payOrder,
|
payOrder,
|
||||||
requestClose,
|
requestClose,
|
||||||
} from "@/lib/api/orders"
|
} from "@/lib/api/orders"
|
||||||
import type { Actor } from "@/lib/policy/actor"
|
|
||||||
import { notifyInfo, notifySuccess } from "@/lib/toast"
|
import { notifyInfo, notifySuccess } from "@/lib/toast"
|
||||||
import type { OrderStatus } from "@/lib/types"
|
import type { OrderStatus } from "@/lib/types"
|
||||||
import { useAuthStore } from "@/store/auth"
|
import { useAuthStore } from "@/store/auth"
|
||||||
@@ -58,6 +56,8 @@ export default function OrderActions({
|
|||||||
sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id
|
sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id
|
||||||
|
|
||||||
const status = order?.status ?? initialStatus
|
const status = order?.status ?? initialStatus
|
||||||
|
const isConsumer = order?.consumerId === currentUserId
|
||||||
|
const isPlayer = order?.playerId === currentUserId
|
||||||
|
|
||||||
const handleDecision = useCallback(
|
const handleDecision = useCallback(
|
||||||
(okMessage: string, result: { decision: { ok: boolean; message?: string } }) => {
|
(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 (
|
return (
|
||||||
<div className="flex gap-2 flex-wrap">
|
<div className="flex gap-2 flex-wrap">
|
||||||
{status === "pending_payment" && (
|
{status === "pending_payment" && isConsumer && (
|
||||||
<>
|
<>
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
@@ -101,7 +82,6 @@ export default function OrderActions({
|
|||||||
notifyInfo("请先登录")
|
notifyInfo("请先登录")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = cancelPreAccept(orderId)
|
const result = cancelPreAccept(orderId)
|
||||||
handleDecision("订单已取消", result)
|
handleDecision("订单已取消", result)
|
||||||
}}
|
}}
|
||||||
@@ -115,7 +95,6 @@ export default function OrderActions({
|
|||||||
notifyInfo("请先登录")
|
notifyInfo("请先登录")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = payOrder(orderId)
|
const result = payOrder(orderId)
|
||||||
handleDecision("订单支付成功", result)
|
handleDecision("订单支付成功", result)
|
||||||
}}
|
}}
|
||||||
@@ -128,6 +107,7 @@ export default function OrderActions({
|
|||||||
|
|
||||||
{status === "pending_accept" && (
|
{status === "pending_accept" && (
|
||||||
<>
|
<>
|
||||||
|
{isConsumer && (
|
||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
@@ -135,7 +115,6 @@ export default function OrderActions({
|
|||||||
notifyInfo("请先登录")
|
notifyInfo("请先登录")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = cancelPreAccept(orderId)
|
const result = cancelPreAccept(orderId)
|
||||||
handleDecision("订单已取消", result)
|
handleDecision("订单已取消", result)
|
||||||
}}
|
}}
|
||||||
@@ -143,7 +122,9 @@ export default function OrderActions({
|
|||||||
<XCircle className="mr-1 h-4 w-4" />
|
<XCircle className="mr-1 h-4 w-4" />
|
||||||
取消订单
|
取消订单
|
||||||
</Button>
|
</Button>
|
||||||
{order?.shopId && dispatchMode === "auto" ? (
|
)}
|
||||||
|
{isPlayer &&
|
||||||
|
(order?.shopId && dispatchMode === "auto" ? (
|
||||||
<Button disabled>
|
<Button disabled>
|
||||||
<Clock className="mr-1 h-4 w-4" />
|
<Clock className="mr-1 h-4 w-4" />
|
||||||
系统派单中
|
系统派单中
|
||||||
@@ -155,7 +136,6 @@ export default function OrderActions({
|
|||||||
notifyInfo("请先登录")
|
notifyInfo("请先登录")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = acceptOrder(orderId)
|
const result = acceptOrder(orderId)
|
||||||
handleDecision("已接单", result)
|
handleDecision("已接单", result)
|
||||||
}}
|
}}
|
||||||
@@ -163,7 +143,7 @@ export default function OrderActions({
|
|||||||
<CheckCircle2 className="mr-1 h-4 w-4" />
|
<CheckCircle2 className="mr-1 h-4 w-4" />
|
||||||
接单
|
接单
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
))}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
@@ -202,19 +182,20 @@ export default function OrderActions({
|
|||||||
|
|
||||||
{status === "pending_close" && (
|
{status === "pending_close" && (
|
||||||
<>
|
<>
|
||||||
|
{isConsumer && (
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
if (!currentUserId) {
|
if (!currentUserId) {
|
||||||
notifyInfo("请先登录")
|
notifyInfo("请先登录")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = confirmClose(orderId)
|
const result = confirmClose(orderId)
|
||||||
handleDecision("已确认结单", result)
|
handleDecision("已确认结单", result)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
确认结单
|
确认结单
|
||||||
</Button>
|
</Button>
|
||||||
|
)}
|
||||||
<Button variant="destructive" asChild>
|
<Button variant="destructive" asChild>
|
||||||
<Link href={`/dispute/${orderId}`}>
|
<Link href={`/dispute/${orderId}`}>
|
||||||
<AlertTriangle className="mr-1 h-4 w-4" />
|
<AlertTriangle className="mr-1 h-4 w-4" />
|
||||||
|
|||||||
Reference in New Issue
Block a user