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 22:41:20 +00:00
|
|
|
import { useDrawRandomChallengeMutation, useEndChallenge } from '@/hooks/mutations/useChallengeMutation'
|
2024-12-12 21:55:59 +00:00
|
|
|
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-14 16:18:41 +00:00
|
|
|
import { FontAwesome6, MaterialCommunityIcons } from '@expo/vector-icons'
|
2024-12-12 22:02:02 +00:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
2024-12-14 10:55:11 +00:00
|
|
|
import { useRouter } from 'expo-router'
|
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 22:41:20 +00:00
|
|
|
import { ActivityIndicator, Appbar, Banner, FAB, MD3Colors, Snackbar, 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() {
|
2024-12-14 10:55:11 +00:00
|
|
|
const router = useRouter()
|
2024-12-12 19:15:43 +00:00
|
|
|
return <>
|
|
|
|
<Appbar.Header>
|
2024-12-14 10:55:11 +00:00
|
|
|
<Appbar.Content title={"Défi en cours"} />
|
|
|
|
<Appbar.Action icon='format-list-bulleted' onPress={() => router.navigate('/challenges-list')} />
|
2024-12-12 19:15:43 +00:00
|
|
|
</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-13 23:27:30 +00:00
|
|
|
return challengeActions.find((action) => action.id === game.activeChallengeId)
|
2024-12-12 18:33:44 +00:00
|
|
|
}, [game, challengeActions])
|
|
|
|
const currentChallenge = useMemo(() => {
|
|
|
|
if (!currentChallengeAction)
|
|
|
|
return null
|
2024-12-13 23:27:30 +00:00
|
|
|
return challenges.find((challenge) => challenge.id === currentChallengeAction.challengeId)
|
2024-12-12 18:33:44 +00:00
|
|
|
}, [currentChallengeAction, challenges])
|
2024-12-12 21:55:59 +00:00
|
|
|
const [loading, setLoading] = useState(false)
|
2024-12-12 22:41:20 +00:00
|
|
|
const [successSnackbarVisible, setSuccessSnackbarVisible] = useState(false)
|
2024-12-14 00:44:32 +00:00
|
|
|
const [errorVisible, setErrorVisible] = useState(false)
|
|
|
|
const [error, setError] = useState([200, ""])
|
2024-12-12 21:55:59 +00:00
|
|
|
const drawRandomChallengeMutation = useDrawRandomChallengeMutation({
|
|
|
|
auth,
|
2024-12-12 22:02:02 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setLoading(true)
|
2024-12-12 22:41:20 +00:00
|
|
|
setSuccessSnackbarVisible(true)
|
2024-12-12 22:02:02 +00:00
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' })
|
2024-12-14 00:44:32 +00:00
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-12 21:55:59 +00:00
|
|
|
})
|
2024-12-12 22:41:20 +00:00
|
|
|
const endChallenge = useEndChallenge({
|
|
|
|
auth,
|
|
|
|
onPostSuccess: () => {
|
|
|
|
setLoading(true)
|
|
|
|
setSuccessSnackbarVisible(true)
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' })
|
|
|
|
},
|
2024-12-14 00:44:32 +00:00
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-12 22:41:20 +00:00
|
|
|
})
|
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 <>
|
2024-12-12 22:41:20 +00:00
|
|
|
{loading &&
|
|
|
|
<View style={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
|
|
<ActivityIndicator size={'large'} />
|
|
|
|
</View>}
|
|
|
|
{!loading && currentChallenge &&
|
|
|
|
<ChallengeCard
|
|
|
|
challenge={currentChallenge}
|
|
|
|
onSuccess={() => { setLoading(true); endChallenge.mutate({ success: true }) }}
|
2024-12-14 10:55:11 +00:00
|
|
|
onFail={() => endChallenge.mutate({ success: false })}
|
|
|
|
style={{ flex: 1, margin: 20 }} />}
|
2024-12-12 23:02:58 +00:00
|
|
|
{!loading && !game.penaltyEnd && !currentChallenge && game.currentRunner && <>
|
2024-12-12 19:15:43 +00:00
|
|
|
<Banner
|
2024-12-14 16:18:41 +00:00
|
|
|
elevation={4}
|
2024-12-12 21:55:59 +00:00
|
|
|
visible={!currentChallenge && game.currentRunner && !loading}
|
2024-12-14 16:18:41 +00:00
|
|
|
icon='vanish'>
|
|
|
|
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.
|
2024-12-12 19:15:43 +00:00
|
|
|
</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} />
|
2024-12-12 19:15:43 +00:00
|
|
|
</View>
|
|
|
|
</>}
|
|
|
|
<Banner
|
2024-12-12 22:41:20 +00:00
|
|
|
visible={!loading && game.gameStarted && !game.currentRunner}
|
2024-12-12 19:15:43 +00:00
|
|
|
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>
|
2024-12-12 22:41:20 +00:00
|
|
|
<Snackbar
|
2024-12-14 00:44:32 +00:00
|
|
|
key='success-snackbar'
|
2024-12-12 22:41:20 +00:00
|
|
|
visible={successSnackbarVisible}
|
2024-12-14 00:44:32 +00:00
|
|
|
icon={'close'}
|
2024-12-12 22:41:20 +00:00
|
|
|
onDismiss={() => setSuccessSnackbarVisible(false)}
|
2024-12-14 00:44:32 +00:00
|
|
|
onIconPress={() => setSuccessSnackbarVisible(false)}>
|
2024-12-12 22:41:20 +00:00
|
|
|
Jeu actualisé
|
|
|
|
</Snackbar>
|
2024-12-14 00:44:32 +00:00
|
|
|
<Snackbar
|
|
|
|
key='error-snackbar'
|
|
|
|
visible={errorVisible}
|
|
|
|
icon={'close'}
|
|
|
|
onDismiss={() => setErrorVisible(false)}
|
|
|
|
onIconPress={() => setErrorVisible(false)}>
|
|
|
|
<Text variant='bodyMedium' style={{ color: MD3Colors.secondary0 }}>
|
|
|
|
Erreur {error[0]} : {error[1]}
|
|
|
|
</Text>
|
|
|
|
</Snackbar>
|
2024-12-12 19:15:43 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|