parent
48a48f9364
commit
2ce0e5b65d
@ -4,7 +4,7 @@ import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet'
|
|||||||
import LineDot from './LineDot'
|
import LineDot from './LineDot'
|
||||||
import { categoryNames } from '../assets/category'
|
import { categoryNames } from '../assets/category'
|
||||||
import { useBook } from '../hooks/api'
|
import { useBook } from '../hooks/api'
|
||||||
import { Announcement } from '../hooks/api/useAnnouncements'
|
import { Announcement } from '../api/announcement/types'
|
||||||
import { iconItem } from '../utils/markerIcons'
|
import { iconItem } from '../utils/markerIcons'
|
||||||
|
|
||||||
type AnnouncementDetailsProps = {
|
type AnnouncementDetailsProps = {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { Marker, Popup } from 'react-leaflet'
|
import { Marker, Popup } from 'react-leaflet'
|
||||||
|
|
||||||
import { Trashbox } from '../hooks/api/useTrashboxes'
|
import { Trashbox } from '../api/trashbox/types'
|
||||||
import { iconTrash } from '../utils/markerIcons'
|
import { iconTrash } from '../utils/markerIcons'
|
||||||
|
|
||||||
type TrashboxMarkersProps = {
|
type TrashboxMarkersProps = {
|
||||||
|
@ -4,15 +4,15 @@ import { composeAnnouncementsURL, initialAnnouncements, processAnnouncements } f
|
|||||||
|
|
||||||
import { isAnnouncementsResponse } from '../../api/announcements/types'
|
import { isAnnouncementsResponse } from '../../api/announcements/types'
|
||||||
|
|
||||||
const useAnnouncements = (filters: FiltersType) => {
|
const useAnnouncements = (filters: FiltersType) =>
|
||||||
return useFetch(
|
useFetch(
|
||||||
composeAnnouncementsURL(filters),
|
composeAnnouncementsURL(filters),
|
||||||
'GET',
|
'GET',
|
||||||
false,
|
false,
|
||||||
processAnnouncements,
|
|
||||||
isAnnouncementsResponse,
|
isAnnouncementsResponse,
|
||||||
|
processAnnouncements,
|
||||||
initialAnnouncements
|
initialAnnouncements
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
export default useAnnouncements
|
export default useAnnouncements
|
||||||
|
@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from 'react'
|
|||||||
import { handleHTTPErrors, isAborted } from '../../utils'
|
import { handleHTTPErrors, isAborted } from '../../utils'
|
||||||
import { getToken } from '../../utils/auth'
|
import { getToken } from '../../utils/auth'
|
||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
|
import { SetState } from '../../utils/types'
|
||||||
|
|
||||||
type UseFetchShared = {
|
type UseFetchShared = {
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
@ -19,9 +20,11 @@ type UseFetchErrored = {
|
|||||||
data: undefined
|
data: undefined
|
||||||
} & UseFetchShared
|
} & UseFetchShared
|
||||||
|
|
||||||
const gotError = <T>(res: UseFetchErrored | UseFetchSucced<T>): res is UseFetchErrored => {
|
const gotError = <T>(res: UseFetchErrored | UseFetchSucced<T>): res is UseFetchErrored =>
|
||||||
return typeof res.error === 'string'
|
typeof res.error === 'string'
|
||||||
}
|
|
||||||
|
const fallbackError = <T>(res: UseFetchSucced<T> | UseFetchErrored) =>
|
||||||
|
gotError(res) ? res.error : res.data
|
||||||
|
|
||||||
type UseFetchReturn<T> = ({
|
type UseFetchReturn<T> = ({
|
||||||
error: null,
|
error: null,
|
||||||
@ -31,6 +34,7 @@ type UseFetchReturn<T> = ({
|
|||||||
data: undefined
|
data: undefined
|
||||||
}) & {
|
}) & {
|
||||||
loading: boolean,
|
loading: boolean,
|
||||||
|
setData: SetState<T | undefined>
|
||||||
abort?: (() => void)
|
abort?: (() => void)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,8 +42,8 @@ const useFetch = <R, T>(
|
|||||||
url: string,
|
url: string,
|
||||||
method: RequestInit['method'],
|
method: RequestInit['method'],
|
||||||
needAuth: boolean,
|
needAuth: boolean,
|
||||||
processData: (data: R) => T,
|
|
||||||
guardResponse: (data: unknown) => data is R,
|
guardResponse: (data: unknown) => data is R,
|
||||||
|
processData: (data: R) => T,
|
||||||
initialData?: T,
|
initialData?: T,
|
||||||
params?: RequestInit
|
params?: RequestInit
|
||||||
): UseFetchReturn<T> => {
|
): UseFetchReturn<T> => {
|
||||||
@ -114,10 +118,11 @@ const useFetch = <R, T>(
|
|||||||
}) : ({ data: undefined, error })
|
}) : ({ data: undefined, error })
|
||||||
),
|
),
|
||||||
loading,
|
loading,
|
||||||
|
setData,
|
||||||
abort: abortControllerRef.current?.abort.bind(abortControllerRef.current)
|
abort: abortControllerRef.current?.abort.bind(abortControllerRef.current)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useFetch
|
export default useFetch
|
||||||
|
|
||||||
export { gotError }
|
export { gotError, fallbackError }
|
||||||
|
@ -4,15 +4,15 @@ import useFetch from './useFetch'
|
|||||||
import { composeTrashboxURL } from '../../api/trashbox'
|
import { composeTrashboxURL } from '../../api/trashbox'
|
||||||
import { isTrashboxResponse } from '../../api/trashbox/types'
|
import { isTrashboxResponse } from '../../api/trashbox/types'
|
||||||
|
|
||||||
const useTrashboxes = (position: LatLng) => {
|
const useTrashboxes = (position: LatLng) =>
|
||||||
return useFetch(
|
useFetch(
|
||||||
composeTrashboxURL(position),
|
composeTrashboxURL(position),
|
||||||
'GET',
|
'GET',
|
||||||
true,
|
true,
|
||||||
(data) => data,
|
|
||||||
isTrashboxResponse,
|
isTrashboxResponse,
|
||||||
|
(data) => data,
|
||||||
[]
|
[]
|
||||||
)
|
)
|
||||||
}
|
|
||||||
|
|
||||||
export default useTrashboxes
|
export default useTrashboxes
|
||||||
|
Loading…
x
Reference in New Issue
Block a user