AssessmentMigrationOptions.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "@src/components/ui/Button";
  18. import FieldInput from "@src/components/ui/FieldInput";
  19. import ToggleButtonBar from "@src/components/ui/ToggleButtonBar";
  20. import type { Field } from "@src/@types/Field";
  21. import { ThemeProps } from "@src/components/Theme";
  22. import LabelDictionary from "@src/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 (
  116. this.state.fieldValues[fieldName] &&
  117. this.state.fieldValues[fieldName][propName]
  118. );
  119. }
  120. handleValueChange(fieldName: string, value: any) {
  121. this.setState(prevState => {
  122. const fieldValues = { ...prevState.fieldValues };
  123. if (value != null) {
  124. fieldValues[fieldName] = value;
  125. } else {
  126. delete fieldValues[fieldName];
  127. }
  128. return { fieldValues };
  129. });
  130. }
  131. // UNSAFE_componentDidUpdate(_: Props, prevState: State) {
  132. // if (prevState.showAdvancedOptions !== this.state.showAdvancedOptions
  133. // && this.props.onResizeUpdate && this.scrollableRef) {
  134. // this.props.onResizeUpdate(this.scrollableRef)
  135. // }
  136. // }
  137. handleObjectValueChange(fieldName: string, propName: string, value: any) {
  138. this.setState(prevState => {
  139. const fieldValues = { ...prevState.fieldValues };
  140. if (!fieldValues[fieldName]) {
  141. fieldValues[fieldName] = {};
  142. }
  143. fieldValues[fieldName][propName] = value;
  144. return { fieldValues };
  145. });
  146. }
  147. renderFields() {
  148. let fields: any = generalFields;
  149. const useReplica = this.getFieldValue("use_replica");
  150. const skipFields = [
  151. "location",
  152. "resource_group",
  153. "network_map",
  154. "storage_map",
  155. "vm_size",
  156. "worker_size",
  157. ];
  158. // eslint-disable-next-line no-shadow
  159. const cleanup = (cleanupFields: any[]) =>
  160. cleanupFields
  161. .filter((f: { name: string }) => !skipFields.find(n => n === f.name))
  162. .map((f: { type: string; nullableBoolean: boolean }) => {
  163. if (f.type === "boolean") {
  164. // eslint-disable-next-line no-param-reassign
  165. f.nullableBoolean = true;
  166. }
  167. return { ...f };
  168. });
  169. if (useReplica) {
  170. fields = [...fields, ...replicaFields];
  171. if (this.state.showAdvancedOptions) {
  172. fields = [...fields, ...cleanup(this.props.replicaSchema)];
  173. }
  174. } else {
  175. fields = [...fields, ...migrationFields];
  176. if (this.state.showAdvancedOptions) {
  177. fields = [...fields, ...cleanup(this.props.migrationSchema)];
  178. }
  179. }
  180. const sortPriority: any = {
  181. boolean: 1,
  182. string: 2,
  183. object: 3,
  184. };
  185. fields.sort((a: any, b: any) => {
  186. if (sortPriority[a.type] && sortPriority[b.type]) {
  187. return sortPriority[a.type] - sortPriority[b.type];
  188. }
  189. if (sortPriority[a.type]) {
  190. return -1;
  191. }
  192. if (sortPriority[b.type]) {
  193. return 1;
  194. }
  195. return a.name.localeCompare(b.name);
  196. });
  197. const rows: JSX.Element[] = [];
  198. let lastField: JSX.Element;
  199. fields.forEach((field: any, index: number) => {
  200. let additionalProps;
  201. if (field.type === "object" && field.properties) {
  202. additionalProps = {
  203. valueCallback: (callbackField: { name: string }) =>
  204. this.getObjectFieldValue(field.name, callbackField.name),
  205. onChange: (value: any, callbackField: { name: string }) => {
  206. const propName = callbackField.name.substr(
  207. callbackField.name.lastIndexOf("/") + 1
  208. );
  209. this.handleObjectValueChange(field.name, propName, value);
  210. },
  211. properties: field.properties.map((p: any) => ({
  212. ...p,
  213. required: false,
  214. })),
  215. };
  216. } else {
  217. const value = this.getFieldValue(field.name);
  218. additionalProps = {
  219. value,
  220. // eslint-disable-next-line no-shadow
  221. onChange: (changeValue: any) => {
  222. this.handleValueChange(field.name, changeValue);
  223. },
  224. type: field.type,
  225. };
  226. }
  227. const currentField = (
  228. <FieldStyled
  229. width={ThemeProps.inputSizes.large.width}
  230. // eslint-disable-next-line react/jsx-props-no-spreading
  231. {...field}
  232. // eslint-disable-next-line react/jsx-props-no-spreading
  233. {...additionalProps}
  234. label={field.label || LabelDictionary.get(field.name)}
  235. />
  236. );
  237. const pushRow = (field1: React.ReactNode, field2?: React.ReactNode) => {
  238. rows.push(
  239. <Row key={field.name}>
  240. {field1}
  241. {field2}
  242. </Row>
  243. );
  244. };
  245. if (index === fields.length - 1 && index % 2 === 0) {
  246. pushRow(currentField);
  247. } else if (index % 2 !== 0) {
  248. pushRow(lastField, currentField);
  249. } else {
  250. lastField = currentField;
  251. }
  252. });
  253. return (
  254. // <Fields ref={(ref: HTMLElement | null | undefined) => { this.scrollableRef = ref }}>
  255. <Fields>{rows}</Fields>
  256. );
  257. }
  258. render() {
  259. return (
  260. <Wrapper>
  261. <Image />
  262. <ToggleButtonBarStyled
  263. items={[
  264. { label: "Basic", value: "basic" },
  265. { label: "Advanced", value: "advanced" },
  266. ]}
  267. selectedValue={this.state.showAdvancedOptions ? "advanced" : "basic"}
  268. onChange={item => {
  269. this.setState({ showAdvancedOptions: item.value === "advanced" });
  270. }}
  271. />
  272. {this.renderFields()}
  273. <Buttons>
  274. <Button
  275. large
  276. secondary
  277. onClick={() => {
  278. this.props.onCancelClick();
  279. }}
  280. >
  281. Cancel
  282. </Button>
  283. <Button
  284. large
  285. onClick={() => {
  286. this.props.onExecuteClick(this.state.fieldValues);
  287. }}
  288. disabled={this.props.executeButtonDisabled}
  289. >
  290. Execute
  291. </Button>
  292. </Buttons>
  293. </Wrapper>
  294. );
  295. }
  296. }
  297. export default AssessmentMigrationOptions;