refactor(dashboard): fetch current player via /players/me
This commit is contained in:
@@ -5,9 +5,9 @@ import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
|
||||
import { EmptyState } from "@/components/ui/empty-state"
|
||||
import { Progress } from "@/components/ui/progress"
|
||||
import { StatusBadge, type StatusBadgeProps } from "@/components/ui/status-badge"
|
||||
import { getShopIncomeStats, listOrders, listPlayers, listServices, listShops } from "@/lib/api"
|
||||
import { getMyPlayer, getMyShop, getShopIncomeStats, listOrders } from "@/lib/api"
|
||||
import { statusLabels } from "@/lib/constants"
|
||||
import type { Player, PlayerService, Shop } from "@/lib/types"
|
||||
import type { Player, Shop } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { CheckCircle, Clock, DollarSign, ListOrdered, Star, TrendingUp, Users } from "lucide-react"
|
||||
import Link from "next/link"
|
||||
@@ -38,26 +38,29 @@ export default function DashboardPage() {
|
||||
|
||||
const [player, setPlayer] = useState<Player | null>(null)
|
||||
const [shop, setShop] = useState<Shop | null>(null)
|
||||
const [services, setServices] = useState<PlayerService[]>([])
|
||||
const [orders, setOrders] = useState<Awaited<ReturnType<typeof listOrders>>>([])
|
||||
const [monthlyIncome, setMonthlyIncome] = useState<string>("0")
|
||||
const recentOrders = orders.slice(0, 3)
|
||||
|
||||
useEffect(() => {
|
||||
if (!user?.id) {
|
||||
setPlayer(null)
|
||||
setShop(null)
|
||||
return
|
||||
}
|
||||
|
||||
let cancelled = false
|
||||
|
||||
Promise.all([listPlayers(), listShops(), listServices()])
|
||||
.then(([players, shops, services]) => {
|
||||
Promise.all([getMyPlayer(), getMyShop()])
|
||||
.then(([player, shop]) => {
|
||||
if (cancelled) return
|
||||
setPlayer(players.find((item) => item.user.id === user?.id) ?? null)
|
||||
setShop(shops.find((item) => item.owner.id === user?.id) ?? null)
|
||||
setServices(services)
|
||||
setPlayer(player ?? null)
|
||||
setShop(shop ?? null)
|
||||
})
|
||||
.catch(() => {
|
||||
if (cancelled) return
|
||||
setPlayer(null)
|
||||
setShop(null)
|
||||
setServices([])
|
||||
})
|
||||
|
||||
return () => {
|
||||
@@ -116,9 +119,7 @@ export default function DashboardPage() {
|
||||
const rating = isOwner ? (shop?.rating ?? 0) : (player?.rating ?? 0)
|
||||
const playerCount = shop?.playerCount ?? 0
|
||||
const completionRate = player?.completionRate ?? 0
|
||||
const serviceCount = player
|
||||
? services.filter((service) => String(service.playerId) === String(player.id)).length
|
||||
: 0
|
||||
const serviceCount = player?.services.length ?? 0
|
||||
|
||||
return (
|
||||
<div className="container mx-auto max-w-6xl px-4 py-8 space-y-8">
|
||||
|
||||
Reference in New Issue
Block a user