2020-01-22 06:19:13 -09:00
|
|
|
import React from 'react'
|
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
|
|
|
|
|
|
const DurationField = ({ record = {}, source }) => {
|
2020-04-27 18:46:40 -08:00
|
|
|
try {
|
|
|
|
|
return <span>{format(record[source])}</span>
|
|
|
|
|
} catch (e) {
|
2020-05-11 18:04:35 -08:00
|
|
|
console.log('Error in DurationField! Record:', record)
|
|
|
|
|
return <span>00:00</span>
|
2020-04-27 18:46:40 -08:00
|
|
|
}
|
2020-01-22 06:19:13 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const format = (d) => {
|
2020-11-05 10:02:09 -09:00
|
|
|
const hours = Math.floor(d / 3600)
|
|
|
|
|
const minutes = Math.floor(d / 60) % 60
|
|
|
|
|
const seconds = d % 60
|
|
|
|
|
return [hours, minutes, seconds]
|
2020-11-05 14:27:46 -09:00
|
|
|
.map((v) => Math.round(v).toString())
|
|
|
|
|
.map((v) => (v.length !== 2 ? '0' + v : v))
|
2020-11-05 10:02:09 -09:00
|
|
|
.filter((v, i) => v !== '00' || i > 0)
|
|
|
|
|
.join(':')
|
2020-01-22 06:19:13 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DurationField.propTypes = {
|
|
|
|
|
label: PropTypes.string,
|
|
|
|
|
record: PropTypes.object,
|
|
|
|
|
source: PropTypes.string.isRequired,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DurationField.defaultProps = {
|
|
|
|
|
addLabel: true,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default DurationField
|