-
Notifications
You must be signed in to change notification settings - Fork 254
/
index.js
95 lines (88 loc) · 2.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import _ from 'intl'
import React from 'react'
import BaseComponent from 'base-component'
import { compileTemplate } from '@xen-orchestra/template'
import { connectStore } from 'utils'
import { Container, Col, Row } from 'grid'
import { createGetObjectsOfType } from 'selectors'
import { forEach } from 'lodash'
const RULES = {
'{date}': () => new Date().toISOString(),
'{description}': vm => vm.name_description,
'{name}': vm => vm.name_label,
}
@connectStore(
{
vms: createGetObjectsOfType('VM').pick((_, props) => props.vms),
},
{ withRef: true }
)
export default class SnapshotVmModalBody extends BaseComponent {
state = {
descriptionPattern: '{description}',
namePattern: '{name}_{date}',
}
get value() {
const { descriptionPattern, namePattern, saveMemory } = this.state
if (namePattern === '' && descriptionPattern === '') {
return { names: {}, descriptions: {}, saveMemory }
}
const generateName = compileTemplate(namePattern, RULES)
const generateDescription = compileTemplate(descriptionPattern, RULES)
const names = {}
const descriptions = {}
forEach(this.props.vms, (vm, id) => {
if (namePattern !== '') {
names[id] = generateName(vm)
}
if (descriptionPattern !== '') {
descriptions[id] = generateDescription(vm)
}
})
return {
descriptions,
names,
saveMemory,
}
}
render() {
return (
<Container>
<Row className='mb-1'>
<Col size={6}>{_('snapshotVmsName')}</Col>
<Col size={6}>
<input
className='form-control'
onChange={this.linkState('namePattern')}
type='text'
value={this.state.namePattern}
/>
</Col>
</Row>
<Row>
<Col size={6}>{_('snapshotVmsDescription')}</Col>
<Col size={6}>
<input
className='form-control'
onChange={this.linkState('descriptionPattern')}
type='text'
value={this.state.descriptionPattern}
/>
</Col>
</Row>
<Row>
<Col>
<label>
<input
type='checkbox'
onChange={this.linkState('saveMemory')}
checked={this.state.saveMemory}
/>{' '}
{_('snapshotSaveMemory')}
</label>
</Col>
</Row>
</Container>
)
}
}