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

Handle pasted data types #989

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
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
40 changes: 24 additions & 16 deletions lib/editor/components/timetable/TimetableGrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,27 +378,35 @@ export default class TimetableGrid extends Component<Props> {
} = this.props
let activeRow = rowIndex
let activeCol = colIndex
// iterate over rows in pasted selection
// Iterate over rows in pasted selection
for (var i = 0; i < pastedRows.length; i++) {
activeRow = rowIndex + i
// construct new row if it doesn't exist
if (typeof data[i + rowIndex] === 'undefined') {
// Construct new row if it doesn't exist
if (typeof data[activeRow] === 'undefined') {
addNewRow()
}
// iterate over number of columns in pasted selection
for (var j = 0; j < pastedRows[0].length; j++) {
// Iterate over number of columns in pasted selection
for (var j = 0; j < pastedRows[i].length; j++) {
AdrianaCeric marked this conversation as resolved.
Show resolved Hide resolved
activeCol = colIndex + j
const path = `${rowIndex + i}.${columns[colIndex + j].key}`
const value = parseCellValue(pastedRows[i][j], columns[colIndex + j])
updateCellValue({value, rowIndex: rowIndex + i, key: path})
// if departure times are hidden, paste into adjacent time column
const adjacentPath = `${rowIndex + i}.${columns[colIndex + j + 2].key}`
if (
hideDepartureTimes &&
isTimeFormat(columns[colIndex + j].type) &&
typeof objectPath.get(data, adjacentPath) !== 'undefined'
) {
updateCellValue({value, rowIndex: rowIndex + i, key: adjacentPath})
const col = columns[activeCol]
const path = `${activeRow}.${col.key}`
const originalValue = objectPath.get(data[activeRow], col.key)
const value = parseCellValue(pastedRows[i][j], col)
if (value !== null) {
const finalValue = value
updateCellValue({ value: finalValue, rowIndex: activeRow, key: path })
// If departure times are hidden, paste into adjacent time column
const adjacentPath = `${activeRow}.${columns[activeCol + 2].key}`
if (
hideDepartureTimes &&
isTimeFormat(columns[activeCol].type) &&
typeof objectPath.get(data, adjacentPath) !== 'undefined'
) {
updateCellValue({ value: finalValue, rowIndex: activeRow, key: adjacentPath })
}
} else {
// If the value is rejected, keep the original value
updateCellValue({ value: originalValue, rowIndex: activeRow, key: path })
AdrianaCeric marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/editor/components/timetable/TimetableHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export default class TimetableHeader extends Component<Props> {
bsStyle: 'primary',
'data-test-id': 'save-trip-button',
children: <Icon type='floppy-o' />,
disabled: edited.length === 0 || errorCount,
disabled: edited.length === 0 || errorCount > 0,
AdrianaCeric marked this conversation as resolved.
Show resolved Hide resolved
onClick: this._onClickSave
}
}]
Expand Down
15 changes: 11 additions & 4 deletions lib/editor/util/timetable.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,20 @@ export function getHeaderColumns (
* Handles pasted data from clipboard (e.g. from CSV file)
* If departure/arrival time cell, pastes in time format, otherwise returns string as is
*/
let alertShown = false
AdrianaCeric marked this conversation as resolved.
Show resolved Hide resolved
export function parseCellValue (timeString: string, col: TimetableColumn) {
if (isTimeFormat(col.type)) {
const date = moment().startOf('day').format('YYYY-MM-DD')
return moment(date + 'T' + timeString, TIMETABLE_FORMATS).diff(
date,
'seconds'
)
const parsedDate = moment(date + 'T' + timeString, TIMETABLE_FORMATS, true)
if (!parsedDate.isValid()) {
if (!alertShown) {
alert('Please enter a valid time format')
AdrianaCeric marked this conversation as resolved.
Show resolved Hide resolved
alertShown = true
}
return null
} else {
return moment(date + 'T' + timeString, TIMETABLE_FORMATS).diff(date, 'seconds')
}
} else {
return timeString
}
Expand Down
Loading