Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 48 additions & 54 deletions lib/manager/components/version/FeedVersionActionsMTC.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Link } from 'react-router'
import { connect } from 'react-redux'

import * as versionsActions from '../../actions/versions'
import { BLOCKING_ERROR_TYPES } from '../../util/version'
import type { Feed, FeedVersion, FeedVersionSummary, GtfsPlusValidation, ServerJob } from '../../../types'
import type { ManagerUserState } from '../../../types/reducers'

Expand Down Expand Up @@ -62,18 +61,6 @@ function userCanManageFeed (user: ManagerUserState, version: FeedVersion): boole
) !== null
}

/**
* Check that the validation did not encounter any fatal exception or blocking
* errors.
*/
function checkBlockingIssue (version: FeedVersion): boolean {
if (!version.validationResult) return false
if (version.validationResult.fatalException) return true
const errorCounts = version.validationResult.error_counts
return !!errorCounts &&
!!(errorCounts.find(ec => BLOCKING_ERROR_TYPES.indexOf(ec.type) !== -1))
}

class FeedVersionActionsMTC extends Component<Props> {
_handleMergeVersion: ((versionId: string) => void) = (versionId: string) => {
// Note: service period feed merge has only been extensively tested with
Expand All @@ -83,67 +70,74 @@ class FeedVersionActionsMTC extends Component<Props> {

_onClickPublish: (() => any) = () => this.props.publishFeedVersion(this.props.version)

// eslint-disable-next-line complexity
render (): React$Element<"div"> {
_getWarning = () => {
const {
feedSource,
gtfsPlusValidation,
jobs,
user,
version
} = this.props
const {validationSummary: summary} = version
// We must check the version ID against the feed source in props (not the
// feed source nested underneath version) because this is the only place the
// published version is updated.
const isPublished = version.namespace === feedSource.publishedVersionId
// Version is in the "processing" state if it has been sent to external
// source, but has not been processed yet.
const processing = version.sentToExternalPublisher &&
!version.processedByExternalPublisher
const hasBlockingIssue = checkBlockingIssue(version)
const { autoPublish, feedVersionSummaries: summaries, id } = feedSource

// Use version summaries from FeedSource to get the publish status returned by backend.
if (!summaries) return ''
const versionSummary = summaries.find(v => v.id === version.id)

const { publishState = '', validationSummary: summary } = versionSummary || {}

const hasGtfsPlusBlockingIssue = gtfsPlusValidation && gtfsPlusValidation.issues.length > 0
const isMergedServicePeriods = version.retrievalMethod === 'SERVICE_PERIOD_MERGE'

// Expiry is computed same as with VersionDateLabel (TODO: refactor and reconcile with versionHasExpired).
// Expiry is computed same as in backend using FeedValidationSummary.startDate/endDate.
const now = +moment().startOf('day')
const end = +moment(summary.endDate)
const start = +moment(summary.startDate)
const expired = end < now

const jobsProcessingThisVersion = jobs && jobs.some(job => job.feedVersionId === version.id)

const publishButtonDisabled = !gtfsPlusValidation ||
hasGtfsPlusBlockingIssue ||
!gtfsPlusValidation.published ||
expired ||
!version.validationResult ||
!version.validationResult.error_counts ||
hasBlockingIssue ||
isPublished ||
processing ||
!userCanManageFeed(user, version) ||
jobsProcessingThisVersion

let publishWarningMessage
if (hasBlockingIssue || hasGtfsPlusBlockingIssue) {
publishWarningMessage = (
const future = start > now

if (expired) {
return 'Cannot publish version because it has expired.'
} else if (future) {
return 'Cannot publish version because it is in the future.'
} else if (publishState === 'PUBLISH_BLOCKED') {
return (
<span>
Cannot publish version because it has a{' '}
{hasGtfsPlusBlockingIssue ? 'GTFS+ ' : ''}
blocking issue.
(See{' '}
<Link
to={`/feed/${feedSource.id}/version/${version.version}/${hasGtfsPlusBlockingIssue ? 'gtfsplus' : 'issues'}`}
to={`/feed/${id}/version/${version.version}/${hasGtfsPlusBlockingIssue ? 'gtfsplus' : 'issues'}`}
>
{hasGtfsPlusBlockingIssue ? 'GTFS+' : 'validation'} issues
</Link>.)
</span>
)
} else if (expired) {
publishWarningMessage = 'Cannot publish version because it has expired.'
} else if (feedSource.autoPublish) {
publishWarningMessage = 'Reminder: this feed is already set to be auto-published after auto-fetch!'
} else if (autoPublish) {
return 'Reminder: this feed is already set to be auto-published after auto-fetch!'
}
}

render (): React$Element<"div"> {
const {
feedSource,
jobs,
user,
version
} = this.props
const { feedVersionSummaries: summaries } = feedSource

// Use version summaries from FeedSource to get the publish status returned by backend.
const versionSummary = summaries && summaries.find(v => v.id === version.id)

const { publishState = '' } = versionSummary || {}
const isPublished = publishState === 'PUBLISHED'
const processing = publishState === 'PUBLISHING'
const isMergedServicePeriods = version.retrievalMethod === 'SERVICE_PERIOD_MERGE'

const jobsProcessingThisVersion = jobs && jobs.some(job => job.feedVersionId === version.id)

const publishButtonDisabled = publishState !== 'READY_TO_PUBLISH' ||
!userCanManageFeed(user, version) ||
jobsProcessingThisVersion

return (
<div>
Expand All @@ -161,7 +155,7 @@ class FeedVersionActionsMTC extends Component<Props> {
: 'Merge with version'
}</span>}
version={version}
versions={feedSource.feedVersionSummaries}
versions={summaries}
/>
<Button
bsStyle={isPublished ? 'success' : 'warning'}
Expand All @@ -186,7 +180,7 @@ class FeedVersionActionsMTC extends Component<Props> {
width: '180px'
}}
>
{publishWarningMessage}
{this._getWarning()}
</div>
</div>
)
Expand Down
1 change: 1 addition & 0 deletions lib/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ export type FeedVersionSummary = {
name: string,
namespace: string,
originNamespace: ?string,
publishState?: 'PUBLISHED' | 'PUBLISHING' | 'PUBLISH_BLOCKED' | 'READY_TO_PUBLISH',
retrievalMethod: ?RetrievalMethod,
updated: number,
// HACK: validationSummary is fully populated in FeedVersion.
Expand Down
Loading