Prototype envoi géolocalisations

This commit is contained in:
2024-12-11 01:30:21 +01:00
parent db7a0b970d
commit bdd53eb8bb
10 changed files with 140 additions and 32 deletions

View File

@ -0,0 +1,56 @@
import { AuthState } from "@/utils/features/location/authSlice"
import { useMutation } from "@tanstack/react-query"
import { LocationObject } from "expo-location"
type ErrorResponse = {
error: string
message: string
statusCode: number
}
type LoginForm = {
name: string
password: string
}
type onPostSuccessFunc = (data: any, variables: LocationObject, context: unknown) => void
type ErrorFuncProps = { response?: ErrorResponse, error?: Error }
type onErrorFunc = (props: ErrorFuncProps) => void
type PostProps = {
auth: AuthState
onPostSuccess?: onPostSuccessFunc
onError?: onErrorFunc
}
export const useGeolocationMutation = ({ auth, onPostSuccess, onError }: PostProps) => {
return useMutation({
mutationFn: async (location: LocationObject) => {
return fetch(`${process.env.EXPO_PUBLIC_TRAINTRAPE_MOI_SERVER}/geolocations/`, {
method: "POST",
headers: {
"Authorization": `Bearer ${auth.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
longitude: location.coords.longitude,
latitude: location.coords.latitude,
speed: location.coords.speed,
accuracy: location.coords.accuracy,
altitude: location.coords.altitude,
altitudeAccuracy: location.coords.altitudeAccuracy,
timestamp: location.timestamp,
})
}).then(resp => resp.json())
},
networkMode: 'offlineFirst',
onSuccess: async (data, location: LocationObject, context: unknown) => {
if (onPostSuccess)
onPostSuccess(data, location, context)
},
onError: async (error: Error) => {
if (onError)
onError({ error: error })
}
})
}

View File

@ -1,9 +1,15 @@
import { LocationObject } from "expo-location"
import { useAppDispatch, useAppSelector } from "./useStore"
import { setLocation } from "@/utils/features/location/locationSlice"
import { setLastLocation, unqueueLocation } from "@/utils/features/location/locationSlice"
export const useLocation = () => useAppSelector((state) => state.location.location)
export const useSetLocation = () => {
export const useLastOwnLocation = () => useAppSelector((state) => state.location.lastOwnLocation)
export const useQueuedLocations = () => useAppSelector((state) => state.location.queuedLocations)
export const useSetLastLocation = () => {
const dispatch = useAppDispatch()
return (location: LocationObject) => dispatch(setLocation(location))
return (location: LocationObject) => dispatch(setLastLocation(location))
}
export const useUnqueueLocation = () => {
const dispatch = useAppDispatch()
return (location: LocationObject) => dispatch(unqueueLocation(location))
}