fix(shop): load orders and income from backend
This commit is contained in:
@@ -11,27 +11,62 @@ import {
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table"
|
||||
import { listOrders } from "@/lib/api"
|
||||
import { statusLabels } from "@/lib/constants"
|
||||
import { isActiveOrder, isCompletedOrder, isDisputedOrder } from "@/lib/domain/order-filters"
|
||||
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useOrderStore } from "@/store/orders"
|
||||
import { useShopStore } from "@/store/shops"
|
||||
import { toApiError } from "@/lib/errors"
|
||||
import { useMyShop } from "@/lib/hooks/use-my-shop"
|
||||
import { notifyInfo } from "@/lib/toast"
|
||||
import { AlertCircle, CheckCircle, Clock, ListOrdered } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
import { useEffect, useMemo, useState } from "react"
|
||||
|
||||
export default function ShopOrdersPage() {
|
||||
const userId = useAuthStore((state) => state.user?.id)
|
||||
const shops = useShopStore((state) => state.shops)
|
||||
const orders = useOrderStore((state) => state.orders)
|
||||
const shop = resolveOwnerShop(userId, shops)
|
||||
const { shop, loading, error } = useMyShop()
|
||||
const [orders, setOrders] = useState<Awaited<ReturnType<typeof listOrders>>>([])
|
||||
const [ordersLoading, setOrdersLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
if (!shop) return
|
||||
|
||||
let cancelled = false
|
||||
|
||||
listOrders({ role: "owner" })
|
||||
.then((items) => {
|
||||
if (cancelled) return
|
||||
setOrders(items)
|
||||
})
|
||||
.catch((error) => {
|
||||
if (cancelled) return
|
||||
notifyInfo(toApiError(error).msg)
|
||||
})
|
||||
.finally(() => {
|
||||
if (cancelled) return
|
||||
setOrdersLoading(false)
|
||||
})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [shop])
|
||||
|
||||
const shopOrders = useMemo(
|
||||
() => (shop ? orders.filter((order) => order.shopId === shop.id) : []),
|
||||
[orders, shop],
|
||||
)
|
||||
|
||||
if (loading) {
|
||||
return <div className="text-sm text-muted-foreground">加载中...</div>
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <div className="text-sm text-muted-foreground">{error}</div>
|
||||
}
|
||||
|
||||
if (!shop) {
|
||||
return <div className="text-sm text-muted-foreground">当前账号没有可管理的店铺</div>
|
||||
}
|
||||
|
||||
const shopOrders = orders.filter((order) => order.shopId === shop?.id)
|
||||
|
||||
const totalOrders = shopOrders.length
|
||||
const activeOrders = shopOrders.filter((o) => isActiveOrder(o.status)).length
|
||||
const completedOrders = shopOrders.filter((o) => isCompletedOrder(o.status)).length
|
||||
@@ -100,23 +135,37 @@ export default function ShopOrdersPage() {
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{shopOrders.map((order) => (
|
||||
<TableRow key={order.id}>
|
||||
<TableCell className="font-medium">{order.service.title}</TableCell>
|
||||
<TableCell>{order.consumerId}</TableCell>
|
||||
<TableCell>{order.playerId}</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="outline">{statusLabels[order.status]}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>¥{order.totalPrice}</TableCell>
|
||||
<TableCell>{new Date(order.createdAt).toLocaleDateString()}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button variant="ghost" size="sm" asChild>
|
||||
<Link href={`/order/${order.id}`}>查看</Link>
|
||||
</Button>
|
||||
{ordersLoading ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={7} className="text-center text-sm text-muted-foreground">
|
||||
加载中...
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
) : shopOrders.length === 0 ? (
|
||||
<TableRow>
|
||||
<TableCell colSpan={7} className="text-center text-sm text-muted-foreground">
|
||||
暂无订单
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
) : (
|
||||
shopOrders.map((order) => (
|
||||
<TableRow key={order.id}>
|
||||
<TableCell className="font-medium">{order.service.title}</TableCell>
|
||||
<TableCell>{order.consumerId}</TableCell>
|
||||
<TableCell>{order.playerId}</TableCell>
|
||||
<TableCell>
|
||||
<Badge variant="outline">{statusLabels[order.status]}</Badge>
|
||||
</TableCell>
|
||||
<TableCell>¥{order.totalPrice}</TableCell>
|
||||
<TableCell>{new Date(order.createdAt).toLocaleDateString()}</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button variant="ghost" size="sm" asChild>
|
||||
<Link href={`/order/${order.id}`}>查看</Link>
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
)}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</CardContent>
|
||||
|
||||
Reference in New Issue
Block a user