Skip to content

Commit

Permalink
fix(EditableCell): reuse logic, make alert show only once
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianaCeric committed Oct 24, 2023
1 parent 689a399 commit c52287d
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/editor/components/timetable/EditableCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

// $FlowFixMe
import React, {useEffect, useState} from 'react'
import moment from 'moment'

import * as tripActions from '../../actions/trip'
import {secondsAfterMidnightToHHMM} from '../../../common/util/gtfs'
import { isTimeFormat } from '../../util/timetable'
import { isTimeFormat, parseCellValue } from '../../util/timetable'
import type {TimetableColumn} from '../../../types'
import type {EditorValidationIssue} from '../../util/validation'

Expand Down Expand Up @@ -206,6 +205,7 @@ function EditableCell (props: Props) {
onStopEditing()
}

let alertShown = false
const save = () => {
const {column} = props
const {data} = state
Expand All @@ -226,25 +226,28 @@ function EditableCell (props: Props) {
cancel()
}
} else if (isTimeFormat(column.type)) {
if (!isTimeFormat(data)) {
window.alert('Please enter a valid time format.')
cancel()
} else {
const duration = moment.duration(data)
// $FlowFixMe - Flow type error
const value = duration.isValid() ? duration.valueOf() / 1000 : false
const parsedValue = parseCellValue(data, column)

if (value !== false) {
_handleSave(value)
} else {
cancel()
if (parsedValue !== null) {
_handleSave(parsedValue)
} else {
if (!alertShown) {
alert('Please enter a valid time format')
alertShown = true
}
cancel()
}
}
}

const handleBlur = () => {
save()
setState((prevState) => ({
...prevState,
isEditing: false,
isFocused: false,
data: prevState.data
}))
props.onStopEditing()
}

const handleChange = (evt) => {
Expand Down

0 comments on commit c52287d

Please sign in to comment.