From 3348979738b1894f223c6b07ca264cef50ab7971 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Sat, 14 Dec 2024 15:57:14 +0100 Subject: [PATCH] =?UTF-8?q?Suppression=20de=20trains=20et=20d=C3=A9fis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/app/(tabs)/history.tsx | 124 +++++++++++++++++- .../hooks/mutations/useChallengeMutation.ts | 35 ++++- client/hooks/mutations/useGameMutation.ts | 2 +- client/hooks/mutations/useTrainMutation.ts | 27 ++++ 4 files changed, 177 insertions(+), 11 deletions(-) diff --git a/client/app/(tabs)/history.tsx b/client/app/(tabs)/history.tsx index 40ad072..b390827 100644 --- a/client/app/(tabs)/history.tsx +++ b/client/app/(tabs)/history.tsx @@ -1,15 +1,92 @@ +import { useDeleteChallengeActionMutation } from '@/hooks/mutations/useChallengeMutation' +import { useGameRepairMutation } from '@/hooks/mutations/useGameMutation' +import { useDeleteTrainMutation } from '@/hooks/mutations/useTrainMutation' +import { useAuth } from '@/hooks/useAuth' import { useChallengeActions } from '@/hooks/useChallengeActions' import { useChallenges } from '@/hooks/useChallenges' import { useMoneyUpdates } from '@/hooks/useMoneyUpdates' import { useTrain } from '@/hooks/useTrain' import { MoneyUpdate } from '@/utils/features/moneyUpdates/moneyUpdatesSlice' import { FontAwesome6 } from '@expo/vector-icons' -import { useMemo } from 'react' +import { useQueryClient } from '@tanstack/react-query' +import { useMemo, useState } from 'react' import { FlatList } from 'react-native' -import { Divider, FAB, List, Surface, Text } from 'react-native-paper' +import { Button, Dialog, Divider, List, MD3Colors, Portal, Snackbar, Surface, Text } from 'react-native-paper' export default function HistoryScreen() { + const auth = useAuth() + const queryClient = useQueryClient() const moneyUpdates = useMoneyUpdates() + const [deletingMoneyUpdate, setDeletingMoneyUpdate] = useState(null) + + const [successVisible, setSuccessVisible] = useState(false) + const [successMessage, setSuccessMessage] = useState("") + const [errorVisible, setErrorVisible] = useState(false) + const [error, setError] = useState([200, ""]) + + const deleteTrainMutation = useDeleteTrainMutation({ + auth, + onPostSuccess: () => { + setDeletingMoneyUpdate(null) + setSuccessVisible(true) + setSuccessMessage("Train supprimé") + gameRepairMutation.mutate() + queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-money-updates' || query.queryKey[0] === 'get-trains' }) + }, + onError: ({ response, error }) => { + setErrorVisible(true) + if (response) + setError([response.statusCode, response.message]) + else if (error) + setError([400, error.message]) + }, + }) + const deleteChallengeActionMutation = useDeleteChallengeActionMutation({ + auth, + onPostSuccess: () => { + setDeletingMoneyUpdate(null) + setSuccessVisible(true) + setSuccessMessage("Réalisation du défi supprimée") + gameRepairMutation.mutate() + queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-money-updates' || query.queryKey[0] === 'get-challenges' }) + }, + onError: ({ response, error }) => { + setErrorVisible(true) + if (response) + setError([response.statusCode, response.message]) + else if (error) + setError([400, error.message]) + }, + }) + const gameRepairMutation = useGameRepairMutation({ + auth, + onPostSuccess: () => { + queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-player' }) + }, + onError: ({ response, error }) => { + setErrorVisible(true) + if (response) + setError([response.statusCode, response.message]) + else if (error) + setError([400, error.message]) + }, + }) + + function deleteMoneyUpdate(): void { + if (!deletingMoneyUpdate) + return + switch (deletingMoneyUpdate.reason) { + case 'BUY_TRAIN': + if (!deletingMoneyUpdate.tripId) return + deleteTrainMutation.mutate(deletingMoneyUpdate.tripId) + break + case 'CHALLENGE': + if (!deletingMoneyUpdate.actionId) return + deleteChallengeActionMutation.mutate(deletingMoneyUpdate.actionId) + break + } + setDeletingMoneyUpdate(null) + } return ( @@ -17,12 +94,47 @@ export default function HistoryScreen() { data={moneyUpdates} keyExtractor={(moneyUpdate) => `money-update-list-item-${moneyUpdate.id}`} ItemSeparatorComponent={() => } - renderItem={(item) => } /> + renderItem={(item) => + setDeletingMoneyUpdate(item.item)} />} /> + setSuccessVisible(false)} + onIconPress={() => setSuccessVisible(false)}> + + {successMessage} + + + setErrorVisible(false)} + onIconPress={() => setErrorVisible(false)}> + + Erreur {error[0]} : {error[1]} + + + + setDeletingMoneyUpdate(null)}> + Êtes-vous sûre ? + + Voulez-vous vraiment supprimer ce mouvement de fonds ? + {deletingMoneyUpdate && } + + + + + + + ) } -function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) { +function MoneyUpdateListItem({ moneyUpdate, onDelete }: { moneyUpdate: MoneyUpdate, onDelete?: () => void }) { const trains = useTrain() const challengeActions = useChallengeActions() const challenges = useChallenges() @@ -64,7 +176,7 @@ function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) { const earnTime = new Date(moneyUpdate.timestamp).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' }) const verb = moneyUpdate.amount >= 0 ? "Gagné" : "Dépensé" return - {verb} {moneyUpdate.amount} le {earnDate} à {earnTime} + {verb} {Math.abs(moneyUpdate.amount)} le {earnDate} à {earnTime} }, [moneyUpdate.amount]) @@ -73,6 +185,6 @@ function MoneyUpdateListItem({ moneyUpdate }: { moneyUpdate: MoneyUpdate }) { left={(props) => } title={title} description={description} - right={(props) => {}} />} /> + onLongPress={onDelete} /> ) } diff --git a/client/hooks/mutations/useChallengeMutation.ts b/client/hooks/mutations/useChallengeMutation.ts index 7a89618..baa6cb8 100644 --- a/client/hooks/mutations/useChallengeMutation.ts +++ b/client/hooks/mutations/useChallengeMutation.ts @@ -81,6 +81,33 @@ export const useEndChallenge = ({ auth, onPostSuccess, onError }: ChallengeActio }) } +export const useDeleteChallengeActionMutation = ({ auth, onPostSuccess, onError }: ChallengeActionProps) => { + return useMutation({ + mutationFn: async (challengeActionId: number) => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/challenge-actions/${challengeActionId}/`, { + method: "DELETE", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }) + }, + onSuccess: async (resp) => { + if (resp.status >= 400) { + if (onError) + onError({ response: await resp.json() }) + return + } + if (onPostSuccess) + onPostSuccess() + }, + onError: async (error: Error) => { + if (onError) + onError({ error: error }) + } + }) +} + export const useAddChallengeMutation = ({ auth, onPostSuccess, onError }: ChallengeProps) => { return useMutation({ mutationFn: async (challenge: Omit) => { @@ -154,12 +181,12 @@ export const useDeleteChallengeMutation = ({ auth, onPostSuccess, onError }: Cha "Authorization": `Bearer ${auth.token}`, "Content-Type": "application/json", }, - }).then(resp => resp.json()) + }) }, - onSuccess: async (data) => { - if (data.statusCode) { + onSuccess: async (resp) => { + if (resp.status >= 400) { if (onError) - onError({ response: data }) + onError({ response: await resp.json() }) return } if (onPostSuccess) diff --git a/client/hooks/mutations/useGameMutation.ts b/client/hooks/mutations/useGameMutation.ts index f622515..ebaa862 100644 --- a/client/hooks/mutations/useGameMutation.ts +++ b/client/hooks/mutations/useGameMutation.ts @@ -103,7 +103,7 @@ export const useGameSwitchPlayerMutation = ({ auth, updateGameState, onPostSucce }) } -export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: GameProps) => { +export const useGameRepairMutation = ({ auth, onPostSuccess, onError }: Omit) => { return useMutation({ mutationFn: async () => { return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/repair/`, { diff --git a/client/hooks/mutations/useTrainMutation.ts b/client/hooks/mutations/useTrainMutation.ts index a18d367..7046852 100644 --- a/client/hooks/mutations/useTrainMutation.ts +++ b/client/hooks/mutations/useTrainMutation.ts @@ -47,3 +47,30 @@ export const useAddTrainMutation = ({ auth, onPostSuccess, onError }: TrainProps } }) } + +export const useDeleteTrainMutation = ({ auth, onPostSuccess, onError }: TrainProps) => { + return useMutation({ + mutationFn: async (trainId: string) => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/${trainId}/`, { + method: "DELETE", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }) + }, + onSuccess: async (resp) => { + if (resp.status >= 400) { + if (onError) + onError({ response: await resp.json() }) + return + } + if (onPostSuccess) + onPostSuccess() + }, + onError: async (error: Error) => { + if (onError) + onError({ error: error }) + } + }) +}