TransferExecutionOptions.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 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 LabelDictionary from "@src/utils/LabelDictionary";
  20. import KeyboardManager from "@src/utils/KeyboardManager";
  21. import { executionOptions } from "@src/constants";
  22. import type { Field } from "@src/@types/Field";
  23. import executionImage from "./images/execution.svg";
  24. import LoadingButton from "@src/components/ui/LoadingButton/LoadingButton";
  25. const Wrapper = styled.div<any>`
  26. display: flex;
  27. flex-direction: column;
  28. align-items: center;
  29. padding: 0 32px 32px 32px;
  30. `;
  31. const ExecutionImage = styled.div<any>`
  32. margin-top: 48px;
  33. margin-bottom: 96px;
  34. width: 96px;
  35. height: 96px;
  36. background: url("${executionImage}") no-repeat center;
  37. `;
  38. const Form = styled.div<any>`
  39. height: 120px;
  40. `;
  41. const Buttons = styled.div<any>`
  42. display: flex;
  43. justify-content: space-between;
  44. width: 100%;
  45. `;
  46. const FieldInputStyled = styled(FieldInput)`
  47. width: 319px;
  48. justify-content: space-between;
  49. `;
  50. type Props = {
  51. options?: { [prop: string]: any } | null;
  52. disableExecutionOptions: boolean;
  53. onChange?: (fieldName: string, fieldValue: string) => void;
  54. executionLabel: string;
  55. executing?: boolean;
  56. onCancelClick: () => void;
  57. onExecuteClick: (options: Field[]) => void;
  58. };
  59. type State = {
  60. fields: Field[];
  61. };
  62. @observer
  63. class TransferExecutionOptions extends React.Component<Props, State> {
  64. static defaultProps = {
  65. executionLabel: "Execute",
  66. executing: false,
  67. };
  68. state: State = {
  69. fields: [...executionOptions],
  70. };
  71. componentDidMount() {
  72. KeyboardManager.onEnter(
  73. "execution-options",
  74. () => {
  75. this.props.onExecuteClick(this.state.fields);
  76. },
  77. 2
  78. );
  79. }
  80. componentWillUnmount() {
  81. KeyboardManager.removeKeyDown("execution-options");
  82. }
  83. getFieldValue(field: Field) {
  84. if (!this.props.options || this.props.options[field.name] == null) {
  85. return field.value;
  86. }
  87. return this.props.options[field.name];
  88. }
  89. handleValueChange(field: Field, value: string) {
  90. this.setState(prevState => {
  91. const fields = prevState.fields.map(f => {
  92. if (f.name === field.name) {
  93. return { ...f, value };
  94. }
  95. return { ...f };
  96. });
  97. return { fields };
  98. });
  99. if (this.props.onChange) {
  100. this.props.onChange(field.name, value);
  101. }
  102. }
  103. render() {
  104. return (
  105. <Wrapper>
  106. <ExecutionImage />
  107. <Form>
  108. {this.state.fields.map(field => (
  109. <FieldInputStyled
  110. key={field.name}
  111. name={field.name}
  112. type="boolean"
  113. layout="page"
  114. value={this.getFieldValue(field)}
  115. label={LabelDictionary.get(field.name)}
  116. onChange={value => this.handleValueChange(field, value)}
  117. disabled={this.props.disableExecutionOptions}
  118. description={
  119. this.props.disableExecutionOptions
  120. ? "The execution options are disabled for the source provider"
  121. : ""
  122. }
  123. />
  124. ))}
  125. </Form>
  126. <Buttons>
  127. <Button secondary onClick={this.props.onCancelClick}>
  128. Cancel
  129. </Button>
  130. {this.props.executing ? (
  131. <LoadingButton>Executing ...</LoadingButton>
  132. ) : (
  133. <Button
  134. onClick={() => {
  135. this.props.onExecuteClick(this.state.fields);
  136. }}
  137. >
  138. {this.props.executionLabel}
  139. </Button>
  140. )}
  141. </Buttons>
  142. </Wrapper>
  143. );
  144. }
  145. }
  146. export default TransferExecutionOptions;