feat(services): manage services through backend
This commit is contained in:
@@ -12,13 +12,20 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select"
|
||||
import { Textarea } from "@/components/ui/textarea"
|
||||
import { getGameById, listGames } from "@/lib/api"
|
||||
import {
|
||||
createPlayerService,
|
||||
getGameById,
|
||||
getServiceById,
|
||||
listGames,
|
||||
listPlayers,
|
||||
updatePlayerService,
|
||||
} from "@/lib/api"
|
||||
import { resolveOwnerShop } from "@/lib/domain/resolve-current-shop"
|
||||
import { toApiError } from "@/lib/errors"
|
||||
import { GameIcon } from "@/lib/game-icons"
|
||||
import { notifyInfo, notifySuccess } from "@/lib/toast"
|
||||
import type { Game, PlayerService } from "@/lib/types"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { usePlayerStore } from "@/store/players"
|
||||
import { useServiceStore } from "@/store/services"
|
||||
import { useShopStore } from "@/store/shops"
|
||||
import { standardSchemaResolver } from "@hookform/resolvers/standard-schema"
|
||||
import { ArrowLeft } from "lucide-react"
|
||||
@@ -45,15 +52,14 @@ export default function NewServicePage() {
|
||||
const userId = useAuthStore((state) => state.user?.id)
|
||||
const currentRole = useAuthStore((state) => state.currentRole)
|
||||
const shops = useShopStore((state) => state.shops)
|
||||
const players = usePlayerStore((state) => state.players)
|
||||
const services = useServiceStore((state) => state.services)
|
||||
const createService = useServiceStore((state) => state.createService)
|
||||
const updateService = useServiceStore((state) => state.updateService)
|
||||
const [players, setPlayers] = useState<Awaited<ReturnType<typeof listPlayers>>>([])
|
||||
const [editingService, setEditingService] = useState<PlayerService | undefined>(undefined)
|
||||
const [loadingService, setLoadingService] = useState(Boolean(serviceId))
|
||||
const ownerShop = resolveOwnerShop(userId, shops)
|
||||
const scopedPlayerIds =
|
||||
currentRole === "player"
|
||||
? userId
|
||||
? [userId]
|
||||
? players.filter((player) => player.user.id === userId).map((player) => player.id)
|
||||
: []
|
||||
: currentRole === "owner"
|
||||
? ownerShop
|
||||
@@ -61,9 +67,6 @@ export default function NewServicePage() {
|
||||
: []
|
||||
: []
|
||||
const scopedPlayerIdSet = new Set(scopedPlayerIds)
|
||||
const editingService = services.find(
|
||||
(service) => service.id === serviceId && scopedPlayerIdSet.has(service.playerId),
|
||||
)
|
||||
const targetPlayerId = editingService?.playerId ?? scopedPlayerIds[0]
|
||||
const {
|
||||
register,
|
||||
@@ -84,6 +87,42 @@ export default function NewServicePage() {
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
|
||||
listPlayers()
|
||||
.then((items) => {
|
||||
if (!cancelled) setPlayers(items)
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!cancelled) notifyInfo(toApiError(error).msg)
|
||||
})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!serviceId) return
|
||||
let cancelled = false
|
||||
|
||||
getServiceById(serviceId)
|
||||
.then((service) => {
|
||||
if (!cancelled) setEditingService(service)
|
||||
})
|
||||
.catch((error) => {
|
||||
if (!cancelled) notifyInfo(toApiError(error).msg)
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setLoadingService(false)
|
||||
})
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [serviceId])
|
||||
|
||||
useEffect(() => {
|
||||
if (!editingService) return
|
||||
setValue("gameId", editingService.gameId)
|
||||
@@ -117,7 +156,11 @@ export default function NewServicePage() {
|
||||
}
|
||||
}, [])
|
||||
|
||||
if (serviceId && !editingService) {
|
||||
if (loadingService) {
|
||||
return <div className="text-sm text-muted-foreground">加载中...</div>
|
||||
}
|
||||
|
||||
if (serviceId && (!editingService || !scopedPlayerIdSet.has(editingService.playerId))) {
|
||||
return <div className="text-sm text-muted-foreground">服务不存在或当前身份不可编辑</div>
|
||||
}
|
||||
|
||||
@@ -145,9 +188,11 @@ export default function NewServicePage() {
|
||||
}
|
||||
|
||||
if (editingService) {
|
||||
updateService(editingService.id, payload)
|
||||
await updatePlayerService(editingService.id, payload)
|
||||
notifySuccess("服务已保存")
|
||||
} else {
|
||||
createService(payload)
|
||||
await createPlayerService(payload)
|
||||
notifySuccess("服务已发布")
|
||||
}
|
||||
|
||||
router.push("/dashboard/services")
|
||||
|
||||
Reference in New Issue
Block a user