From 2ce0e5b65d84113d62e4bc2c9ec8641922709882 Mon Sep 17 00:00:00 2001 From: dm1sh Date: Sat, 15 Jul 2023 11:07:12 +0300 Subject: [PATCH] Returned setData from useFetch Fixed imports Related to #19 --- front/src/components/AnnouncementDetails.tsx | 2 +- front/src/components/TrashboxMarkers.tsx | 2 +- front/src/hooks/api/useAnnouncements.ts | 8 ++++---- front/src/hooks/api/useFetch.ts | 15 ++++++++++----- front/src/hooks/api/useTrashboxes.ts | 8 ++++---- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/front/src/components/AnnouncementDetails.tsx b/front/src/components/AnnouncementDetails.tsx index eef9bba..c4b8da6 100644 --- a/front/src/components/AnnouncementDetails.tsx +++ b/front/src/components/AnnouncementDetails.tsx @@ -4,7 +4,7 @@ import { MapContainer, Marker, Popup, TileLayer } from 'react-leaflet' import LineDot from './LineDot' import { categoryNames } from '../assets/category' import { useBook } from '../hooks/api' -import { Announcement } from '../hooks/api/useAnnouncements' +import { Announcement } from '../api/announcement/types' import { iconItem } from '../utils/markerIcons' type AnnouncementDetailsProps = { diff --git a/front/src/components/TrashboxMarkers.tsx b/front/src/components/TrashboxMarkers.tsx index 1c5a91f..2478566 100644 --- a/front/src/components/TrashboxMarkers.tsx +++ b/front/src/components/TrashboxMarkers.tsx @@ -1,6 +1,6 @@ import { Marker, Popup } from 'react-leaflet' -import { Trashbox } from '../hooks/api/useTrashboxes' +import { Trashbox } from '../api/trashbox/types' import { iconTrash } from '../utils/markerIcons' type TrashboxMarkersProps = { diff --git a/front/src/hooks/api/useAnnouncements.ts b/front/src/hooks/api/useAnnouncements.ts index beed64e..1c73b6d 100644 --- a/front/src/hooks/api/useAnnouncements.ts +++ b/front/src/hooks/api/useAnnouncements.ts @@ -4,15 +4,15 @@ import { composeAnnouncementsURL, initialAnnouncements, processAnnouncements } f import { isAnnouncementsResponse } from '../../api/announcements/types' -const useAnnouncements = (filters: FiltersType) => { - return useFetch( +const useAnnouncements = (filters: FiltersType) => + useFetch( composeAnnouncementsURL(filters), 'GET', false, - processAnnouncements, isAnnouncementsResponse, + processAnnouncements, initialAnnouncements ) -} + export default useAnnouncements diff --git a/front/src/hooks/api/useFetch.ts b/front/src/hooks/api/useFetch.ts index 4759a93..cfb65f2 100644 --- a/front/src/hooks/api/useFetch.ts +++ b/front/src/hooks/api/useFetch.ts @@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from 'react' import { handleHTTPErrors, isAborted } from '../../utils' import { getToken } from '../../utils/auth' import { useNavigate } from 'react-router-dom' +import { SetState } from '../../utils/types' type UseFetchShared = { loading: boolean, @@ -19,9 +20,11 @@ type UseFetchErrored = { data: undefined } & UseFetchShared -const gotError = (res: UseFetchErrored | UseFetchSucced): res is UseFetchErrored => { - return typeof res.error === 'string' -} +const gotError = (res: UseFetchErrored | UseFetchSucced): res is UseFetchErrored => + typeof res.error === 'string' + +const fallbackError = (res: UseFetchSucced | UseFetchErrored) => + gotError(res) ? res.error : res.data type UseFetchReturn = ({ error: null, @@ -31,6 +34,7 @@ type UseFetchReturn = ({ data: undefined }) & { loading: boolean, + setData: SetState abort?: (() => void) } @@ -38,8 +42,8 @@ const useFetch = ( url: string, method: RequestInit['method'], needAuth: boolean, - processData: (data: R) => T, guardResponse: (data: unknown) => data is R, + processData: (data: R) => T, initialData?: T, params?: RequestInit ): UseFetchReturn => { @@ -114,10 +118,11 @@ const useFetch = ( }) : ({ data: undefined, error }) ), loading, + setData, abort: abortControllerRef.current?.abort.bind(abortControllerRef.current) } } export default useFetch -export { gotError } +export { gotError, fallbackError } diff --git a/front/src/hooks/api/useTrashboxes.ts b/front/src/hooks/api/useTrashboxes.ts index f9a02b6..9c23d5d 100644 --- a/front/src/hooks/api/useTrashboxes.ts +++ b/front/src/hooks/api/useTrashboxes.ts @@ -4,15 +4,15 @@ import useFetch from './useFetch' import { composeTrashboxURL } from '../../api/trashbox' import { isTrashboxResponse } from '../../api/trashbox/types' -const useTrashboxes = (position: LatLng) => { - return useFetch( +const useTrashboxes = (position: LatLng) => + useFetch( composeTrashboxURL(position), 'GET', true, - (data) => data, isTrashboxResponse, + (data) => data, [] ) -} + export default useTrashboxes