|
| 1 | +// @flow |
| 2 | +import Icon from '@conveyal/woonerf/components/icon' |
| 3 | +import humanizeDuration from 'humanize-duration' |
| 4 | +import moment from 'moment' |
| 5 | +import React, { PureComponent } from 'react' |
| 6 | + |
| 7 | +import { getComponentMessages } from '../../common/util/config' |
| 8 | + |
| 9 | +type Props = { |
| 10 | + latestSentToExternalPublisher?: number, |
| 11 | + publishState: string; |
| 12 | +}; |
| 13 | + |
| 14 | +type PublishStatusState = { |
| 15 | + elapsed: string |
| 16 | +} |
| 17 | + |
| 18 | +export class PublishStatus extends PureComponent<Props, PublishStatusState> { |
| 19 | + messages = getComponentMessages('PublishStatus'); |
| 20 | + _interval: ?IntervalID; |
| 21 | + |
| 22 | + constructor (props: Props) { |
| 23 | + super(props) |
| 24 | + this.state = { elapsed: '' } |
| 25 | + this._interval = null |
| 26 | + } |
| 27 | + |
| 28 | + componentDidMount () { |
| 29 | + if (this.props.publishState === 'PUBLISHING') { |
| 30 | + this.updateElapsed() |
| 31 | + this._interval = setInterval(this.updateElapsed, 10000) |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + componentDidUpdate (prevProps: Props) { |
| 36 | + const wasProcessing = prevProps.publishState === 'PUBLISHING' |
| 37 | + const isProcessingNow = this.props.publishState === 'PUBLISHING' |
| 38 | + if (!wasProcessing && isProcessingNow) { |
| 39 | + this.updateElapsed() |
| 40 | + this._interval = setInterval(this.updateElapsed, 10000) |
| 41 | + } else if (wasProcessing && !isProcessingNow && this._interval) { |
| 42 | + clearInterval(this._interval) |
| 43 | + this._interval = null |
| 44 | + this.setState({ elapsed: '' }) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + componentWillUnmount () { |
| 49 | + if (this._interval) clearInterval(this._interval) |
| 50 | + } |
| 51 | + |
| 52 | + updateElapsed = () => { |
| 53 | + const ts = this.props.latestSentToExternalPublisher |
| 54 | + if (!ts) { |
| 55 | + this.setState({ elapsed: '' }) |
| 56 | + return |
| 57 | + } |
| 58 | + // Normalize to milliseconds |
| 59 | + let ms = Number(ts) |
| 60 | + if (isNaN(ms)) { |
| 61 | + ms = +moment(ts) |
| 62 | + } |
| 63 | + if (!ms || isNaN(ms)) { |
| 64 | + this.setState({ elapsed: '' }) |
| 65 | + return |
| 66 | + } |
| 67 | + const human = humanizeDuration(Date.now() - ms, { largest: 2, round: true }) |
| 68 | + this.setState({ elapsed: human }) |
| 69 | + }; |
| 70 | + |
| 71 | + render () { |
| 72 | + const { publishState } = this.props |
| 73 | + const { elapsed } = this.state |
| 74 | + |
| 75 | + if (publishState === 'PUBLISHED') { |
| 76 | + return ( |
| 77 | + <span className='feed-status status-active'> |
| 78 | + <Icon type='check-circle' /> |
| 79 | + {this.messages('published')} |
| 80 | + </span> |
| 81 | + ) |
| 82 | + } else if (publishState === 'PUBLISHING') { |
| 83 | + return ( |
| 84 | + <> |
| 85 | + <span className='feed-status status-publishing'> |
| 86 | + <Icon type='spinner' /> |
| 87 | + {this.messages('publishingInProgress')} |
| 88 | + </span> |
| 89 | + <p> |
| 90 | + {elapsed ? `Processing for ${elapsed}` : null} |
| 91 | + </p> |
| 92 | + </> |
| 93 | + ) |
| 94 | + } else if (!publishState) { |
| 95 | + return ( |
| 96 | + <span className='feed-status status-no-version'> |
| 97 | + <Icon type='minus-circle' /> |
| 98 | + {this.messages('no-version')} |
| 99 | + </span> |
| 100 | + ) |
| 101 | + } else if (publishState === 'PUBLISH_BLOCKED') { |
| 102 | + return ( |
| 103 | + <span className='feed-status status-publish-disabled'> |
| 104 | + <Icon type='ban' /> |
| 105 | + {this.messages('publishDisabled')} |
| 106 | + </span> |
| 107 | + ) |
| 108 | + } else if (publishState === 'READY_TO_PUBLISH') { |
| 109 | + return ( |
| 110 | + <span className='feed-status status-unpublished'> |
| 111 | + <Icon type='circle' /> |
| 112 | + {this.messages('canPublish')} |
| 113 | + </span> |
| 114 | + ) |
| 115 | + } |
| 116 | + return null |
| 117 | + } |
| 118 | +} |
0 commit comments