Skip to content

Commit

Permalink
fix: make state explicit so callback doesn't call all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianaCeric committed Dec 7, 2023
1 parent 19dc66e commit 3eda422
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions lib/editor/components/timetable/TripSeriesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,15 @@ const TripSeriesModal = (props: Props) => {

const onClickGenerate = useCallback(() => {
const {addNewTrip, constructNewRow, onClose} = props
const {
startTime,
endTime,
headway,
autoTripIdStart,
autoTripIdIncrement,
autoTripIdPrefix,
autoBlockIdPrefix,
autoBlockIdIncrement,
autoBlockIdStart,
useAutoTripIds,
useAutoBlockIds
} = state
// Check state variables to make flow happy
if (startTime === null || endTime === null || headway === null) return
const adjustedEndTime = startTime < endTime ? endTime : endTime + 24 * 60 * 60

let tripId = autoTripIdStart
let currentBlockIdSuffix = autoBlockIdStart
for (let time = startTime; time <= adjustedEndTime; time += headway) {
const autoTripId = useAutoTripIds ? `${autoTripIdPrefix}-${tripId}` : null
const autoBlockId = useAutoBlockIds ? `${autoBlockIdPrefix}-${currentBlockIdSuffix}` : null
if (state.startTime === null || state.endTime === null || state.headway === null) return
const adjustedEndTime = state.startTime < state.endTime ? state.endTime : state.endTime + 24 * 60 * 60

let tripId = state.autoTripIdStart
let currentBlockIdSuffix = state.autoBlockIdStart
for (let time = state.startTime; time <= adjustedEndTime; time += state.headway) {
const autoTripId = state.useAutoTripIds ? `${state.autoTripIdPrefix}-${tripId}` : null
const autoBlockId = state.useAutoBlockIds ? `${state.autoBlockIdPrefix}-${currentBlockIdSuffix}` : null
addNewTrip(constructNewRow(null, time, autoTripId, autoBlockId))
// If we're updating the trip IDs automatically, increment the trip ID:
if (useAutoTripIds) tripId += autoTripIdIncrement
Expand All @@ -110,7 +97,7 @@ const TripSeriesModal = (props: Props) => {
}
setState(initialState)
onClose()
}, [state])
}, [state.startTime, state.endTime, state.headway, state.autoTripIdStart, state.autoTripIdIncrement, state.autoTripIdPrefix, state.autoBlockIdPrefix, state.autoBlockIdIncrement, state.autoBlockIdStart, state.useAutoTripIds, state.useAutoBlockIds])

const {Body, Footer, Header, Title} = Modal
const {onClose, show, useSecondsInOffset} = props
Expand Down

0 comments on commit 3eda422

Please sign in to comment.