Récupération de défis et tirage d'un nouveau défi via des boutons
This commit is contained in:
@ -1,7 +1,11 @@
|
||||
import { useAuth } from '@/hooks/useAuth'
|
||||
import { useGame, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
||||
import { useDownloadChallengeActions } from '@/hooks/useChallengeActions'
|
||||
import { useDownloadChallenges } from '@/hooks/useChallenges'
|
||||
import { useGame, useUpdateActiveChallengeId, useUpdateGameState, useUpdateMoney } from '@/hooks/useGame'
|
||||
import { useDownloadTrains } from '@/hooks/useTrain'
|
||||
import { isAuthValid } from '@/utils/features/auth/authSlice'
|
||||
import { ChallengeActionPayload } from '@/utils/features/challengeActions/challengeActionsSlice'
|
||||
import { Challenge } from '@/utils/features/challenges/challengesSlice'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { ReactNode, useEffect } from 'react'
|
||||
|
||||
@ -10,7 +14,10 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
const game = useGame()
|
||||
const updateGameState = useUpdateGameState()
|
||||
const updateMoney = useUpdateMoney()
|
||||
const updateActiveChallengeId = useUpdateActiveChallengeId()
|
||||
const downloadTrains = useDownloadTrains()
|
||||
const downloadChallenges = useDownloadChallenges()
|
||||
const downloadChallengeActions = useDownloadChallengeActions()
|
||||
|
||||
const gameQuery = useQuery({
|
||||
queryKey: ['get-game', auth.token],
|
||||
@ -34,8 +41,10 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
refetchInterval: 5000,
|
||||
})
|
||||
useEffect(() => {
|
||||
if (playerQuery.isSuccess && playerQuery.data)
|
||||
if (playerQuery.isSuccess && playerQuery.data) {
|
||||
updateMoney(playerQuery.data.money)
|
||||
updateActiveChallengeId(playerQuery.data.activeChallengeId)
|
||||
}
|
||||
}, [playerQuery.status, playerQuery.dataUpdatedAt])
|
||||
|
||||
const trainsQuery = useQuery({
|
||||
@ -47,10 +56,27 @@ export default function GameProvider({ children }: { children: ReactNode }) {
|
||||
refetchInterval: 5000,
|
||||
})
|
||||
useEffect(() => {
|
||||
if (trainsQuery.isSuccess && trainsQuery.data && trainsQuery)
|
||||
if (trainsQuery.isSuccess && trainsQuery.data)
|
||||
downloadTrains(trainsQuery.data)
|
||||
}, [trainsQuery.status, trainsQuery.dataUpdatedAt])
|
||||
|
||||
const challengesQuery = useQuery({
|
||||
queryKey: ['get-challenges', game.playerId, auth.token],
|
||||
queryFn: () => fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/challenges/?size=10000`, {
|
||||
headers: { "Authorization": `Bearer ${auth.token}` }}
|
||||
).then(resp => resp.json()),
|
||||
enabled: isAuthValid(auth) && !!game.playerId,
|
||||
refetchInterval: 5000,
|
||||
})
|
||||
useEffect(() => {
|
||||
if (challengesQuery.isSuccess && challengesQuery.data) {
|
||||
downloadChallenges(challengesQuery.data)
|
||||
const dataWithPlayerActions = challengesQuery.data.data.filter(
|
||||
(challenge: (Challenge & {action: ChallengeActionPayload | null})) => challenge.action !== null && challenge.action.playerId === game.playerId)
|
||||
downloadChallengeActions({ data: dataWithPlayerActions })
|
||||
}
|
||||
}, [challengesQuery.status, challengesQuery.dataUpdatedAt])
|
||||
|
||||
return <>
|
||||
{children}
|
||||
</>
|
||||
|
Reference in New Issue
Block a user