traintrape-moi/client/components/GeolocationProvider.tsx

33 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-12-11 00:30:21 +00:00
import { ReactNode, useEffect } from 'react'
import { useAuth } from '@/hooks/useAuth'
import { useQueuedLocations, useUnqueueLocation } from '@/hooks/useLocation'
import { useGeolocationMutation } from '@/hooks/mutations/useGeolocationMutation'
import { useStartGeolocationServiceEffect } from '@/utils/geolocation'
import { Platform } from 'react-native'
2024-12-11 00:30:21 +00:00
export default function GeolocationProvider({ children }: { children: ReactNode }) {
useStartGeolocationServiceEffect()
const auth = useAuth()
const geolocationsQueue = useQueuedLocations()
const unqueueLocation = useUnqueueLocation()
const geolocationMutation = useGeolocationMutation({
auth,
2024-12-12 23:32:52 +00:00
onPostSuccess: (data) => {
unqueueLocation(data)
2024-12-11 00:30:21 +00:00
geolocationMutation.reset()
},
onError: ({ response, error }) => { console.error(response, error) }
})
useEffect(() => {
if (geolocationsQueue.length === 0 || geolocationMutation.isPending || Platform.OS === "web")
2024-12-11 00:30:21 +00:00
return
const locToSend = geolocationsQueue[0]
geolocationMutation.mutate(locToSend)
}, [auth, geolocationsQueue])
return <>
{children}
</>
}