Récupération de la liste des trains empruntés
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
||||
import { useDownloadTrains } from '@/hooks/useTrain'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { ReactNode, useEffect } from 'react'
|
||||
|
||||
@ -8,6 +9,7 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
const game = useGame()
|
||||
const updateGameState = useUpdateGameState()
|
||||
const updateMoney = useUpdateMoney()
|
||||
const downloadTrains = useDownloadTrains()
|
||||
|
||||
const gameQuery = useQuery({
|
||||
queryKey: ['get-game'],
|
||||
@ -20,10 +22,10 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
useEffect(() => {
|
||||
if (gameQuery.isSuccess && gameQuery.data)
|
||||
updateGameState(gameQuery.data)
|
||||
}, [gameQuery])
|
||||
}, [gameQuery.status, gameQuery.dataUpdatedAt])
|
||||
|
||||
const playerQuery = useQuery({
|
||||
queryKey: ['get-player'],
|
||||
queryKey: ['get-player', game.playerId],
|
||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/players/${game.playerId}/`, {
|
||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||
).then(resp => resp.json()),
|
||||
@ -33,7 +35,20 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
useEffect(() => {
|
||||
if (playerQuery.isSuccess && playerQuery.data)
|
||||
updateMoney(playerQuery.data.money)
|
||||
}, [playerQuery])
|
||||
}, [playerQuery.status, playerQuery.dataUpdatedAt])
|
||||
|
||||
const trainsQuery = useQuery({
|
||||
queryKey: ['get-trains'],
|
||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/`, {
|
||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||
).then(resp => resp.json()),
|
||||
enabled: auth.loggedIn,
|
||||
refetchInterval: 5000,
|
||||
})
|
||||
useEffect(() => {
|
||||
if (trainsQuery.isSuccess && trainsQuery.data)
|
||||
downloadTrains(trainsQuery.data)
|
||||
}, [trainsQuery.status, trainsQuery.dataUpdatedAt])
|
||||
|
||||
return <>
|
||||
{children}
|
||||
|
Reference in New Issue
Block a user