2024-12-12 17:43:56 +00:00
|
|
|
import PenaltyBanner from '@/components/PenalyBanner'
|
2024-12-11 22:42:22 +00:00
|
|
|
import { useAddTrainMutation } from '@/hooks/mutations/useTrainMutation'
|
|
|
|
import { useAuth } from '@/hooks/useAuth'
|
2024-12-12 13:56:17 +00:00
|
|
|
import { useTrain } from '@/hooks/useTrain'
|
|
|
|
import { TrainTrip } from '@/utils/features/train/trainSlice'
|
|
|
|
import { FontAwesome6 } from '@expo/vector-icons'
|
2024-12-14 00:44:32 +00:00
|
|
|
import { useQueryClient } from '@tanstack/react-query'
|
2024-12-16 16:52:08 +00:00
|
|
|
import { useShareIntentContext } from 'expo-share-intent'
|
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2024-12-12 13:56:17 +00:00
|
|
|
import { FlatList, StyleSheet } from 'react-native'
|
2024-12-14 00:44:32 +00:00
|
|
|
import { Button, Dialog, Divider, FAB, HelperText, List, MD3Colors, Portal, Snackbar, Surface, Text, TextInput } from 'react-native-paper'
|
2024-12-01 17:58:53 +00:00
|
|
|
|
|
|
|
export default function TrainScreen() {
|
2024-12-11 22:42:22 +00:00
|
|
|
const [addTrainVisible, setAddTrainVisible] = useState(false)
|
|
|
|
const [addTrainUrl, setAddTrainUrl] = useState("")
|
|
|
|
const trainId = useMemo(() => /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/.exec(addTrainUrl)?.[0], [addTrainUrl])
|
|
|
|
|
2024-12-14 00:44:32 +00:00
|
|
|
const [successSnackbarVisible, setSuccessSnackbarVisible] = useState(false)
|
|
|
|
const [errorVisible, setErrorVisible] = useState(false)
|
|
|
|
const [error, setError] = useState([200, ""])
|
|
|
|
|
2024-12-11 22:42:22 +00:00
|
|
|
const auth = useAuth()
|
2024-12-14 00:44:32 +00:00
|
|
|
const queryClient = useQueryClient()
|
2024-12-11 22:42:22 +00:00
|
|
|
const addTrainMutation = useAddTrainMutation({
|
|
|
|
auth,
|
2024-12-14 00:44:32 +00:00
|
|
|
onPostSuccess: () => {
|
|
|
|
setAddTrainVisible(false)
|
|
|
|
setSuccessSnackbarVisible(true)
|
|
|
|
queryClient.invalidateQueries({ predicate: (query) => query.queryKey[0] === 'get-trains' })
|
|
|
|
},
|
|
|
|
onError: ({ response, error }) => {
|
|
|
|
setErrorVisible(true)
|
|
|
|
if (response)
|
|
|
|
setError([response.statusCode, response.message])
|
|
|
|
else if (error)
|
|
|
|
setError([400, error.message])
|
|
|
|
},
|
2024-12-11 22:42:22 +00:00
|
|
|
})
|
|
|
|
|
2024-12-12 13:56:17 +00:00
|
|
|
const trains = useTrain()
|
|
|
|
|
2024-12-16 16:52:08 +00:00
|
|
|
|
|
|
|
const { hasShareIntent, shareIntent, resetShareIntent } = useShareIntentContext()
|
|
|
|
useEffect(() => {
|
|
|
|
if (hasShareIntent) {
|
|
|
|
resetShareIntent()
|
|
|
|
if (!shareIntent.text || !shareIntent.text.includes("eurailapp.com/share"))
|
|
|
|
return
|
|
|
|
const parsedTrainId = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/.exec(shareIntent.text)?.[0]
|
|
|
|
if (parsedTrainId)
|
|
|
|
addTrainMutation.mutate(parsedTrainId)
|
|
|
|
else {
|
|
|
|
setErrorVisible(true)
|
|
|
|
setError([400, "Impossible de récupérer l'identifiant du train à ajouter"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [hasShareIntent])
|
|
|
|
|
2024-12-01 17:58:53 +00:00
|
|
|
return (
|
2024-12-11 22:42:22 +00:00
|
|
|
<Surface style={{ flex: 1 }}>
|
2024-12-12 17:43:56 +00:00
|
|
|
<PenaltyBanner />
|
2024-12-12 13:56:17 +00:00
|
|
|
<FlatList
|
2024-12-13 23:27:30 +00:00
|
|
|
data={trains}
|
2024-12-12 14:38:47 +00:00
|
|
|
keyExtractor={(train) => train.id}
|
|
|
|
ItemSeparatorComponent={() => <Divider />}
|
2024-12-12 13:56:17 +00:00
|
|
|
renderItem={(item) => <TrainListItem train={item.item} />} />
|
2024-12-11 22:42:22 +00:00
|
|
|
<FAB
|
|
|
|
icon='plus'
|
|
|
|
style={styles.addTrainButton}
|
|
|
|
onPress={() => setAddTrainVisible(true)} />
|
2024-12-14 00:44:32 +00:00
|
|
|
<Snackbar
|
|
|
|
key='success-snackbar'
|
|
|
|
visible={successSnackbarVisible}
|
|
|
|
icon={'close'}
|
|
|
|
onDismiss={() => setSuccessSnackbarVisible(false)}
|
|
|
|
onIconPress={() => setSuccessSnackbarVisible(false)}>
|
|
|
|
Train ajouté avec succès
|
|
|
|
</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>
|
2024-12-11 22:42:22 +00:00
|
|
|
<Portal>
|
|
|
|
<Dialog visible={addTrainVisible} onDismiss={() => setAddTrainVisible(false)}>
|
|
|
|
<Dialog.Title>Ajout d'un train</Dialog.Title>
|
|
|
|
<Dialog.Content>
|
|
|
|
<TextInput
|
|
|
|
label="URL de partage RailPlanner"
|
|
|
|
autoComplete='url'
|
|
|
|
inputMode='url'
|
|
|
|
defaultValue={addTrainUrl}
|
|
|
|
multiline={true}
|
|
|
|
onChangeText={setAddTrainUrl}
|
|
|
|
error={!trainId}
|
|
|
|
onEndEditing={() => {
|
|
|
|
if (trainId !== undefined)
|
|
|
|
addTrainMutation.mutate(trainId)
|
|
|
|
}}
|
|
|
|
placeholder="https://eurailapp.com/share/journey?id=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&type=list&brand=interrail" />
|
|
|
|
<HelperText type='error' visible={!trainId && addTrainVisible}>
|
|
|
|
Le champ doit contenir l'identifiant d'un voyage au format UUID. {trainId}
|
|
|
|
</HelperText>
|
|
|
|
</Dialog.Content>
|
|
|
|
<Dialog.Actions>
|
|
|
|
<Button onPress={() => setAddTrainVisible(false)}>Annuler</Button>
|
|
|
|
<Button onPress={() => {
|
|
|
|
if (trainId !== undefined)
|
|
|
|
addTrainMutation.mutate(trainId)
|
|
|
|
}} disabled={trainId === undefined || addTrainMutation.isPending}>Ajouter</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-11 22:42:22 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
addTrainButton: {
|
|
|
|
position: 'absolute',
|
|
|
|
right: 25,
|
|
|
|
bottom: 25,
|
|
|
|
}
|
|
|
|
})
|
2024-12-12 13:56:17 +00:00
|
|
|
|
|
|
|
function TrainListItem({ train }: { train: TrainTrip }) {
|
|
|
|
const depDateTime = new Date(train.departureTime)
|
|
|
|
const depDate = depDateTime.toLocaleDateString()
|
|
|
|
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' })
|
|
|
|
const durationInMinutes = (arrDateTime.getTime() - depDateTime.getTime()) / 1000 / 60
|
|
|
|
const duration = `${Math.floor(durationInMinutes / 60).toString().padStart(2, '0')}:${Math.floor(durationInMinutes % 60).toString().padStart(2, '0')}`
|
|
|
|
const title = `${train.from} ${depTime} => ${train.to} ${arrTime} (${depDate})`
|
|
|
|
const distanceKm = Math.ceil(train.distance / 1000)
|
|
|
|
const cost = 10 * distanceKm
|
|
|
|
return <>
|
|
|
|
<List.Item
|
|
|
|
title={title}
|
|
|
|
description={<><Text>Durée : {duration}, distance : {distanceKm} km, coût : {cost}</Text> <FontAwesome6 name='coins' /></>}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
}
|