diff --git a/lib/manager/components/version/FeedVersionActionsMTC.js b/lib/manager/components/version/FeedVersionActionsMTC.js index 9f8ea0088..b34e50be7 100644 --- a/lib/manager/components/version/FeedVersionActionsMTC.js +++ b/lib/manager/components/version/FeedVersionActionsMTC.js @@ -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' @@ -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 { _handleMergeVersion: ((versionId: string) => void) = (versionId: string) => { // Note: service period feed merge has only been extensively tested with @@ -83,67 +70,74 @@ class FeedVersionActionsMTC extends Component { _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 ( Cannot publish version because it has a{' '} {hasGtfsPlusBlockingIssue ? 'GTFS+ ' : ''} blocking issue. (See{' '} {hasGtfsPlusBlockingIssue ? 'GTFS+' : 'validation'} issues .) ) - } 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 (
@@ -161,7 +155,7 @@ class FeedVersionActionsMTC extends Component { : 'Merge with version' }} version={version} - versions={feedSource.feedVersionSummaries} + versions={summaries} />
) diff --git a/lib/types/index.js b/lib/types/index.js index 1c1ef3693..8d78f78f3 100644 --- a/lib/types/index.js +++ b/lib/types/index.js @@ -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.