feat(games): fetch games from backend

This commit is contained in:
zetaloop
2026-02-28 16:23:30 +08:00
parent 6dd21e1090
commit f4365668ab
5 changed files with 96 additions and 14 deletions
@@ -15,7 +15,7 @@ import { Textarea } from "@/components/ui/textarea"
import { getGameById, listGames } from "@/lib/api"
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
import { GameIcon } from "@/lib/game-icons"
import type { PlayerService } from "@/lib/types"
import type { Game, PlayerService } from "@/lib/types"
import { useAuthStore } from "@/store/auth"
import { usePlayerStore } from "@/store/players"
import { useServiceStore } from "@/store/services"
@@ -24,7 +24,7 @@ import { standardSchemaResolver } from "@hookform/resolvers/standard-schema"
import { ArrowLeft } from "lucide-react"
import Link from "next/link"
import { useRouter, useSearchParams } from "next/navigation"
import { useEffect } from "react"
import { useEffect, useState } from "react"
import { useForm, useWatch } from "react-hook-form"
import { z } from "zod"
@@ -97,7 +97,25 @@ export default function NewServicePage() {
const selectedGameId = useWatch({ control, name: "gameId" })
const selectedUnit = useWatch({ control, name: "unit" })
const games = listGames()
const [games, setGames] = useState<Game[]>([])
useEffect(() => {
let cancelled = false
listGames()
.then((items) => {
if (cancelled) return
setGames(items)
})
.catch(() => {
if (cancelled) return
setGames([])
})
return () => {
cancelled = true
}
}, [])
if (serviceId && !editingService) {
return <div className="text-sm text-muted-foreground"></div>
@@ -108,7 +126,7 @@ export default function NewServicePage() {
}
const onSubmit = async (data: z.infer<typeof serviceSchema>) => {
const game = getGameById(data.gameId)
const game = await getGameById(data.gameId)
if (!game) return
const payload: Omit<PlayerService, "id"> = {