chore(xo-web/restore): minor improvements & fixes (#2789)

Fixes #2692
This commit is contained in:
Pierre Donias 2018-03-21 15:51:12 +01:00 committed by Julien Fontanet
parent ff59d091f1
commit a4d0fa62d2

View File

@ -1,20 +1,10 @@
import _, { messages } from 'intl' import _, { messages } from 'intl'
import ChooseSrForEachVdisModal from 'xo/choose-sr-for-each-vdis-modal' import ChooseSrForEachVdisModal from 'xo/choose-sr-for-each-vdis-modal'
import Component from 'base-component' import Component from 'base-component'
import every from 'lodash/every'
import filter from 'lodash/filter'
import find from 'lodash/find'
import forEach from 'lodash/forEach'
import groupBy from 'lodash/groupBy'
import Icon from 'icon' import Icon from 'icon'
import isEmpty from 'lodash/isEmpty'
import map from 'lodash/map'
import mapValues from 'lodash/mapValues'
import moment from 'moment' import moment from 'moment'
import React from 'react' import React from 'react'
import reduce from 'lodash/reduce'
import SortedTable from 'sorted-table' import SortedTable from 'sorted-table'
import uniq from 'lodash/uniq'
import Upgrade from 'xoa-upgrade' import Upgrade from 'xoa-upgrade'
import { confirm } from 'modal' import { confirm } from 'modal'
import { createSelector } from 'selectors' import { createSelector } from 'selectors'
@ -23,6 +13,19 @@ import { Container, Row, Col } from 'grid'
import { FormattedDate, injectIntl } from 'react-intl' import { FormattedDate, injectIntl } from 'react-intl'
import { info, error } from 'notification' import { info, error } from 'notification'
import { Select, Toggle } from 'form' import { Select, Toggle } from 'form'
import {
countBy,
every,
filter,
find,
forEach,
groupBy,
isEmpty,
map,
mapValues,
reduce,
uniq,
} from 'lodash'
import { import {
importBackup, importBackup,
@ -98,19 +101,19 @@ const VM_COLUMNS = [
}, },
{ {
name: _('availableBackupsColumn'), name: _('availableBackupsColumn'),
itemRenderer: ({ simpleCount, deltaCount }) => ( itemRenderer: ({ count }) => (
<span> <span>
{!!simpleCount && ( {count.simple > 0 && (
<span> <span>
{_('simpleBackup')}{' '} {_('simpleBackup')}{' '}
<span className='tag tag-pill tag-primary'>{simpleCount}</span> <span className='tag tag-pill tag-primary'>{count.simple}</span>
</span> </span>
)} )}
{!!simpleCount && !!deltaCount && ', '} {count.simple > 0 && count.delta > 0 && ', '}
{!!deltaCount && ( {count.delta > 0 && (
<span> <span>
{_('delta')}{' '} {_('delta')}{' '}
<span className='tag tag-pill tag-primary'>{deltaCount}</span> <span className='tag tag-pill tag-primary'>{count.delta}</span>
</span> </span>
)} )}
</span> </span>
@ -140,8 +143,6 @@ const doImport = ({ backup, targetSrs, start }) => {
mapVdisSrs: targetSrs.mapVdisSrs, mapVdisSrs: targetSrs.mapVdisSrs,
remote: backup.remoteId, remote: backup.remoteId,
sr: targetSrs.mainSr, sr: targetSrs.mainSr,
}).then(id => {
return id
}) })
if (start) { if (start) {
importPromise.then(id => startVm({ id })) importPromise.then(id => startVm({ id }))
@ -242,16 +243,20 @@ const ImportModalBody = injectIntl(_ModalBody, { withRef: true })
}) })
export default class Restore extends Component { export default class Restore extends Component {
componentWillReceiveProps ({ rawRemotes }) { componentWillReceiveProps ({ rawRemotes }) {
let filteredRemotes if (rawRemotes !== this.props.rawRemotes) {
if ( this._listAll(rawRemotes).catch(noop)
(filteredRemotes = filter(rawRemotes, 'enabled')) !==
filter(this.props.rawRemotes, 'enabled')
) {
this._listAll(filteredRemotes).catch(noop)
} }
} }
_listAll = async remotes => { componentDidMount () {
const { rawRemotes } = this.props
if (rawRemotes !== undefined) {
this._listAll(rawRemotes).catch(noop)
}
}
_listAll = async rawRemotes => {
const remotes = filter(rawRemotes, 'enabled')
const remotesInfo = await Promise.all( const remotesInfo = await Promise.all(
map(remotes, async remote => ({ map(remotes, async remote => ({
files: await listRemote(remote.id), files: await listRemote(remote.id),
@ -309,24 +314,15 @@ export default class Restore extends Component {
forEach(backupInfoByVm, (backups, vm) => { forEach(backupInfoByVm, (backups, vm) => {
backupInfoByVm[vm] = { backupInfoByVm[vm] = {
backups, backups,
count: countBy(backups, 'type'),
last: reduce(backups, (last, b) => (b.date > last.date ? b : last)), last: reduce(backups, (last, b) => (b.date > last.date ? b : last)),
tagsByRemote: mapValues( tagsByRemote: mapValues(
groupBy(backups, 'remoteId'), groupBy(backups, 'remoteId'),
(backups, remoteId) => ({ (backups, remoteId) => ({
remoteName: find(remotes, remote => remote.id === remoteId).name, remoteName: backups[0].remoteName,
tags: uniq(map(backups, 'tag')), tags: uniq(map(backups, 'tag')),
}) })
), ),
simpleCount: reduce(
backups,
(sum, b) => (b.type === 'simple' ? ++sum : sum),
0
),
deltaCount: reduce(
backups,
(sum, b) => (b.type === 'delta' ? ++sum : sum),
0
),
} }
}) })
this.setState({ backupInfoByVm }) this.setState({ backupInfoByVm })