Improved useFetch

Related to #19
This commit is contained in:
2023-07-14 20:32:52 +03:00
parent 7ef4194bbd
commit 48a48f9364
15 changed files with 272 additions and 148 deletions

View File

@ -1,35 +1,18 @@
import { LatLng } from 'leaflet'
import { API_URL } from '../../config'
import { isArrayOf, isObject } from '../../utils/types'
import useFetch from './useFetch'
import { isString } from '../../utils/types'
type Trashbox = {
Lat: number,
Lng: number,
Address: string,
Categories: string[]
}
const isTrashbox = (obj: unknown): obj is Trashbox => isObject(obj, {
'Lat': 'number',
'Lng': 'number',
'Address': 'string',
'Categories': obj => isArrayOf<string>(obj, isString)
})
import { composeTrashboxURL } from '../../api/trashbox'
import { isTrashboxResponse } from '../../api/trashbox/types'
const useTrashboxes = (position: LatLng) => {
return useFetch(
API_URL + '/trashbox?' + new URLSearchParams({
lat: position.lat.toString(),
lng: position.lng.toString()
}).toString(),
undefined,
[],
(obj): obj is Trashbox[] => isArrayOf(obj, isTrashbox)
composeTrashboxURL(position),
'GET',
true,
(data) => data,
isTrashboxResponse,
[]
)
}
export type { Trashbox }
export default useTrashboxes