-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathusePlayerStats.ts
More file actions
31 lines (26 loc) · 841 Bytes
/
usePlayerStats.ts
File metadata and controls
31 lines (26 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import useSWR from 'swr'
import buildError from '../utils/buildError'
import { Game } from '../entities/game'
import makeValidatedGetRequest from './makeValidatedGetRequest'
import { z } from 'zod'
import { playerGameStatSchema } from '../entities/playerGameStat'
const usePlayerStats = (activeGame: Game, playerId: string) => {
const fetcher = async ([url]: [string]) => {
const res = await makeValidatedGetRequest(url, z.object({
stats: z.array(playerGameStatSchema)
}))
return res
}
const { data, error, mutate } = useSWR(
[`/games/${activeGame.id}/players/${playerId}/stats`],
fetcher
)
return {
stats: data?.stats ?? [],
loading: !data && !error,
error: error && buildError(error),
errorStatusCode: error && error.response?.status,
mutate
}
}
export default usePlayerStats