diff --git a/client/app/(tabs)/settings.tsx b/client/app/(tabs)/settings.tsx index e3de771..fd5ba1f 100644 --- a/client/app/(tabs)/settings.tsx +++ b/client/app/(tabs)/settings.tsx @@ -1,18 +1,81 @@ +import { useGameRepairMutation, useGameResetMutation, useGameStartMutation, useGameStopMutation, useGameSwitchPlayerMutation } from '@/hooks/mutations/useGameMutation' import { useAuth } from '@/hooks/useAuth' +import { useGame } from '@/hooks/useGame' import { useRouter } from 'expo-router' import { FAB, List, Surface } from 'react-native-paper' export default function HistoryScreen() { const router = useRouter() const auth = useAuth() + const game = useGame() + + const gameStartMutation = useGameStartMutation({ + auth, + game, + }) + const gameStopMutation = useGameStopMutation({ + auth, + game, + }) + const gameSwitchMutation = useGameSwitchPlayerMutation({ + auth, + game, + }) + const gameRepairMutation = useGameRepairMutation({ + auth, + game, + }) + const gameResetMutation = useGameResetMutation({ + auth, + game, + }) + return ( - router.navigate('/login')} />} - onPress={() => router.navigate('/login')} /> + + router.navigate('/login')} />} + onPress={() => router.navigate('/login')} /> + + + } + onPress={() => gameStartMutation.mutate()} /> + } + onPress={() => gameStopMutation.mutate()} /> + } + onPress={() => gameSwitchMutation.mutate()} /> + + + } + onPress={() => gameRepairMutation.mutate()} /> + } + onPress={() => gameResetMutation.mutate()} /> + ) } diff --git a/client/hooks/mutations/useGameMutation.ts b/client/hooks/mutations/useGameMutation.ts new file mode 100644 index 0000000..be8fb69 --- /dev/null +++ b/client/hooks/mutations/useGameMutation.ts @@ -0,0 +1,155 @@ +import { AuthState } from "@/utils/features/auth/authSlice" +import { GameState } from "@/utils/features/game/gameSlice" +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 GameProps = { + game: GameState + auth: AuthState + onPostSuccess?: onPostSuccessFunc + onError?: onErrorFunc +} + +export const useGameStartMutation = ({ game, auth, onPostSuccess, onError }: GameProps) => { + return useMutation({ + mutationFn: async () => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/start/`, { + method: "POST", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }).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 }) + } + }) +} + +export const useGameStopMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => { + return useMutation({ + mutationFn: async () => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/stop/`, { + method: "POST", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }).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 }) + } + }) +} + +export const useGameSwitchPlayerMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => { + return useMutation({ + mutationFn: async () => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/switch-running-player/`, { + method: "POST", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }).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 }) + } + }) +} + +export const useGameRepairMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => { + return useMutation({ + mutationFn: async () => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/repair/`, { + method: "PUT", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }).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 }) + } + }) +} + +export const useGameResetMutation = ({ auth, game, onPostSuccess, onError }: GameProps) => { + return useMutation({ + mutationFn: async () => { + return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/game/reset/`, { + method: "DELETE", + headers: { + "Authorization": `Bearer ${auth.token}`, + "Content-Type": "application/json", + }, + }).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 }) + } + }) +} diff --git a/client/utils/geolocation.ts b/client/utils/geolocation.ts index bf95cd4..aa84ec8 100644 --- a/client/utils/geolocation.ts +++ b/client/utils/geolocation.ts @@ -35,6 +35,7 @@ export async function startGeolocationService(): Promise void)> { accuracy: Location.Accuracy.BestForNavigation, activityType: Location.ActivityType.OtherNavigation, deferredUpdatesInterval: 100, + timeInterval: 100, foregroundService: { killServiceOnDestroy: false, notificationBody: "Géolocalisation activée pour « Traintrape-moi »",