fix(xo-web/backup/overview): fix the race condition between subscriptions (#2766)

Fixes #2733
This commit is contained in:
badrAZ 2018-03-14 14:15:47 +01:00 committed by Julien Fontanet
parent 35f210e074
commit 301ab65c01

View File

@ -14,7 +14,7 @@ import { addSubscriptions } from 'utils'
import { constructQueryString } from 'smart-backup-pattern' import { constructQueryString } from 'smart-backup-pattern'
import { createSelector } from 'selectors' import { createSelector } from 'selectors'
import { Card, CardHeader, CardBlock } from 'card' import { Card, CardHeader, CardBlock } from 'card'
import { filter, find, forEach, get, map, orderBy } from 'lodash' import { filter, find, forEach, get, keyBy, map, orderBy } from 'lodash'
import { import {
deleteBackupSchedule, deleteBackupSchedule,
disableSchedule, disableSchedule,
@ -125,6 +125,8 @@ const JOB_COLUMNS = [
// =================================================================== // ===================================================================
@addSubscriptions({ @addSubscriptions({
jobs: cb => subscribeJobs(jobs => cb(keyBy(jobs, 'id'))),
schedules: cb => subscribeSchedules(schedules => cb(keyBy(schedules, 'id'))),
users: subscribeUsers, users: subscribeUsers,
}) })
export default class Overview extends Component { export default class Overview extends Component {
@ -132,37 +134,20 @@ export default class Overview extends Component {
router: React.PropTypes.object, router: React.PropTypes.object,
} }
componentWillMount () { _getSchedules = createSelector(
const unsubscribeJobs = subscribeJobs(jobs => { () => this.props.jobs,
const obj = {} () => this.props.schedules,
forEach(jobs, job => { (jobs, schedules) =>
obj[job.id] = job jobs === undefined || schedules === undefined
}) ? []
: orderBy(
this.setState({ filter(schedules, schedule => {
jobs: obj, const job = jobs[schedule.jobId]
})
})
const unsubscribeSchedules = subscribeSchedules(schedules => {
// Get only backup jobs.
schedules = filter(schedules, schedule => {
const job = this.state.jobs && this.state.jobs[schedule.jobId]
return job && jobKeyToLabel[job.key] return job && jobKeyToLabel[job.key]
}) }),
'id'
this.setState({ )
schedules: orderBy(schedules, schedule => +schedule.id.split(':')[1], [ )
'desc',
]),
})
})
this.componentWillUnmount = () => {
unsubscribeJobs()
unsubscribeSchedules()
}
}
_redirectToMatchingVms = pattern => { _redirectToMatchingVms = pattern => {
this.context.router.push({ this.context.router.push({
@ -172,8 +157,8 @@ export default class Overview extends Component {
} }
_getScheduleCollection = createSelector( _getScheduleCollection = createSelector(
() => this.state.schedules, this._getSchedules,
() => this.state.jobs, () => this.props.jobs,
(schedules, jobs) => { (schedules, jobs) => {
if (!schedules || !jobs) { if (!schedules || !jobs) {
return [] return []
@ -202,8 +187,8 @@ export default class Overview extends Component {
) )
_getIsScheduleUserMissing = createSelector( _getIsScheduleUserMissing = createSelector(
() => this.state.schedules, this._getSchedules,
() => this.state.jobs, () => this.props.jobs,
() => this.props.users, () => this.props.users,
(schedules, jobs, users) => { (schedules, jobs, users) => {
const isScheduleUserMissing = {} const isScheduleUserMissing = {}
@ -218,8 +203,7 @@ export default class Overview extends Component {
) )
render () { render () {
const { schedules } = this.state const schedules = this._getSchedules()
const isScheduleUserMissing = this._getIsScheduleUserMissing() const isScheduleUserMissing = this._getIsScheduleUserMissing()
return ( return (