feat: wire order and chat state flow
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
import { AlertTriangle, CheckCircle2, MessageSquare, RefreshCw, Star, XCircle } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useState } from "react"
|
||||
import { useEffect, useState } from "react"
|
||||
import { Button } from "@/components/ui/button"
|
||||
import type { OrderStatus } from "@/lib/types"
|
||||
import { useChatStore } from "@/store/chat"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
|
||||
interface OrderActionsProps {
|
||||
orderId: string
|
||||
@@ -24,16 +26,32 @@ export default function OrderActions({
|
||||
chatSessionId,
|
||||
serviceId,
|
||||
}: OrderActionsProps) {
|
||||
const [status, setStatus] = useState<OrderStatus>(initialStatus)
|
||||
const order = useOrderStore((state) => state.orders.find((item) => item.id === orderId))
|
||||
const updateOrderStatus = useOrderStore((state) => state.updateOrderStatus)
|
||||
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
|
||||
const [resolvedChatSessionId, setResolvedChatSessionId] = useState(chatSessionId)
|
||||
|
||||
const status = order?.status ?? initialStatus
|
||||
|
||||
useEffect(() => {
|
||||
if (chatSessionId) {
|
||||
setResolvedChatSessionId(chatSessionId)
|
||||
return
|
||||
}
|
||||
|
||||
if (!order) return
|
||||
const session = ensureOrderSession(order)
|
||||
setResolvedChatSessionId(session.id)
|
||||
}, [chatSessionId, order, ensureOrderSession])
|
||||
|
||||
return (
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
{status === "pending_accept" && (
|
||||
{status === "pending_payment" && (
|
||||
<>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
setStatus("cancelled")
|
||||
updateOrderStatus(orderId, "cancelled")
|
||||
showFeedback("订单已取消")
|
||||
}}
|
||||
>
|
||||
@@ -42,7 +60,30 @@ export default function OrderActions({
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setStatus("in_progress")
|
||||
updateOrderStatus(orderId, "pending_accept")
|
||||
}}
|
||||
>
|
||||
<CheckCircle2 className="mr-1 h-4 w-4" />
|
||||
确认支付
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
|
||||
{status === "pending_accept" && (
|
||||
<>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
updateOrderStatus(orderId, "cancelled")
|
||||
showFeedback("订单已取消")
|
||||
}}
|
||||
>
|
||||
<XCircle className="mr-1 h-4 w-4" />
|
||||
取消订单
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
updateOrderStatus(orderId, "in_progress")
|
||||
showFeedback("已接单")
|
||||
}}
|
||||
>
|
||||
@@ -52,9 +93,9 @@ export default function OrderActions({
|
||||
</>
|
||||
)}
|
||||
|
||||
{(status === "in_progress" || status === "pending_close") && chatSessionId && (
|
||||
{(status === "in_progress" || status === "pending_close") && resolvedChatSessionId && (
|
||||
<Button asChild>
|
||||
<Link href={`/chat/${chatSessionId}`}>
|
||||
<Link href={`/chat/${resolvedChatSessionId}`}>
|
||||
<MessageSquare className="mr-1 h-4 w-4" />
|
||||
聊天
|
||||
</Link>
|
||||
@@ -65,7 +106,7 @@ export default function OrderActions({
|
||||
<>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setStatus("pending_close")
|
||||
updateOrderStatus(orderId, "pending_close")
|
||||
showFeedback("已发起结单")
|
||||
}}
|
||||
>
|
||||
@@ -84,8 +125,7 @@ export default function OrderActions({
|
||||
<>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setStatus("completed")
|
||||
showFeedback("订单已完成")
|
||||
updateOrderStatus(orderId, "pending_review")
|
||||
}}
|
||||
>
|
||||
确认结单
|
||||
|
||||
Reference in New Issue
Block a user