diff --git a/app/(dashboard)/dashboard/page.tsx b/app/(dashboard)/dashboard/page.tsx index f2a9fdc..b681ea6 100644 --- a/app/(dashboard)/dashboard/page.tsx +++ b/app/(dashboard)/dashboard/page.tsx @@ -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(null) const [shop, setShop] = useState(null) - const [services, setServices] = useState([]) const [orders, setOrders] = useState>>([]) const [monthlyIncome, setMonthlyIncome] = useState("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 (
diff --git a/lib/api/index.ts b/lib/api/index.ts index c1632f8..a8bc696 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -13,7 +13,7 @@ export { markNotificationAsRead, } from "./notifications" export { getOrderById, listOrders } from "./orders" -export { getPlayerById, listPlayers, listPlayersByShop } from "./players" +export { getMyPlayer, getPlayerById, listPlayers, listPlayersByShop } from "./players" export { createPost, getPostById, listPosts, listPostsByAuthor, togglePostLike } from "./posts" export { listReviews, listReviewsByOrder, listReviewsByTargetUser } from "./reviews" export { diff --git a/lib/api/players.ts b/lib/api/players.ts index 155a5ef..2df22f6 100644 --- a/lib/api/players.ts +++ b/lib/api/players.ts @@ -35,6 +35,20 @@ export async function getPlayerById(playerId: string): Promise { + try { + return await httpJson("/api/v1/players/me", { cache: "no-store" }) + } catch (error) { + if (error instanceof Error && error.message === "UNAUTHORIZED") { + throw error + } + if (isApiError(error) && error.code === 404) { + return undefined + } + throw error + } +} + export async function listPlayersByShop(shopId: string): Promise { const players = await listPlayers() return players.filter((player) => String(player.shopId) === shopId)