Prototype envoi géolocalisations
This commit is contained in:
56
client/hooks/mutations/useGeolocationMutation.ts
Normal file
56
client/hooks/mutations/useGeolocationMutation.ts
Normal 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 })
|
||||
}
|
||||
})
|
||||
}
|
@ -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))
|
||||
}
|
||||
|
Reference in New Issue
Block a user