Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fikse uendelig loop i useeffekten til holdeplass-view #454

Merged
merged 2 commits into from
Sep 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions src/dashboards/BusStop/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Loader } from '@entur/loader'

import { FormFactor } from '@entur/sdk/lib/mobility/types'

import { usePrevious, isEqualUnsorted } from '../../utils'
import { isEqualUnsorted } from '../../utils'
import DashboardWrapper from '../../containers/DashboardWrapper'

import { DEFAULT_ZOOM } from '../../constants'
Expand All @@ -26,6 +26,7 @@ import { useSettingsContext } from '../../settings'

import RearrangeModal, { Item } from '../../components/RearrangeModal'
import { LongPressProvider } from '../../logic/longPressContext'
import { StopPlaceWithDepartures } from '../../types'

import DepartureTile from './DepartureTile'

Expand Down Expand Up @@ -86,42 +87,42 @@ const BusStop = ({ history }: Props): JSX.Element | null => {
const [isLongPressStarted, setIsLongPressStarted] = useState<boolean>(false)
const isCancelled = useRef<NodeJS.Timeout>()
const [modalVisible, setModalVisible] = useState(false)

function clearLongPressTimeout() {
setIsLongPressStarted(false)
if (isCancelled.current) {
clearTimeout(isCancelled.current)
}
}
const [numberOfStopPlaces, setNumberOfStopPlaces] = useState<number>(0)
const stopPlacesWithDepartures = useStopPlacesWithDepartures()

const dashboardKey = history.location.key

const boardId =
useRouteMatch<{ documentId: string }>('/t/:documentId')?.params
?.documentId

const [gridLayouts, setGridLayouts] = useState<Layouts | undefined>(
getFromLocalStorage(dashboardKey),
)

const [tileOrder, setTileOrder] = useState<Item[] | undefined>(
boardId ? getFromLocalStorage(boardId + '-tile-order') : undefined,
)

let stopPlacesWithDepartures = useStopPlacesWithDepartures()
const numberOfStopPlaces = stopPlacesWithDepartures
? stopPlacesWithDepartures.length
: 0
const prevNumberOfStopPlaces = usePrevious(numberOfStopPlaces)
const scooters = useMobility(FormFactor.SCOOTER)
const bikeRentalStations = useBikeRentalStations()

if (stopPlacesWithDepartures) {
stopPlacesWithDepartures = stopPlacesWithDepartures.filter(
const walkInfo = useWalkInfo(stopPlacesWithDepartures)

function clearLongPressTimeout() {
setIsLongPressStarted(false)
if (isCancelled.current) {
clearTimeout(isCancelled.current)
}
}

function filterStopPlacesWithDepartures(
Collatty marked this conversation as resolved.
Show resolved Hide resolved
sPlacesWithDepartures: StopPlaceWithDepartures[],
): StopPlaceWithDepartures[] {
return sPlacesWithDepartures.filter(
({ departures }) => departures.length > 0,
)
}

const walkInfo = useWalkInfo(stopPlacesWithDepartures)
const mapCol = settings?.showMap ? 1 : 0
const totalItems = numberOfStopPlaces + mapCol
const hasData = Boolean(
Expand Down Expand Up @@ -149,13 +150,14 @@ const BusStop = ({ history }: Props): JSX.Element | null => {
useEffect(() => {
let defaultTileOrder: Item[] = []
if (stopPlacesWithDepartures) {
if (stopPlacesWithDepartures.length == prevNumberOfStopPlaces) {
return
}
defaultTileOrder = stopPlacesWithDepartures.map((item) => ({
const filtered = filterStopPlacesWithDepartures(
stopPlacesWithDepartures,
).map((item) => ({
id: item.id,
name: item.name,
}))
setNumberOfStopPlaces(filtered.length)
defaultTileOrder = filtered
}
if (mapCol) {
defaultTileOrder = [
Expand All @@ -178,13 +180,7 @@ const BusStop = ({ history }: Props): JSX.Element | null => {
} else {
setTileOrder(defaultTileOrder)
}
}, [
stopPlacesWithDepartures,
prevNumberOfStopPlaces,
mapCol,
settings?.showMap,
boardId,
])
}, [stopPlacesWithDepartures, mapCol, boardId])

const longPress = useLongPress(
() => {
Expand Down