refactor(api): add adapter layer for order/chat/review/dispute writes
This commit is contained in:
@@ -12,6 +12,14 @@ import {
|
||||
import Link from "next/link"
|
||||
import { useCallback, useEffect } 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"
|
||||
@@ -37,21 +45,9 @@ export default function OrderActions({
|
||||
chatSessionId,
|
||||
serviceId,
|
||||
}: OrderActionsProps) {
|
||||
const currentRole = useAuthStore((state) => state.currentRole)
|
||||
const currentUserId = useAuthStore((state) => state.user?.id)
|
||||
const payOrder = useOrderStore((state) => state.payOrder)
|
||||
const acceptOrder = useOrderStore((state) => state.acceptOrder)
|
||||
const requestClose = useOrderStore((state) => state.requestClose)
|
||||
const confirmClose = useOrderStore((state) => state.confirmClose)
|
||||
const cancelPreAccept = useOrderStore((state) => state.cancelPreAccept)
|
||||
const order = useOrderStore((state) => state.orders.find((item) => item.id === orderId))
|
||||
const sessions = useChatStore((state) => state.sessions)
|
||||
const ensureOrderSession = useChatStore((state) => state.ensureOrderSession)
|
||||
const actorShopId = useShopStore((state) => {
|
||||
if (!currentUserId || currentRole !== "owner") return undefined
|
||||
const owned = state.shops.find((shop) => shop.owner.id === currentUserId)
|
||||
return owned?.id
|
||||
})
|
||||
const dispatchMode = useShopStore((state) => {
|
||||
if (!order?.shopId) return "manual"
|
||||
const shop = state.shops.find((item) => item.id === order.shopId)
|
||||
@@ -62,13 +58,6 @@ export default function OrderActions({
|
||||
sessions.find((session) => session.type === "order" && session.orderId === orderId)?.id
|
||||
|
||||
const status = order?.status ?? initialStatus
|
||||
const actor: Actor | undefined = currentUserId
|
||||
? {
|
||||
userId: currentUserId,
|
||||
role: currentRole,
|
||||
shopId: actorShopId,
|
||||
}
|
||||
: undefined
|
||||
|
||||
const handleDecision = useCallback(
|
||||
(okMessage: string, result: { decision: { ok: boolean; message?: string } }) => {
|
||||
@@ -82,11 +71,6 @@ export default function OrderActions({
|
||||
[],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (chatSessionId || !order || resolvedChatSessionId) return
|
||||
ensureOrderSession(order)
|
||||
}, [chatSessionId, order, ensureOrderSession, resolvedChatSessionId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!order) return
|
||||
if (order.status !== "pending_accept") return
|
||||
@@ -99,12 +83,12 @@ export default function OrderActions({
|
||||
role: "player",
|
||||
shopId: order.shopId,
|
||||
}
|
||||
const result = acceptOrder(orderId, systemActor)
|
||||
const result = acceptOrderAsActor(orderId, systemActor)
|
||||
handleDecision("系统已自动派单", result)
|
||||
}, 3000)
|
||||
|
||||
return () => clearTimeout(timer)
|
||||
}, [acceptOrder, dispatchMode, handleDecision, order, orderId])
|
||||
}, [dispatchMode, handleDecision, order, orderId])
|
||||
|
||||
return (
|
||||
<div className="flex gap-2 flex-wrap">
|
||||
@@ -113,12 +97,12 @@ export default function OrderActions({
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = cancelPreAccept(orderId, actor)
|
||||
const result = cancelPreAccept(orderId)
|
||||
handleDecision("订单已取消", result)
|
||||
}}
|
||||
>
|
||||
@@ -127,12 +111,12 @@ export default function OrderActions({
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = payOrder(orderId, actor)
|
||||
const result = payOrder(orderId)
|
||||
handleDecision("订单支付成功", result)
|
||||
}}
|
||||
>
|
||||
@@ -147,12 +131,12 @@ export default function OrderActions({
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = cancelPreAccept(orderId, actor)
|
||||
const result = cancelPreAccept(orderId)
|
||||
handleDecision("订单已取消", result)
|
||||
}}
|
||||
>
|
||||
@@ -167,12 +151,12 @@ export default function OrderActions({
|
||||
) : (
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = acceptOrder(orderId, actor)
|
||||
const result = acceptOrder(orderId)
|
||||
handleDecision("已接单", result)
|
||||
}}
|
||||
>
|
||||
@@ -196,12 +180,12 @@ export default function OrderActions({
|
||||
<>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = requestClose(orderId, actor)
|
||||
const result = requestClose(orderId)
|
||||
handleDecision("已发起结单", result)
|
||||
}}
|
||||
>
|
||||
@@ -220,12 +204,12 @@ export default function OrderActions({
|
||||
<>
|
||||
<Button
|
||||
onClick={() => {
|
||||
if (!actor) {
|
||||
if (!currentUserId) {
|
||||
notifyInfo("请先登录")
|
||||
return
|
||||
}
|
||||
|
||||
const result = confirmClose(orderId, actor)
|
||||
const result = confirmClose(orderId)
|
||||
handleDecision("已确认结单", result)
|
||||
}}
|
||||
>
|
||||
|
||||
Reference in New Issue
Block a user