forked from polka_billy/porridger
Added type definitions for components, functions, data Added guards for network responses fixes #8
35 lines
893 B
TypeScript
35 lines
893 B
TypeScript
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)
|
|
})
|
|
|
|
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)
|
|
)
|
|
}
|
|
|
|
export type { Trashbox }
|
|
export default useTrashboxes |