index.jsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. // @flow
  15. import React from 'react'
  16. import styled from 'styled-components'
  17. import { observer } from 'mobx-react'
  18. import Tooltip from '../../atoms/Tooltip'
  19. import StyleProps from '../../styleUtils/StyleProps'
  20. import ToggleButtonBar from '../../atoms/ToggleButtonBar'
  21. import WizardOptionsField from '../../molecules/WizardOptionsField'
  22. import StatusImage from '../../atoms/StatusImage'
  23. import type { Field } from '../../../types/Field'
  24. import type { Instance } from '../../../types/Instance'
  25. import { executionOptions } from '../../../config'
  26. const Wrapper = styled.div``
  27. const Options = styled.div``
  28. const Fields = styled.div`
  29. margin-top: 46px;
  30. display: flex;
  31. `
  32. const OneColumn = styled.div``
  33. const Column = styled.div`
  34. ${props => props.left ? 'margin-right: 160px;' : ''}
  35. `
  36. const WizardOptionsFieldStyled = styled(WizardOptionsField) `
  37. width: ${StyleProps.inputSizes.wizard.width}px;
  38. justify-content: space-between;
  39. margin-bottom: 39px;
  40. `
  41. const LoadingWrapper = styled.div`
  42. margin-top: 32px;
  43. display: flex;
  44. flex-direction: column;
  45. align-items: center;
  46. `
  47. const LoadingText = styled.div`
  48. margin-top: 38px;
  49. font-size: 18px;
  50. `
  51. type Props = {
  52. fields: Field[],
  53. selectedInstances: ?Instance[],
  54. data: ?{ [string]: mixed },
  55. onChange: (field: Field, value: any) => void,
  56. useAdvancedOptions: boolean,
  57. onAdvancedOptionsToggle: (showAdvanced: boolean) => void,
  58. wizardType: string,
  59. loading: boolean,
  60. }
  61. @observer
  62. class WizardOptions extends React.Component<Props> {
  63. getFieldValue(fieldName: string, defaultValue: any) {
  64. if (!this.props.data || this.props.data[fieldName] === undefined) {
  65. return defaultValue
  66. }
  67. return this.props.data[fieldName]
  68. }
  69. getDefaultFieldsSchema() {
  70. let fieldsSchema = [
  71. { name: 'description', type: 'string' },
  72. ]
  73. if (this.props.wizardType === 'migration') {
  74. fieldsSchema.unshift({ name: 'skip_os_morphing', type: 'strict-boolean', default: false })
  75. }
  76. if (this.props.selectedInstances && this.props.selectedInstances.length > 1) {
  77. fieldsSchema.unshift({ name: 'separate_vm', type: 'strict-boolean', default: true })
  78. }
  79. if (this.props.wizardType === 'replica') {
  80. fieldsSchema.push({ name: 'execute_now', type: 'strict-boolean', default: true })
  81. let executeNowValue = this.getFieldValue('execute_now', true)
  82. if (executeNowValue) {
  83. fieldsSchema = [
  84. ...fieldsSchema,
  85. {
  86. name: 'execute_now_options',
  87. type: 'object',
  88. properties: executionOptions,
  89. },
  90. ]
  91. }
  92. }
  93. return fieldsSchema
  94. }
  95. renderOptionsField(field: Field) {
  96. if (field.type === 'object' && field.properties) {
  97. return (
  98. <WizardOptionsFieldStyled
  99. key={field.name}
  100. name={field.name}
  101. type={field.type}
  102. valueCallback={f => this.getFieldValue(f.name, f.default)}
  103. properties={field.properties}
  104. onChange={(f, value) => { this.props.onChange(f, value) }}
  105. />
  106. )
  107. }
  108. return (
  109. <WizardOptionsFieldStyled
  110. key={field.name}
  111. name={field.name}
  112. type={field.type}
  113. enum={field.enum}
  114. required={field.required}
  115. value={this.getFieldValue(field.name, field.default)}
  116. onChange={value => { this.props.onChange(field, value) }}
  117. />
  118. )
  119. }
  120. renderOptionsFields() {
  121. let fieldsSchema = this.getDefaultFieldsSchema()
  122. fieldsSchema = fieldsSchema.concat(this.props.fields.filter(f => f.required))
  123. if (this.props.useAdvancedOptions) {
  124. fieldsSchema = fieldsSchema.concat(this.props.fields.filter(f => !f.required))
  125. }
  126. let executeNowColumn
  127. let fields = fieldsSchema.map((field, i) => {
  128. let column = i % 2 === 0 ? 'left' : 'right'
  129. if (field.name === 'execute_now') {
  130. executeNowColumn = column
  131. }
  132. if (field.name === 'execute_now_options') {
  133. column = executeNowColumn
  134. }
  135. return {
  136. column,
  137. component: this.renderOptionsField(field),
  138. }
  139. })
  140. if (fields.length < 8) {
  141. return (
  142. <Fields>
  143. <OneColumn>
  144. {fields.map(f => f.component)}
  145. </OneColumn>
  146. <Tooltip />
  147. </Fields>
  148. )
  149. }
  150. return (
  151. <Fields>
  152. <Column left>
  153. {fields.map(f => f.column === 'left' && f.component)}
  154. </Column>
  155. <Column right>
  156. {fields.map(f => f.column === 'right' && f.component)}
  157. </Column>
  158. <Tooltip />
  159. </Fields>
  160. )
  161. }
  162. renderLoading() {
  163. if (!this.props.loading) {
  164. return null
  165. }
  166. return (
  167. <LoadingWrapper>
  168. <StatusImage loading />
  169. <LoadingText>Loading options...</LoadingText>
  170. </LoadingWrapper>
  171. )
  172. }
  173. renderOptions() {
  174. if (this.props.loading) {
  175. return null
  176. }
  177. return (
  178. <Options>
  179. <ToggleButtonBar
  180. items={[{ label: 'Simple', value: 'simple' }, { label: 'Advanced', value: 'advanced' }]}
  181. selectedValue={this.props.useAdvancedOptions ? 'advanced' : 'simple'}
  182. onChange={item => { this.props.onAdvancedOptionsToggle(item.value === 'advanced') }}
  183. />
  184. {this.renderOptionsFields()}
  185. </Options>
  186. )
  187. }
  188. render() {
  189. return (
  190. <Wrapper>
  191. {this.renderOptions()}
  192. {this.renderLoading()}
  193. </Wrapper>
  194. )
  195. }
  196. }
  197. export default WizardOptions