From 4cef642c13e48571c72998b844e8cb15edd9da8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B8ran=20Dalum?= Date: Tue, 8 Oct 2024 18:21:58 +0200 Subject: [PATCH] refactor: Some refactorings --- .../map/components/DeparturesDialogSheet.tsx | 25 ++++++------------- src/place-screen/PlaceScreenComponent.tsx | 4 +-- .../hooks/use-stops-details-data-query.ts | 8 +++--- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/components/map/components/DeparturesDialogSheet.tsx b/src/components/map/components/DeparturesDialogSheet.tsx index 217f97269e..6bf1215cce 100644 --- a/src/components/map/components/DeparturesDialogSheet.tsx +++ b/src/components/map/components/DeparturesDialogSheet.tsx @@ -52,24 +52,13 @@ export const DeparturesDialogSheet = ({ const { data: stopDetailsData, - isFetching: isStopDetailsLoading, - isError: isStopDetailsError, - refetch: refetchStopDetailsData, - } = useStopsDetailsDataQuery(stopPlaceIds.length ? stopPlaceIds : undefined); - - const thereIsSomeQuays = stopDetailsData?.stopPlaces?.some( - (sp) => sp.quays?.length, - ); - - const refresh = () => { - if (isStopDetailsError) { - return refetchStopDetailsData(); - } - }; + status: stopDetailsStatus, + refetch: refetchStopDetails, + } = useStopsDetailsDataQuery(stopPlaceIds); const StopPlaceViewOrError = () => { - if (!isStopDetailsLoading && !isStopDetailsError) { - if (stopDetailsData && thereIsSomeQuays) { + if (stopDetailsStatus === 'success') { + if (stopDetailsData.stopPlaces.some((sp) => sp.quays?.length)) { return ( diff --git a/src/place-screen/PlaceScreenComponent.tsx b/src/place-screen/PlaceScreenComponent.tsx index 48d938503f..733eb608a3 100644 --- a/src/place-screen/PlaceScreenComponent.tsx +++ b/src/place-screen/PlaceScreenComponent.tsx @@ -57,9 +57,7 @@ export const PlaceScreenComponent = ({ ); const {data: stopsDetailsData, isError: isStopsDetailsError} = - useStopsDetailsDataQuery( - place.quays === undefined ? [place.id] : undefined, - ); + useStopsDetailsDataQuery(place.quays === undefined ? [place.id] : []); const isFocused = useIsFocused(); diff --git a/src/place-screen/hooks/use-stops-details-data-query.ts b/src/place-screen/hooks/use-stops-details-data-query.ts index e2874f277c..b68b979700 100644 --- a/src/place-screen/hooks/use-stops-details-data-query.ts +++ b/src/place-screen/hooks/use-stops-details-data-query.ts @@ -1,12 +1,12 @@ import {getStopsDetails} from '@atb/api/departures/stops-nearest'; import {useQuery} from '@tanstack/react-query'; -import {ONE_HOUR_MS} from "@atb/utils/durations.ts"; +import {ONE_HOUR_MS} from '@atb/utils/durations.ts'; -export const useStopsDetailsDataQuery = (ids?: Array) => +export const useStopsDetailsDataQuery = (ids: string[]) => useQuery({ - enabled: ids !== undefined, + enabled: ids.length > 0, queryKey: ['stopDetailsData', ids], - queryFn: () => ids && getStopsDetails({ids: ids}), + queryFn: () => getStopsDetails({ids}), staleTime: ONE_HOUR_MS, cacheTime: ONE_HOUR_MS, });