2024-12-12 18:33:44 +00:00
|
|
|
import ChallengeCard from '@/components/ChallengeCard'
|
2024-12-12 17:43:56 +00:00
|
|
|
import PenaltyBanner from '@/components/PenalyBanner'
|
2024-12-12 21:55:59 +00:00
|
|
|
import { useDrawRandomChallengeMutation } from '@/hooks/mutations/useChallengeMutation'
|
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
2024-12-12 18:33:44 +00:00
|
|
|
import { useChallengeActions } from '@/hooks/useChallengeActions'
|
|
|
|
import { useChallenges } from '@/hooks/useChallenges'
|
|
|
|
import { useGame } from '@/hooks/useGame'
|
2024-12-12 19:15:43 +00:00
|
|
|
import { FontAwesome6 } from '@expo/vector-icons'
|
2024-12-12 22:02:02 +00:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
2024-12-12 21:55:59 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2024-12-12 19:15:43 +00:00
|
|
|
import { View } from 'react-native'
|
2024-12-12 21:55:59 +00:00
|
|
|
import { ActivityIndicator, Appbar, Banner, FAB, MD3Colors, Surface, Text, TouchableRipple } from 'react-native-paper'
|
2024-12-01 17:58:53 +00:00
|
|
|
|
2024-12-12 19:15:43 +00:00
|
|
|
function ChallengeScreenHeader() {
|
|
|
|
return <>
|
|
|
|
<Appbar.Header>
|
|
|
|
<Appbar.Content title={"Défis"} />
|
|
|
|
<Appbar.Action icon='format-list-bulleted' />
|
|
|
|
</Appbar.Header>
|
|
|
|
<PenaltyBanner />
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
|
|
|
function ChallengeScreenBody() {
|
2024-12-12 22:02:02 +00:00
|
|
|
const queryClient = useQueryClient()
|
2024-12-12 21:55:59 +00:00
|
|
|
const auth = useAuth()
|
2024-12-12 18:33:44 +00:00
|
|
|
const game = useGame()
|
|
|
|
const challengeActions = useChallengeActions()
|
|
|
|
const challenges = useChallenges()
|
|
|
|
const currentChallengeAction = useMemo(() => {
|
2024-12-12 21:55:59 +00:00
|
|
|
if (!game.activeChallengeId)
|
2024-12-12 18:33:44 +00:00
|
|
|
return null
|
2024-12-12 21:55:59 +00:00
|
|
|
return challengeActions.challengeActions.find((action) => action.id === game.activeChallengeId)
|
2024-12-12 18:33:44 +00:00
|
|
|
}, [game, challengeActions])
|
|
|
|
const currentChallenge = useMemo(() => {
|
|
|
|
if (!currentChallengeAction)
|
|
|
|
return null
|
|
|
|
return challenges.challenges.find((challenge) => challenge.id === currentChallengeAction.challengeId)
|
|
|
|
}, [currentChallengeAction, challenges])
|
2024-12-12 21:55:59 +00:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const drawRandomChallengeMutation = useDrawRandomChallengeMutation({
|
|
|
|
auth,
|
2024-12-12 22:02:02 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setLoading(true)
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' })
|
|
|
|
}
|
2024-12-12 21:55:59 +00:00
|
|
|
})
|
|
|
|
useEffect(() => {
|
|
|
|
if (challengeActions)
|
|
|
|
setLoading(false)
|
|
|
|
}, [challengeActions])
|
2024-12-12 18:33:44 +00:00
|
|
|
|
2024-12-12 19:15:43 +00:00
|
|
|
return <>
|
|
|
|
{currentChallenge && <ChallengeCard challenge={currentChallenge} />}
|
|
|
|
{!currentChallenge && game.currentRunner && <>
|
|
|
|
<Banner
|
2024-12-12 21:55:59 +00:00
|
|
|
visible={!currentChallenge && game.currentRunner && !loading}
|
2024-12-12 19:15:43 +00:00
|
|
|
icon='cancel'
|
|
|
|
style={{ backgroundColor: MD3Colors.error40 }}>
|
|
|
|
<Text variant='titleMedium' style={{ textAlign: 'center' }}>Aucun défi en cours.</Text>
|
|
|
|
</Banner>
|
|
|
|
<View style={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}>
|
2024-12-12 21:55:59 +00:00
|
|
|
<FAB
|
|
|
|
label='Tirer un défi'
|
|
|
|
icon='cards'
|
|
|
|
disabled={drawRandomChallengeMutation.isPending}
|
|
|
|
visible={!currentChallenge && game.currentRunner && !loading}
|
|
|
|
onPress={() => drawRandomChallengeMutation.mutate()}
|
|
|
|
variant='tertiary'
|
|
|
|
customSize={64} />
|
|
|
|
{loading && <ActivityIndicator size={'large'} />}
|
2024-12-12 19:15:43 +00:00
|
|
|
</View>
|
|
|
|
</>}
|
|
|
|
<Banner
|
|
|
|
visible={game.gameStarted && !game.currentRunner}
|
|
|
|
icon={({ size }) => <FontAwesome6 name='cat' size={size} color={'pink'} />}
|
|
|
|
style={{ backgroundColor: MD3Colors.secondary30 }}>
|
|
|
|
Vous êtes poursuiveuse, et n'avez donc pas de défi à accomplir.
|
|
|
|
</Banner>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ChallengesScreen() {
|
2024-12-01 17:58:53 +00:00
|
|
|
return (
|
2024-12-12 17:01:08 +00:00
|
|
|
<Surface style={{ flex: 1 }}>
|
2024-12-12 19:15:43 +00:00
|
|
|
<ChallengeScreenHeader />
|
|
|
|
<ChallengeScreenBody />
|
2024-12-09 20:00:15 +00:00
|
|
|
</Surface>
|
2024-12-07 09:24:41 +00:00
|
|
|
)
|
2024-12-01 17:58:53 +00:00
|
|
|
}
|