import ChallengeCard from '@/components/ChallengeCard' import PenaltyBanner from '@/components/PenalyBanner' import { useDrawRandomChallengeMutation, useEndChallenge } from '@/hooks/mutations/useChallengeMutation' import { useAuth } from '@/hooks/useAuth' import { useChallengeActions } from '@/hooks/useChallengeActions' import { useChallenges } from '@/hooks/useChallenges' import { useGame } from '@/hooks/useGame' import { FontAwesome6, MaterialCommunityIcons } from '@expo/vector-icons' import { useQueryClient } from '@tanstack/react-query' import { useRouter } from 'expo-router' import { useEffect, useMemo, useState } from 'react' import { View } from 'react-native' import { ActivityIndicator, Appbar, Banner, FAB, MD3Colors, Snackbar, Surface, Text, TouchableRipple } from 'react-native-paper' function ChallengeScreenHeader() { const router = useRouter() return <> router.navigate('/challenges-list')} /> } function ChallengeScreenBody() { const queryClient = useQueryClient() const auth = useAuth() const game = useGame() const challengeActions = useChallengeActions() const challenges = useChallenges() const currentChallengeAction = useMemo(() => { if (!game.activeChallengeId) return null return challengeActions.find((action) => action.id === game.activeChallengeId) }, [game, challengeActions]) const currentChallenge = useMemo(() => { if (!currentChallengeAction) return null return challenges.find((challenge) => challenge.id === currentChallengeAction.challengeId) }, [currentChallengeAction, challenges]) const [loading, setLoading] = useState(false) const [successSnackbarVisible, setSuccessSnackbarVisible] = useState(false) const [errorVisible, setErrorVisible] = useState(false) const [error, setError] = useState([200, ""]) const drawRandomChallengeMutation = useDrawRandomChallengeMutation({ auth, onPostSuccess: () => { setLoading(true) setSuccessSnackbarVisible(true) queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) const endChallenge = useEndChallenge({ auth, onPostSuccess: () => { setLoading(true) setSuccessSnackbarVisible(true) queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' }) }, onError: ({ response, error }) => { setErrorVisible(true) if (response) setError([response.statusCode, response.message]) else if (error) setError([400, error.message]) }, }) useEffect(() => { if (challengeActions) setLoading(false) }, [challengeActions]) return <> {loading && } {!loading && currentChallenge && { setLoading(true); endChallenge.mutate({ success: true }) }} onFail={() => endChallenge.mutate({ success: false })} style={{ flex: 1, margin: 20 }} />} {!loading && !game.penaltyEnd && !currentChallenge && game.currentRunner && <> Aucun défi n'est en cours. Veuillez tirer un défi en cliquant sur le bouton central. Pour rappel, il faut être hors d'un train pour tirer un défi. drawRandomChallengeMutation.mutate()} variant='tertiary' customSize={64} /> } } style={{ backgroundColor: MD3Colors.secondary30 }}> Vous êtes poursuiveuse, et n'avez donc pas de défi à accomplir. setSuccessSnackbarVisible(false)} onIconPress={() => setSuccessSnackbarVisible(false)}> Jeu actualisé setErrorVisible(false)} onIconPress={() => setErrorVisible(false)}> Erreur {error[0]} : {error[1]} } export default function ChallengesScreen() { return ( ) }