diff --git a/client/app/(tabs)/challenges.tsx b/client/app/(tabs)/challenges.tsx index 61514ff..127f0c1 100644 --- a/client/app/(tabs)/challenges.tsx +++ b/client/app/(tabs)/challenges.tsx @@ -1,6 +1,6 @@ import ChallengeCard from '@/components/ChallengeCard' import PenaltyBanner from '@/components/PenalyBanner' -import { useDrawRandomChallengeMutation } from '@/hooks/mutations/useChallengeMutation' +import { useDrawRandomChallengeMutation, useEndChallenge } from '@/hooks/mutations/useChallengeMutation' import { useAuth } from '@/hooks/useAuth' import { useChallengeActions } from '@/hooks/useChallengeActions' import { useChallenges } from '@/hooks/useChallenges' @@ -9,7 +9,7 @@ import { FontAwesome6 } from '@expo/vector-icons' import { useQueryClient } from '@tanstack/react-query' import { useEffect, useMemo, useState } from 'react' import { View } from 'react-native' -import { ActivityIndicator, Appbar, Banner, FAB, MD3Colors, Surface, Text, TouchableRipple } from 'react-native-paper' +import { ActivityIndicator, Appbar, Banner, FAB, MD3Colors, Snackbar, Surface, Text, TouchableRipple } from 'react-native-paper' function ChallengeScreenHeader() { return <> @@ -38,26 +38,44 @@ function ChallengeScreenBody() { return challenges.challenges.find((challenge) => challenge.id === currentChallengeAction.challengeId) }, [currentChallengeAction, challenges]) const [loading, setLoading] = useState(false) + const [successSnackbarVisible, setSuccessSnackbarVisible] = useState(false) const drawRandomChallengeMutation = useDrawRandomChallengeMutation({ auth, onPostSuccess: () => { setLoading(true) + setSuccessSnackbarVisible(true) queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' }) } }) + const endChallenge = useEndChallenge({ + auth, + onPostSuccess: () => { + setLoading(true) + setSuccessSnackbarVisible(true) + queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-challenges' || query.queryKey[0] === 'get-player' }) + }, + }) useEffect(() => { if (challengeActions) setLoading(false) }, [challengeActions]) return <> - {currentChallenge && } - {!currentChallenge && game.currentRunner && <> + {loading && + + + } + {!loading && currentChallenge && + { setLoading(true); endChallenge.mutate({ success: true }) }} + onFail={() => endChallenge.mutate({ success: false })} />} + {!loading && !currentChallenge && game.currentRunner && <> - Aucun défi en cours. + Aucun défi n'est en cours. drawRandomChallengeMutation.mutate()} variant='tertiary' customSize={64} /> - {loading && } } } style={{ backgroundColor: MD3Colors.secondary30 }}> Vous êtes poursuiveuse, et n'avez donc pas de défi à accomplir. + setSuccessSnackbarVisible(false)} + action={{ label: "Fermer", onPress: () => setSuccessSnackbarVisible(false) }}> + Jeu actualisé + } diff --git a/client/components/ChallengeCard.tsx b/client/components/ChallengeCard.tsx index e03fb8c..70725f5 100644 --- a/client/components/ChallengeCard.tsx +++ b/client/components/ChallengeCard.tsx @@ -1,30 +1,43 @@ import { Challenge } from "@/utils/features/challenges/challengesSlice" import { FontAwesome6 } from "@expo/vector-icons" import { View } from "react-native" -import { Button, Surface, Text } from "react-native-paper" +import { Button, Card, IconButton, MD3Colors, Surface, Text } from "react-native-paper" -export default function ChallengeCard({ challenge }: { challenge: Challenge }) { +export type ChallengeCardProps = { + challenge: Challenge, + onSuccess?: () => void, + onFail?: () => void, + onDelete?: () => void, + onEdit?: () => void, +} + +export default function ChallengeCard({ challenge, onSuccess, onFail, onDelete, onEdit }: ChallengeCardProps) { return ( - - {challenge.title} - - - - {challenge.description} - - Récompense : {challenge.reward} - - - - - - - - + onEdit ? onEdit()} /> : <>} /> + + + {challenge.description} + + Récompense : {challenge.reward} + + + + + {onFail && } + {onSuccess && } + {onDelete && } + + ) } \ No newline at end of file diff --git a/client/hooks/mutations/useChallengeMutation.ts b/client/hooks/mutations/useChallengeMutation.ts index c646734..9e3c1bb 100644 --- a/client/hooks/mutations/useChallengeMutation.ts +++ b/client/hooks/mutations/useChallengeMutation.ts @@ -43,3 +43,33 @@ export const useDrawRandomChallengeMutation = ({ auth, onPostSuccess, onError }: } }) } + +export const useEndChallenge = ({ auth, onPostSuccess, onError }: ChallengeActionProps) => { + return useMutation({ + mutationFn: async ({ success }: { success: boolean }) => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/challenge-actions/end-current/`, { + method: "POST", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + success: success, + }) + }).then(resp => resp.json()) + }, + onSuccess: async (data) => { + if (data.statusCode) { + if (onError) + onError({ response: data }) + return + } + if (onPostSuccess) + onPostSuccess() + }, + onError: async (error: Error) => { + if (onError) + onError({ error: error }) + } + }) +}