243 lines
6.6 KiB
TypeScript
243 lines
6.6 KiB
TypeScript
"use client"
|
|
|
|
import {
|
|
AlertTriangle,
|
|
CheckCircle2,
|
|
Clock,
|
|
MessageSquare,
|
|
RefreshCw,
|
|
Star,
|
|
XCircle,
|
|
} from "lucide-react"
|
|
import Link from "next/link"
|
|
import { useCallback } from "react"
|
|
import { Button } from "@/components/ui/button"
|
|
import {
|
|
acceptOrder,
|
|
cancelPreAccept,
|
|
confirmClose,
|
|
payOrder,
|
|
requestClose,
|
|
} from "@/lib/api/orders"
|
|
import { notifyInfo, notifySuccess } from "@/lib/toast"
|
|
import type { OrderStatus } from "@/lib/types"
|
|
import { useAuthStore } from "@/store/auth"
|
|
import { useChatStore } from "@/store/chat"
|
|
import { useOrderStore } from "@/store/orders"
|
|
import { useShopStore } from "@/store/shops"
|
|
|
|
interface OrderActionsProps {
|
|
orderId: string
|
|
initialStatus: OrderStatus
|
|
chatSessionId?: string
|
|
serviceId: string
|
|
}
|
|
|
|
function showFeedback(message: string) {
|
|
notifySuccess(message)
|
|
}
|
|
|
|
export default function OrderActions({
|
|
orderId,
|
|
initialStatus,
|
|
chatSessionId,
|
|
serviceId,
|
|
}: OrderActionsProps) {
|
|
const currentUserId = useAuthStore((state) => state.user?.id)
|
|
const order = useOrderStore((state) => state.orders.find((item) => item.id === orderId))
|
|
const sessions = useChatStore((state) => state.sessions)
|
|
const dispatchMode = useShopStore((state) => {
|
|
if (!order?.shopId) return "manual"
|
|
const shop = state.shops.find((item) => item.id === order.shopId)
|
|
return shop?.dispatchMode ?? "manual"
|
|
})
|
|
const resolvedChatSessionId =
|
|
chatSessionId ??
|
|
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 } }) => {
|
|
if (result.decision.ok) {
|
|
showFeedback(okMessage)
|
|
return
|
|
}
|
|
|
|
notifyInfo(result.decision.message ?? "当前操作不允许")
|
|
},
|
|
[],
|
|
)
|
|
|
|
return (
|
|
<div className="flex gap-2 flex-wrap">
|
|
{status === "pending_payment" && isConsumer && (
|
|
<>
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => {
|
|
if (!currentUserId) {
|
|
notifyInfo("请先登录")
|
|
return
|
|
}
|
|
const result = cancelPreAccept(orderId)
|
|
handleDecision("订单已取消", result)
|
|
}}
|
|
>
|
|
<XCircle className="mr-1 h-4 w-4" />
|
|
取消订单
|
|
</Button>
|
|
<Button
|
|
onClick={() => {
|
|
if (!currentUserId) {
|
|
notifyInfo("请先登录")
|
|
return
|
|
}
|
|
const result = payOrder(orderId)
|
|
handleDecision("订单支付成功", result)
|
|
}}
|
|
>
|
|
<CheckCircle2 className="mr-1 h-4 w-4" />
|
|
确认支付
|
|
</Button>
|
|
</>
|
|
)}
|
|
|
|
{status === "pending_accept" && (
|
|
<>
|
|
{isConsumer && (
|
|
<Button
|
|
variant="outline"
|
|
onClick={() => {
|
|
if (!currentUserId) {
|
|
notifyInfo("请先登录")
|
|
return
|
|
}
|
|
const result = cancelPreAccept(orderId)
|
|
handleDecision("订单已取消", result)
|
|
}}
|
|
>
|
|
<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>
|
|
))}
|
|
</>
|
|
)}
|
|
|
|
{(status === "in_progress" || status === "pending_close") && resolvedChatSessionId && (
|
|
<Button asChild>
|
|
<Link href={`/chat/${resolvedChatSessionId}`}>
|
|
<MessageSquare className="mr-1 h-4 w-4" />
|
|
聊天
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
|
|
{status === "in_progress" && (
|
|
<>
|
|
<Button
|
|
onClick={() => {
|
|
if (!currentUserId) {
|
|
notifyInfo("请先登录")
|
|
return
|
|
}
|
|
|
|
const result = requestClose(orderId)
|
|
handleDecision("已发起结单", result)
|
|
}}
|
|
>
|
|
发起结单
|
|
</Button>
|
|
<Button variant="destructive" asChild>
|
|
<Link href={`/dispute/${orderId}`}>
|
|
<AlertTriangle className="mr-1 h-4 w-4" />
|
|
发起争议
|
|
</Link>
|
|
</Button>
|
|
</>
|
|
)}
|
|
|
|
{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" />
|
|
发起争议
|
|
</Link>
|
|
</Button>
|
|
</>
|
|
)}
|
|
|
|
{status === "pending_review" && (
|
|
<Button asChild>
|
|
<Link href={`/review/${orderId}`}>
|
|
<Star className="mr-1 h-4 w-4" />
|
|
评价
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
|
|
{status === "completed" && (
|
|
<Button variant="outline" asChild>
|
|
<Link href={`/order/new?serviceId=${serviceId}`}>
|
|
<RefreshCw className="mr-1 h-4 w-4" />
|
|
再来一单
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
|
|
{status === "cancelled" && (
|
|
<Button variant="outline" asChild>
|
|
<Link href={`/order/new?serviceId=${serviceId}`}>
|
|
<RefreshCw className="mr-1 h-4 w-4" />
|
|
再来一单
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
|
|
{status === "disputed" && (
|
|
<Button variant="outline" asChild>
|
|
<Link href={`/dispute/${orderId}`}>查看详情</Link>
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|