| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /*
- Copyright (C) 2017 Cloudbase Solutions SRL
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- import * as React from "react";
- import { observer } from "mobx-react";
- import styled from "styled-components";
- import Button from "@src/components/ui/Button";
- import FieldInput from "@src/components/ui/FieldInput";
- import ToggleButtonBar from "@src/components/ui/ToggleButtonBar";
- import type { Field } from "@src/@types/Field";
- import { ThemeProps } from "@src/components/Theme";
- import LabelDictionary from "@src/utils/LabelDictionary";
- import assessmentImage from "./images/assessment.svg";
- const Wrapper = styled.div<any>`
- padding: 48px 32px 32px 32px;
- display: flex;
- flex-direction: column;
- align-items: center;
- min-height: 0;
- `;
- const Image = styled.div<any>`
- width: 96px;
- height: 96px;
- background: url("${assessmentImage}") center no-repeat;
- `;
- const ToggleButtonBarStyled = styled(ToggleButtonBar)`
- margin-top: 48px;
- `;
- const Fields = styled.div<any>`
- display: flex;
- margin-top: 32px;
- flex-direction: column;
- overflow: auto;
- width: 100%;
- min-height: 0;
- `;
- const FieldStyled = styled(FieldInput)`
- ${ThemeProps.exactWidth(`${ThemeProps.inputSizes.large.width}px`)}
- margin-bottom: 16px;
- `;
- const Row = styled.div<any>`
- display: flex;
- flex-shrink: 0;
- justify-content: space-between;
- `;
- const Buttons = styled.div<any>`
- display: flex;
- justify-content: space-between;
- width: 100%;
- margin-top: 32px;
- `;
- const generalFields = [
- {
- name: "use_replica",
- type: "boolean",
- },
- {
- name: "separate_vm",
- type: "boolean",
- },
- ];
- const replicaFields = [
- {
- name: "shutdown_instances",
- type: "boolean",
- },
- ];
- const migrationFields = [
- {
- name: "skip_os_morphing",
- type: "boolean",
- },
- ];
- type Props = {
- onCancelClick: () => void;
- onExecuteClick: (fieldValues: { [prop: string]: any }) => void;
- executeButtonDisabled: boolean;
- replicaSchema: Field[];
- migrationSchema: Field[];
- onResizeUpdate?: (scrollableRef: HTMLElement, scrollOffset?: number) => void;
- };
- type State = {
- fieldValues: { [prop: string]: any };
- showAdvancedOptions: boolean;
- };
- @observer
- class AssessmentMigrationOptions extends React.Component<Props, State> {
- state: State = {
- fieldValues: {
- separate_vm: true,
- use_replica: false,
- shutdown_instances: false,
- skip_os_morphing: false,
- },
- showAdvancedOptions: false,
- };
- // scrollableRef: HTMLElement | undefined | null
- getFieldValue(fieldName: string) {
- if (this.state.fieldValues[fieldName] != null) {
- return this.state.fieldValues[fieldName];
- }
- return null;
- }
- getObjectFieldValue(fieldName: string, propName: string) {
- return (
- this.state.fieldValues[fieldName] &&
- this.state.fieldValues[fieldName][propName]
- );
- }
- handleValueChange(fieldName: string, value: any) {
- this.setState(prevState => {
- const fieldValues = { ...prevState.fieldValues };
- if (value != null) {
- fieldValues[fieldName] = value;
- } else {
- delete fieldValues[fieldName];
- }
- return { fieldValues };
- });
- }
- // UNSAFE_componentDidUpdate(_: Props, prevState: State) {
- // if (prevState.showAdvancedOptions !== this.state.showAdvancedOptions
- // && this.props.onResizeUpdate && this.scrollableRef) {
- // this.props.onResizeUpdate(this.scrollableRef)
- // }
- // }
- handleObjectValueChange(fieldName: string, propName: string, value: any) {
- this.setState(prevState => {
- const fieldValues = { ...prevState.fieldValues };
- if (!fieldValues[fieldName]) {
- fieldValues[fieldName] = {};
- }
- fieldValues[fieldName][propName] = value;
- return { fieldValues };
- });
- }
- renderFields() {
- let fields: any = generalFields;
- const useReplica = this.getFieldValue("use_replica");
- const skipFields = [
- "location",
- "resource_group",
- "network_map",
- "storage_map",
- "vm_size",
- "worker_size",
- ];
- // eslint-disable-next-line no-shadow
- const cleanup = (cleanupFields: any[]) =>
- cleanupFields
- .filter((f: { name: string }) => !skipFields.find(n => n === f.name))
- .map((f: { type: string; nullableBoolean: boolean }) => {
- if (f.type === "boolean") {
- // eslint-disable-next-line no-param-reassign
- f.nullableBoolean = true;
- }
- return { ...f };
- });
- if (useReplica) {
- fields = [...fields, ...replicaFields];
- if (this.state.showAdvancedOptions) {
- fields = [...fields, ...cleanup(this.props.replicaSchema)];
- }
- } else {
- fields = [...fields, ...migrationFields];
- if (this.state.showAdvancedOptions) {
- fields = [...fields, ...cleanup(this.props.migrationSchema)];
- }
- }
- const sortPriority: any = {
- boolean: 1,
- string: 2,
- object: 3,
- };
- fields.sort((a: any, b: any) => {
- if (sortPriority[a.type] && sortPriority[b.type]) {
- return sortPriority[a.type] - sortPriority[b.type];
- }
- if (sortPriority[a.type]) {
- return -1;
- }
- if (sortPriority[b.type]) {
- return 1;
- }
- return a.name.localeCompare(b.name);
- });
- const rows: JSX.Element[] = [];
- let lastField: JSX.Element;
- fields.forEach((field: any, index: number) => {
- let additionalProps;
- if (field.type === "object" && field.properties) {
- additionalProps = {
- valueCallback: (callbackField: { name: string }) =>
- this.getObjectFieldValue(field.name, callbackField.name),
- onChange: (value: any, callbackField: { name: string }) => {
- const propName = callbackField.name.substr(
- callbackField.name.lastIndexOf("/") + 1
- );
- this.handleObjectValueChange(field.name, propName, value);
- },
- properties: field.properties.map((p: any) => ({
- ...p,
- required: false,
- })),
- };
- } else {
- const value = this.getFieldValue(field.name);
- additionalProps = {
- value,
- // eslint-disable-next-line no-shadow
- onChange: (changeValue: any) => {
- this.handleValueChange(field.name, changeValue);
- },
- type: field.type,
- };
- }
- const currentField = (
- <FieldStyled
- width={ThemeProps.inputSizes.large.width}
- // eslint-disable-next-line react/jsx-props-no-spreading
- {...field}
- // eslint-disable-next-line react/jsx-props-no-spreading
- {...additionalProps}
- label={field.label || LabelDictionary.get(field.name)}
- />
- );
- const pushRow = (field1: React.ReactNode, field2?: React.ReactNode) => {
- rows.push(
- <Row key={field.name}>
- {field1}
- {field2}
- </Row>
- );
- };
- if (index === fields.length - 1 && index % 2 === 0) {
- pushRow(currentField);
- } else if (index % 2 !== 0) {
- pushRow(lastField, currentField);
- } else {
- lastField = currentField;
- }
- });
- return (
- // <Fields ref={(ref: HTMLElement | null | undefined) => { this.scrollableRef = ref }}>
- <Fields>{rows}</Fields>
- );
- }
- render() {
- return (
- <Wrapper>
- <Image />
- <ToggleButtonBarStyled
- items={[
- { label: "Basic", value: "basic" },
- { label: "Advanced", value: "advanced" },
- ]}
- selectedValue={this.state.showAdvancedOptions ? "advanced" : "basic"}
- onChange={item => {
- this.setState({ showAdvancedOptions: item.value === "advanced" });
- }}
- />
- {this.renderFields()}
- <Buttons>
- <Button
- large
- secondary
- onClick={() => {
- this.props.onCancelClick();
- }}
- >
- Cancel
- </Button>
- <Button
- large
- onClick={() => {
- this.props.onExecuteClick(this.state.fieldValues);
- }}
- disabled={this.props.executeButtonDisabled}
- >
- Execute
- </Button>
- </Buttons>
- </Wrapper>
- );
- }
- }
- export default AssessmentMigrationOptions;
|