Skip to content

Commit

Permalink
refactor: Some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
gorandalum committed Oct 8, 2024
1 parent 75a8f7d commit 4cef642
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
25 changes: 7 additions & 18 deletions src/components/map/components/DeparturesDialogSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<StopPlacesView
stopPlaces={stopDetailsData?.stopPlaces}
Expand Down Expand Up @@ -102,14 +91,14 @@ export const DeparturesDialogSheet = ({
);
}

if (!isStopDetailsLoading && isStopDetailsError) {
if (stopDetailsStatus === 'error') {
return (
<View style={styles.paddingHorizontal}>
<MessageInfoBox
type="error"
message={t(DeparturesTexts.message.resultFailed)}
onPressConfig={{
action: refresh,
action: refetchStopDetails,
text: t(dictionary.retry),
}}
/>
Expand Down
4 changes: 1 addition & 3 deletions src/place-screen/PlaceScreenComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/place-screen/hooks/use-stops-details-data-query.ts
Original file line number Diff line number Diff line change
@@ -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<string>) =>
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,
});

0 comments on commit 4cef642

Please sign in to comment.