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 { EmptyState } from "@/components/ui/empty-state"
|
||||||
import { Progress } from "@/components/ui/progress"
|
import { Progress } from "@/components/ui/progress"
|
||||||
import { StatusBadge, type StatusBadgeProps } from "@/components/ui/status-badge"
|
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 { 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 { useAuthStore } from "@/store/auth"
|
||||||
import { CheckCircle, Clock, DollarSign, ListOrdered, Star, TrendingUp, Users } from "lucide-react"
|
import { CheckCircle, Clock, DollarSign, ListOrdered, Star, TrendingUp, Users } from "lucide-react"
|
||||||
import Link from "next/link"
|
import Link from "next/link"
|
||||||
@@ -38,26 +38,29 @@ export default function DashboardPage() {
|
|||||||
|
|
||||||
const [player, setPlayer] = useState<Player | null>(null)
|
const [player, setPlayer] = useState<Player | null>(null)
|
||||||
const [shop, setShop] = useState<Shop | null>(null)
|
const [shop, setShop] = useState<Shop | null>(null)
|
||||||
const [services, setServices] = useState<PlayerService[]>([])
|
|
||||||
const [orders, setOrders] = useState<Awaited<ReturnType<typeof listOrders>>>([])
|
const [orders, setOrders] = useState<Awaited<ReturnType<typeof listOrders>>>([])
|
||||||
const [monthlyIncome, setMonthlyIncome] = useState<string>("0")
|
const [monthlyIncome, setMonthlyIncome] = useState<string>("0")
|
||||||
const recentOrders = orders.slice(0, 3)
|
const recentOrders = orders.slice(0, 3)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!user?.id) {
|
||||||
|
setPlayer(null)
|
||||||
|
setShop(null)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
|
|
||||||
Promise.all([listPlayers(), listShops(), listServices()])
|
Promise.all([getMyPlayer(), getMyShop()])
|
||||||
.then(([players, shops, services]) => {
|
.then(([player, shop]) => {
|
||||||
if (cancelled) return
|
if (cancelled) return
|
||||||
setPlayer(players.find((item) => item.user.id === user?.id) ?? null)
|
setPlayer(player ?? null)
|
||||||
setShop(shops.find((item) => item.owner.id === user?.id) ?? null)
|
setShop(shop ?? null)
|
||||||
setServices(services)
|
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
if (cancelled) return
|
if (cancelled) return
|
||||||
setPlayer(null)
|
setPlayer(null)
|
||||||
setShop(null)
|
setShop(null)
|
||||||
setServices([])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
@@ -116,9 +119,7 @@ export default function DashboardPage() {
|
|||||||
const rating = isOwner ? (shop?.rating ?? 0) : (player?.rating ?? 0)
|
const rating = isOwner ? (shop?.rating ?? 0) : (player?.rating ?? 0)
|
||||||
const playerCount = shop?.playerCount ?? 0
|
const playerCount = shop?.playerCount ?? 0
|
||||||
const completionRate = player?.completionRate ?? 0
|
const completionRate = player?.completionRate ?? 0
|
||||||
const serviceCount = player
|
const serviceCount = player?.services.length ?? 0
|
||||||
? services.filter((service) => String(service.playerId) === String(player.id)).length
|
|
||||||
: 0
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="container mx-auto max-w-6xl px-4 py-8 space-y-8">
|
<div className="container mx-auto max-w-6xl px-4 py-8 space-y-8">
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ export {
|
|||||||
markNotificationAsRead,
|
markNotificationAsRead,
|
||||||
} from "./notifications"
|
} from "./notifications"
|
||||||
export { getOrderById, listOrders } from "./orders"
|
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 { createPost, getPostById, listPosts, listPostsByAuthor, togglePostLike } from "./posts"
|
||||||
export { listReviews, listReviewsByOrder, listReviewsByTargetUser } from "./reviews"
|
export { listReviews, listReviewsByOrder, listReviewsByTargetUser } from "./reviews"
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -35,6 +35,20 @@ export async function getPlayerById(playerId: string): Promise<Player | undefine
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getMyPlayer(): Promise<Player | undefined> {
|
||||||
|
try {
|
||||||
|
return await httpJson<Player>("/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<Player[]> {
|
export async function listPlayersByShop(shopId: string): Promise<Player[]> {
|
||||||
const players = await listPlayers()
|
const players = await listPlayers()
|
||||||
return players.filter((player) => String(player.shopId) === shopId)
|
return players.filter((player) => String(player.shopId) === shopId)
|
||||||
|
|||||||
Reference in New Issue
Block a user