AssessmentMigrationOptions.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. import * as React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled from 'styled-components'
  17. import Button from '../../../ui/Button/Button'
  18. import FieldInput from '../../../ui/FieldInput/FieldInput'
  19. import ToggleButtonBar from '../../../ui/ToggleButtonBar/ToggleButtonBar'
  20. import type { Field } from '../../../../@types/Field'
  21. import { ThemeProps } from '../../../Theme'
  22. import LabelDictionary from '../../../../utils/LabelDictionary'
  23. import assessmentImage from './images/assessment.svg'
  24. const Wrapper = styled.div<any>`
  25. padding: 48px 32px 32px 32px;
  26. display: flex;
  27. flex-direction: column;
  28. align-items: center;
  29. min-height: 0;
  30. `
  31. const Image = styled.div<any>`
  32. width: 96px;
  33. height: 96px;
  34. background: url('${assessmentImage}') center no-repeat;
  35. `
  36. const ToggleButtonBarStyled = styled(ToggleButtonBar)`
  37. margin-top: 48px;
  38. `
  39. const Fields = styled.div<any>`
  40. display: flex;
  41. margin-top: 32px;
  42. flex-direction: column;
  43. overflow: auto;
  44. width: 100%;
  45. min-height: 0;
  46. `
  47. const FieldStyled = styled(FieldInput)`
  48. ${ThemeProps.exactWidth(`${ThemeProps.inputSizes.large.width}px`)}
  49. margin-bottom: 16px;
  50. `
  51. const Row = styled.div<any>`
  52. display: flex;
  53. flex-shrink: 0;
  54. justify-content: space-between;
  55. `
  56. const Buttons = styled.div<any>`
  57. display: flex;
  58. justify-content: space-between;
  59. width: 100%;
  60. margin-top: 32px;
  61. `
  62. const generalFields = [
  63. {
  64. name: 'use_replica',
  65. type: 'boolean',
  66. },
  67. {
  68. name: 'separate_vm',
  69. type: 'boolean',
  70. },
  71. ]
  72. const replicaFields = [
  73. {
  74. name: 'shutdown_instances',
  75. type: 'boolean',
  76. },
  77. ]
  78. const migrationFields = [
  79. {
  80. name: 'skip_os_morphing',
  81. type: 'boolean',
  82. },
  83. ]
  84. type Props = {
  85. onCancelClick: () => void,
  86. onExecuteClick: (fieldValues: { [prop: string]: any }) => void,
  87. executeButtonDisabled: boolean,
  88. replicaSchema: Field[],
  89. migrationSchema: Field[],
  90. onResizeUpdate?: (scrollableRef: HTMLElement, scrollOffset?: number) => void,
  91. }
  92. type State = {
  93. fieldValues: { [prop: string]: any },
  94. showAdvancedOptions: boolean,
  95. }
  96. @observer
  97. class AssessmentMigrationOptions extends React.Component<Props, State> {
  98. state: State = {
  99. fieldValues: {
  100. separate_vm: true,
  101. use_replica: false,
  102. shutdown_instances: false,
  103. skip_os_morphing: false,
  104. },
  105. showAdvancedOptions: false,
  106. }
  107. scrollableRef: HTMLElement | undefined | null
  108. getFieldValue(fieldName: string) {
  109. if (this.state.fieldValues[fieldName] != null) {
  110. return this.state.fieldValues[fieldName]
  111. }
  112. return null
  113. }
  114. getObjectFieldValue(fieldName: string, propName: string) {
  115. return this.state.fieldValues[fieldName] && this.state.fieldValues[fieldName][propName]
  116. }
  117. handleValueChange(fieldName: string, value: any) {
  118. this.setState(prevState => {
  119. const fieldValues = { ...prevState.fieldValues }
  120. if (value != null) {
  121. fieldValues[fieldName] = value
  122. } else {
  123. delete fieldValues[fieldName]
  124. }
  125. return { fieldValues }
  126. })
  127. }
  128. UNSAFE_componentDidUpdate(_: Props, prevState: State) {
  129. if (prevState.showAdvancedOptions !== this.state.showAdvancedOptions
  130. && this.props.onResizeUpdate && this.scrollableRef) {
  131. this.props.onResizeUpdate(this.scrollableRef)
  132. }
  133. }
  134. handleObjectValueChange(fieldName: string, propName: string, value: any) {
  135. this.setState(prevState => {
  136. const fieldValues = { ...prevState.fieldValues }
  137. if (!fieldValues[fieldName]) {
  138. fieldValues[fieldName] = {}
  139. }
  140. fieldValues[fieldName][propName] = value
  141. return { fieldValues }
  142. })
  143. }
  144. renderFields() {
  145. let fields: any = generalFields
  146. const useReplica = this.getFieldValue('use_replica')
  147. const skipFields = ['location', 'resource_group', 'network_map', 'storage_map', 'vm_size', 'worker_size']
  148. // eslint-disable-next-line no-shadow
  149. const cleanup = (cleanupFields: any[]) => cleanupFields.filter((f: {
  150. name: string
  151. }) => !skipFields
  152. .find(n => n === f.name)).map((f: { type: string; nullableBoolean: boolean }) => {
  153. if (f.type === 'boolean') {
  154. // eslint-disable-next-line no-param-reassign
  155. f.nullableBoolean = true
  156. }
  157. return { ...f }
  158. })
  159. if (useReplica) {
  160. fields = [...fields, ...replicaFields]
  161. if (this.state.showAdvancedOptions) {
  162. fields = [
  163. ...fields,
  164. ...cleanup(this.props.replicaSchema),
  165. ]
  166. }
  167. } else {
  168. fields = [...fields, ...migrationFields]
  169. if (this.state.showAdvancedOptions) {
  170. fields = [
  171. ...fields,
  172. ...cleanup(this.props.migrationSchema),
  173. ]
  174. }
  175. }
  176. const sortPriority: any = {
  177. boolean: 1,
  178. string: 2,
  179. object: 3,
  180. }
  181. fields.sort((a: any, b: any) => {
  182. if (sortPriority[a.type] && sortPriority[b.type]) {
  183. return sortPriority[a.type] - sortPriority[b.type]
  184. }
  185. if (sortPriority[a.type]) {
  186. return -1
  187. }
  188. if (sortPriority[b.type]) {
  189. return 1
  190. }
  191. return a.name.localeCompare(b.name)
  192. })
  193. const rows: JSX.Element[] = []
  194. let lastField: JSX.Element
  195. fields.forEach((field: any, index: number) => {
  196. let additionalProps
  197. if (field.type === 'object' && field.properties) {
  198. additionalProps = {
  199. valueCallback: (
  200. callbackField: { name: string },
  201. ) => this.getObjectFieldValue(field.name, callbackField.name),
  202. onChange: (value: any, callbackField: { name: string }) => {
  203. const propName = callbackField.name.substr(callbackField.name.lastIndexOf('/') + 1)
  204. this.handleObjectValueChange(field.name, propName, value)
  205. },
  206. properties: field.properties.map((p: any) => ({ ...p, required: false })),
  207. }
  208. } else {
  209. const value = this.getFieldValue(field.name)
  210. additionalProps = {
  211. value,
  212. // eslint-disable-next-line no-shadow
  213. onChange: (changeValue: any) => { this.handleValueChange(field.name, changeValue) },
  214. type: field.type,
  215. }
  216. }
  217. const currentField = (
  218. <FieldStyled
  219. width={ThemeProps.inputSizes.large.width}
  220. // eslint-disable-next-line react/jsx-props-no-spreading
  221. {...field}
  222. // eslint-disable-next-line react/jsx-props-no-spreading
  223. {...additionalProps}
  224. label={field.label || LabelDictionary.get(field.name)}
  225. />
  226. )
  227. const pushRow = (field1: React.ReactNode, field2?: React.ReactNode) => {
  228. rows.push((
  229. <Row key={field.name}>
  230. {field1}
  231. {field2}
  232. </Row>
  233. ))
  234. }
  235. if (index === fields.length - 1 && index % 2 === 0) {
  236. pushRow(currentField)
  237. } else if (index % 2 !== 0) {
  238. pushRow(lastField, currentField)
  239. } else {
  240. lastField = currentField
  241. }
  242. })
  243. return (
  244. <Fields ref={(ref: HTMLElement | null | undefined) => { this.scrollableRef = ref }}>
  245. {rows}
  246. </Fields>
  247. )
  248. }
  249. render() {
  250. return (
  251. <Wrapper>
  252. <Image />
  253. <ToggleButtonBarStyled
  254. items={[{ label: 'Basic', value: 'basic' }, { label: 'Advanced', value: 'advanced' }]}
  255. selectedValue={this.state.showAdvancedOptions ? 'advanced' : 'basic'}
  256. onChange={item => { this.setState({ showAdvancedOptions: item.value === 'advanced' }) }}
  257. />
  258. {this.renderFields()}
  259. <Buttons>
  260. <Button
  261. large
  262. secondary
  263. onClick={() => { this.props.onCancelClick() }}
  264. >Cancel
  265. </Button>
  266. <Button
  267. large
  268. onClick={() => { this.props.onExecuteClick(this.state.fieldValues) }}
  269. disabled={this.props.executeButtonDisabled}
  270. >Execute
  271. </Button>
  272. </Buttons>
  273. </Wrapper>
  274. )
  275. }
  276. }
  277. export default AssessmentMigrationOptions