feat(catalog): fetch players, services, shops
This commit is contained in:
+38
-7
@@ -1,13 +1,44 @@
|
||||
import { usePlayerStore } from "@/store/players"
|
||||
import { isApiError } from "@/lib/errors"
|
||||
import type { Player } from "@/lib/types"
|
||||
|
||||
export function listPlayers() {
|
||||
return usePlayerStore.getState().players
|
||||
import { httpJson } from "./http"
|
||||
|
||||
type Paginated<T> = {
|
||||
items: T[]
|
||||
meta: {
|
||||
total: number
|
||||
offset: number
|
||||
limit: number
|
||||
}
|
||||
}
|
||||
|
||||
export function getPlayerById(playerId: string) {
|
||||
return usePlayerStore.getState().players.find((player) => player.id === playerId)
|
||||
export async function listPlayers(): Promise<Player[]> {
|
||||
const res = await httpJson<Paginated<Player>>("/api/v1/players?offset=0&limit=1000", {
|
||||
cache: "no-store",
|
||||
})
|
||||
return res.items
|
||||
}
|
||||
|
||||
export function listPlayersByShop(shopId: string) {
|
||||
return usePlayerStore.getState().players.filter((player) => player.shopId === shopId)
|
||||
export async function getPlayerById(playerId: string): Promise<Player | undefined> {
|
||||
try {
|
||||
return await httpJson<Player>(`/api/v1/players/${encodeURIComponent(playerId)}`, {
|
||||
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[]> {
|
||||
const res = await httpJson<Paginated<Player>>(
|
||||
`/api/v1/shops/${encodeURIComponent(shopId)}/players?offset=0&limit=1000`,
|
||||
{ cache: "no-store" },
|
||||
)
|
||||
return res.items
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user