Skip to content

Commit

Permalink
Simplify scroll, and setLoading when there's stale data
Browse files Browse the repository at this point in the history
  • Loading branch information
amy-corson-ibigroup committed Dec 18, 2024
1 parent 5d93587 commit f6d2d86
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/components/viewers/nearby/nearby-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { connect } from 'react-redux'
import { FormattedMessage, useIntl } from 'react-intl'
import { Location } from '@opentripplanner/types'
import { MapRef, useMap } from 'react-map-gl'
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import React, { useCallback, useEffect, useMemo, useState } from 'react'

import * as apiActions from '../../../actions/api'
import * as mapActions from '../../../actions/map'
Expand Down Expand Up @@ -128,7 +128,6 @@ function NearbyView({
const map = useMap().default
const intl = useIntl()
const [loading, setLoading] = useState(true)
const firstItemRef = useRef<HTMLDivElement>(null)
const finalNearbyCoords = useMemo(
() =>
getNearbyCoordsFromUrlOrLocationOrMapCenter(
Expand Down Expand Up @@ -180,9 +179,11 @@ function NearbyView({
}, [map, setViewedNearbyCoords, setHighlightedLocation])

useEffect(() => {
if (typeof firstItemRef.current?.scrollIntoView === 'function') {
firstItemRef.current?.scrollIntoView({ behavior: 'smooth' })
}
window.scrollTo({
behavior: 'smooth',
left: 0,
top: 0
})
if (finalNearbyCoords) {
fetchNearby(finalNearbyCoords, radius, currentServiceWeek)
setLoading(true)
Expand Down Expand Up @@ -250,6 +251,11 @@ function NearbyView({
useEffect(() => {
if (!staleData) {
setLoading(false)
} else if (staleData) {
// If there's stale data, fetch again
setLoading(true)
finalNearbyCoords &&
fetchNearby(finalNearbyCoords, radius, currentServiceWeek)
}
}, [nearby, staleData])

Expand Down Expand Up @@ -285,8 +291,6 @@ function NearbyView({
className="base-color-bg"
style={{ marginBottom: 0 }}
>
{/* This is used to scroll to top */}
<div aria-hidden ref={firstItemRef} />
{loading && (
<FloatingLoadingIndicator>
<Loading extraSmall />
Expand Down

0 comments on commit f6d2d86

Please sign in to comment.