Enregistrement de trajet en train

This commit is contained in:
Emmy D'Anello 2024-12-11 23:42:22 +01:00
parent 54a7806316
commit 33ee18d7e2
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 110 additions and 6 deletions

View File

@ -33,7 +33,7 @@ export default function TabLayout() {
<Tabs.Screen <Tabs.Screen
name="train" name="train"
options={{ options={{
title: 'Ajouter un trajet', title: 'Trains',
headerTitleStyle: {fontSize: 32}, headerTitleStyle: {fontSize: 32},
tabBarIcon: ({ color }) => <FontAwesome6 name="train" size={24} color={color} />, tabBarIcon: ({ color }) => <FontAwesome6 name="train" size={24} color={color} />,
}} }}

View File

@ -27,7 +27,7 @@ export default function MapScreen() {
visible={game.gameStarted || game.money > 0} visible={game.gameStarted || game.money > 0}
size='small' size='small'
color='black' color='black'
icon={game.currentRunner ? 'run-fast' : 'police-badge'} icon={game.currentRunner ? 'run-fast' : () => <FontAwesome6 name='cat' size={20} />}
label={game.currentRunner ? "Coureuse" : "Poursuiveuse"} /> label={game.currentRunner ? "Coureuse" : "Poursuiveuse"} />
</Surface> </Surface>
) )

View File

@ -1,10 +1,65 @@
import { ScrollView } from 'react-native' import { useAddTrainMutation } from '@/hooks/mutations/useTrainMutation'
import { Surface, Text } from 'react-native-paper' import { useAuth } from '@/hooks/useAuth'
import { useMemo, useState } from 'react'
import { StyleSheet } from 'react-native'
import { Button, Dialog, FAB, HelperText, Portal, Surface, Text, TextInput } from 'react-native-paper'
export default function TrainScreen() { export default function TrainScreen() {
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])
const auth = useAuth()
const addTrainMutation = useAddTrainMutation({
auth,
onPostSuccess: () => setAddTrainVisible(false)
})
return ( return (
<Surface> <Surface style={{ flex: 1 }}>
<Text>Ici on aura la page pour ajouter un trajet en train depuis Rail Planner</Text> <Text variant='bodyMedium'>Ici on aura la page pour ajouter un trajet en train depuis Rail Planner</Text>
<FAB
icon='plus'
style={styles.addTrainButton}
onPress={() => setAddTrainVisible(true)} />
<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>
</Surface> </Surface>
) )
} }
const styles = StyleSheet.create({
addTrainButton: {
position: 'absolute',
right: 25,
bottom: 25,
}
})

View File

@ -0,0 +1,49 @@
import { AuthState } from "@/utils/features/auth/authSlice"
import { useMutation } from "@tanstack/react-query"
type ErrorResponse = {
error: string
message: string
statusCode: number
}
type onPostSuccessFunc = () => void
type ErrorFuncProps = { response?: ErrorResponse, error?: Error }
type onErrorFunc = (props: ErrorFuncProps) => void
type TrainProps = {
// addTrain: (payload: TrainPayload) => { payload: GamePayload, type: "train/addTrain" }
auth: AuthState
onPostSuccess?: onPostSuccessFunc
onError?: onErrorFunc
}
export const useAddTrainMutation = ({ auth, onPostSuccess, onError }: TrainProps) => {
return useMutation({
mutationFn: async (trainId: string) => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/trains/import/`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
id: trainId,
}),
}).then(resp => resp.json())
},
onSuccess: async (data) => {
if (data.error) {
if (onError)
onError({ response: data })
return
}
if (onPostSuccess)
onPostSuccess()
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}