2024-12-14 14:57:14 +00:00
|
|
|
import { useDeleteChallengeActionMutation } from '@/hooks/mutations/useChallengeMutation'
|
|
|
|
import { useGameRepairMutation } from '@/hooks/mutations/useGameMutation'
|
|
|
|
import { useDeleteTrainMutation } from '@/hooks/mutations/useTrainMutation'
|
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
2024-12-13 23:27:30 +00:00
|
|
|
import { useChallengeActions } from '@/hooks/useChallengeActions'
|
|
|
|
import { useChallenges } from '@/hooks/useChallenges'
|
2024-12-13 22:07:37 +00:00
|
|
|
import { useMoneyUpdates } from '@/hooks/useMoneyUpdates'
|
2024-12-13 23:27:30 +00:00
|
|
|
import { useTrain } from '@/hooks/useTrain'
|
|
|
|
import { MoneyUpdate } from '@/utils/features/moneyUpdates/moneyUpdatesSlice'
|
|
|
|
import { FontAwesome6 } from '@expo/vector-icons'
|
2024-12-14 14:57:14 +00:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
|
|
|
import { useMemo, useState } from 'react'
|
2024-12-13 23:27:30 +00:00
|
|
|
import { FlatList } from 'react-native'
|
2024-12-14 14:57:14 +00:00
|
|
|
import { Button, Dialog, Divider, List, MD3Colors, Portal, Snackbar, Surface, Text } from 'react-native-paper'
|
2024-12-01 17:58:53 +00:00
|
|
|
|
|
|
|
export default function HistoryScreen() {
|
2024-12-14 14:57:14 +00:00
|
|
|
const auth = useAuth()
|
|
|
|
const queryClient = useQueryClient()
|
2024-12-13 22:07:37 +00:00
|
|
|
const moneyUpdates = useMoneyUpdates()
|
2024-12-14 14:57:14 +00:00
|
|
|
const [deletingMoneyUpdate, setDeletingMoneyUpdate] = useState<MoneyUpdate | null>(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)
|
|
|
|
}
|
2024-12-13 22:07:37 +00:00
|
|
|
|
2024-12-01 17:58:53 +00:00
|
|
|
return (
|
2024-12-13 23:27:30 +00:00
|
|
|
<Surface style={{ flex :1 }}>
|
|
|
|
<FlatList
|
|
|
|
data={moneyUpdates}
|
|
|
|
keyExtractor={(moneyUpdate) => `money-update-list-item-${moneyUpdate.id}`}
|
|
|
|
ItemSeparatorComponent={() => <Divider />}
|
2024-12-14 14:57:14 +00:00
|
|
|
renderItem={(item) =>
|
|
|
|
<MoneyUpdateListItem moneyUpdate={item.item}
|
|
|
|
onDelete={['START', 'NEW_RUN'].includes(item.item.reason) ? undefined : () => setDeletingMoneyUpdate(item.item)} />} />
|
|
|
|
<Snackbar
|
|
|
|
key='success-snackbar'
|
|
|
|
visible={successVisible}
|
|
|
|
icon={'close'}
|
|
|
|
onDismiss={() => setSuccessVisible(false)}
|
|
|
|
onIconPress={() => setSuccessVisible(false)}>
|
|
|
|
<Text variant='bodyMedium' style={{ color: MD3Colors.secondary0 }}>
|
|
|
|
{successMessage}
|
|
|
|
</Text>
|
|
|
|
</Snackbar>
|
|
|
|
<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>
|
|
|
|
<Portal>
|
|
|
|
<Dialog visible={deletingMoneyUpdate !== null} onDismiss={() => setDeletingMoneyUpdate(null)}>
|
|
|
|
<Dialog.Title>Êtes-vous sûre ?</Dialog.Title>
|
|
|
|
<Dialog.Content>
|
|
|
|
<Text>Voulez-vous vraiment supprimer ce mouvement de fonds ?</Text>
|
|
|
|
{deletingMoneyUpdate && <MoneyUpdateListItem moneyUpdate={deletingMoneyUpdate} />}
|
|
|
|
</Dialog.Content>
|
|
|
|
<Dialog.Actions>
|
|
|
|
<Button onPress={() => setDeletingMoneyUpdate(null)}>Annuler</Button>
|
|
|
|
<Button onPress={() => deleteMoneyUpdate()}>Confirmer</Button>
|
|
|
|
</Dialog.Actions>
|
|
|
|
</Dialog>
|
|
|
|
</Portal>
|
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
|
|
|
}
|
2024-12-13 23:27:30 +00:00
|
|
|
|
2024-12-14 14:57:14 +00:00
|
|
|
function MoneyUpdateListItem({ moneyUpdate, onDelete }: { moneyUpdate: MoneyUpdate, onDelete?: () => void }) {
|
2024-12-13 23:27:30 +00:00
|
|
|
const trains = useTrain()
|
|
|
|
const challengeActions = useChallengeActions()
|
|
|
|
const challenges = useChallenges()
|
|
|
|
|
|
|
|
const icon = useMemo(() => {
|
|
|
|
switch (moneyUpdate.reason) {
|
|
|
|
case 'START': return 'star'
|
|
|
|
case 'NEW_RUN': return 'run'
|
|
|
|
case 'BUY_TRAIN': return 'train'
|
2024-12-14 12:30:26 +00:00
|
|
|
case 'CHALLENGE': return 'cards'
|
2024-12-13 23:27:30 +00:00
|
|
|
}
|
|
|
|
}, [moneyUpdate.reason])
|
|
|
|
|
|
|
|
const title = useMemo(() => {
|
|
|
|
switch (moneyUpdate.reason) {
|
|
|
|
case 'START':
|
|
|
|
return "Début de la partie"
|
|
|
|
case 'NEW_RUN':
|
|
|
|
return "Nouvelle tentative"
|
|
|
|
case 'BUY_TRAIN':
|
|
|
|
const train = trains.find((train) => train.id === moneyUpdate.tripId)
|
|
|
|
if (!train) return "Train"
|
|
|
|
const depDateTime = new Date(train.departureTime)
|
|
|
|
const depTime = depDateTime.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
|
|
|
|
const arrDateTime = new Date(train.arrivalTime)
|
|
|
|
const arrTime = arrDateTime.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
|
|
|
|
return `${train.from} ${depTime} => ${train.to} ${arrTime}`
|
2024-12-14 12:30:26 +00:00
|
|
|
case 'CHALLENGE':
|
2024-12-13 23:27:30 +00:00
|
|
|
const challengeAction = challengeActions.find((challengeAction) => challengeAction.id === moneyUpdate.actionId)
|
|
|
|
if (!challengeAction) return "Défi"
|
|
|
|
const challenge = challenges.find((challenge) => challenge.id === challengeAction.challengeId)
|
|
|
|
if (!challenge) return "Défi"
|
|
|
|
return challenge.title
|
|
|
|
}
|
|
|
|
}, [moneyUpdate.reason, moneyUpdate.tripId, moneyUpdate.actionId])
|
|
|
|
|
|
|
|
const description = useMemo(() => {
|
|
|
|
const earnDate = new Date(moneyUpdate.timestamp).toLocaleDateString(undefined, { day: '2-digit', month: '2-digit' })
|
|
|
|
const earnTime = new Date(moneyUpdate.timestamp).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })
|
2024-12-14 12:30:26 +00:00
|
|
|
const verb = moneyUpdate.amount >= 0 ? "Gagné" : "Dépensé"
|
2024-12-13 23:27:30 +00:00
|
|
|
return <Text>
|
2024-12-14 14:57:14 +00:00
|
|
|
{verb} {Math.abs(moneyUpdate.amount)} <FontAwesome6 name='coins' /> le {earnDate} à {earnTime}
|
2024-12-13 23:27:30 +00:00
|
|
|
</Text>
|
|
|
|
}, [moneyUpdate.amount])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<List.Item
|
|
|
|
left={(props) => <List.Icon {...props} icon={icon} />}
|
|
|
|
title={title}
|
|
|
|
description={description}
|
2024-12-14 14:57:14 +00:00
|
|
|
onLongPress={onDelete} />
|
2024-12-13 23:27:30 +00:00
|
|
|
)
|
|
|
|
}
|