WizardOptions.jsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. constructor() {
  64. super()
  65. // $FlowIssue
  66. this.handleResize = this.handleResize.bind(this)
  67. }
  68. componentDidMount() {
  69. window.addEventListener('resize', this.handleResize)
  70. }
  71. componentDidUpdate() {
  72. Tooltip.rebuild()
  73. }
  74. componentWillUnmount() {
  75. window.removeEventListener('resize', this.handleResize, false)
  76. }
  77. getFieldValue(fieldName: string, defaultValue: any) {
  78. if (!this.props.data || this.props.data[fieldName] === undefined) {
  79. return defaultValue
  80. }
  81. return this.props.data[fieldName]
  82. }
  83. getDefaultFieldsSchema() {
  84. let fieldsSchema = [
  85. { name: 'description', type: 'string' },
  86. ]
  87. if (this.props.wizardType === 'migration') {
  88. fieldsSchema.unshift({ name: 'skip_os_morphing', type: 'strict-boolean', default: false })
  89. }
  90. if (this.props.selectedInstances && this.props.selectedInstances.length > 1) {
  91. fieldsSchema.unshift({ name: 'separate_vm', type: 'strict-boolean', default: true })
  92. }
  93. if (this.props.wizardType === 'replica') {
  94. fieldsSchema.push({ name: 'execute_now', type: 'strict-boolean', default: true })
  95. let executeNowValue = this.getFieldValue('execute_now', true)
  96. if (executeNowValue) {
  97. fieldsSchema = [
  98. ...fieldsSchema,
  99. {
  100. name: 'execute_now_options',
  101. type: 'object',
  102. properties: executionOptions,
  103. },
  104. ]
  105. }
  106. }
  107. return fieldsSchema
  108. }
  109. handleResize() {
  110. this.setState({})
  111. }
  112. shouldRenderField(field: Field) {
  113. return (field.type !== 'array' || (field.enum && field.enum.length && field.enum.length > 0)) &&
  114. (field.type !== 'integer' || (field.minimum && field.maximum)) &&
  115. (field.type !== 'object' || field.properties)
  116. }
  117. renderOptionsField(field: Field) {
  118. let additionalProps
  119. if (field.type === 'object' && field.properties) {
  120. additionalProps = {
  121. valueCallback: f => this.getFieldValue(f.name, f.default),
  122. onChange: (f, value) => { this.props.onChange(f, value) },
  123. properties: field.properties,
  124. }
  125. } else {
  126. additionalProps = {
  127. value: this.getFieldValue(field.name, field.default),
  128. onChange: value => { this.props.onChange(field, value) },
  129. }
  130. }
  131. return (
  132. <WizardOptionsFieldStyled
  133. key={field.name}
  134. name={field.name}
  135. type={field.type}
  136. enum={field.enum}
  137. required={field.required}
  138. data-test-id={`wOptions-field-${field.name}`}
  139. {...additionalProps}
  140. />
  141. )
  142. }
  143. renderOptionsFields() {
  144. let fieldsSchema = this.getDefaultFieldsSchema()
  145. fieldsSchema = fieldsSchema.concat(this.props.fields.filter(f => f.required))
  146. if (this.props.useAdvancedOptions) {
  147. fieldsSchema = fieldsSchema.concat(this.props.fields.filter(f => !f.required))
  148. }
  149. let executeNowColumn
  150. let fields = fieldsSchema.filter(f => this.shouldRenderField(f)).map((field, i) => {
  151. let column = i % 2 === 0 ? 'left' : 'right'
  152. if (field.name === 'execute_now') {
  153. executeNowColumn = column
  154. }
  155. if (field.name === 'execute_now_options') {
  156. column = executeNowColumn
  157. }
  158. return {
  159. column,
  160. component: this.renderOptionsField(field),
  161. }
  162. })
  163. if (fields.length * 96 < window.innerHeight - 450) {
  164. return (
  165. <Fields>
  166. <OneColumn>
  167. {fields.map(f => f.component)}
  168. </OneColumn>
  169. <Tooltip />
  170. </Fields>
  171. )
  172. }
  173. return (
  174. <Fields>
  175. <Column left>
  176. {fields.map(f => f.column === 'left' && f.component)}
  177. </Column>
  178. <Column right>
  179. {fields.map(f => f.column === 'right' && f.component)}
  180. </Column>
  181. <Tooltip />
  182. </Fields>
  183. )
  184. }
  185. renderLoading() {
  186. if (!this.props.loading) {
  187. return null
  188. }
  189. return (
  190. <LoadingWrapper>
  191. <StatusImage loading />
  192. <LoadingText>Loading options...</LoadingText>
  193. </LoadingWrapper>
  194. )
  195. }
  196. renderOptions() {
  197. if (this.props.loading) {
  198. return null
  199. }
  200. return (
  201. <Options>
  202. <ToggleButtonBar
  203. items={[{ label: 'Simple', value: 'simple' }, { label: 'Advanced', value: 'advanced' }]}
  204. selectedValue={this.props.useAdvancedOptions ? 'advanced' : 'simple'}
  205. onChange={item => { this.props.onAdvancedOptionsToggle(item.value === 'advanced') }}
  206. />
  207. {this.renderOptionsFields()}
  208. </Options>
  209. )
  210. }
  211. render() {
  212. return (
  213. <Wrapper>
  214. {this.renderOptions()}
  215. {this.renderLoading()}
  216. </Wrapper>
  217. )
  218. }
  219. }
  220. export default WizardOptions