ReplicaExecutionOptions.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. const Wrapper = styled.div<any>`
  25. display: flex;
  26. flex-direction: column;
  27. align-items: center;
  28. padding: 0 32px 32px 32px;
  29. `
  30. const ExecutionImage = styled.div<any>`
  31. margin-top: 48px;
  32. margin-bottom: 96px;
  33. width: 96px;
  34. height: 96px;
  35. background: url('${executionImage}') no-repeat center;
  36. `
  37. const Form = styled.div<any>`
  38. height: 120px;
  39. `
  40. const Buttons = styled.div<any>`
  41. display: flex;
  42. justify-content: space-between;
  43. width: 100%;
  44. `
  45. const FieldInputStyled = styled(FieldInput)`
  46. width: 319px;
  47. justify-content: space-between;
  48. `
  49. type Props = {
  50. options?: { [prop: string]: any } | null,
  51. onChange?: (fieldName: string, fieldValue: string) => void,
  52. executionLabel: string,
  53. onCancelClick: () => void,
  54. onExecuteClick: (options: Field[]) => void,
  55. }
  56. type State = {
  57. fields: Field[],
  58. }
  59. @observer
  60. class ReplicaExecutionOptions extends React.Component<Props, State> {
  61. static defaultProps = {
  62. executionLabel: 'Execute',
  63. }
  64. state: State = {
  65. fields: [...executionOptions],
  66. }
  67. componentDidMount() {
  68. KeyboardManager.onEnter('execution-options', () => { this.props.onExecuteClick(this.state.fields) }, 2)
  69. }
  70. componentWillUnmount() {
  71. KeyboardManager.removeKeyDown('execution-options')
  72. }
  73. getFieldValue(field: Field) {
  74. if (!this.props.options || this.props.options[field.name] == null) {
  75. return field.value
  76. }
  77. return this.props.options[field.name]
  78. }
  79. handleValueChange(field: Field, value: string) {
  80. this.setState(prevState => {
  81. const fields = prevState.fields.map(f => {
  82. if (f.name === field.name) {
  83. return { ...f, value }
  84. }
  85. return { ...f }
  86. })
  87. return { fields }
  88. })
  89. if (this.props.onChange) {
  90. this.props.onChange(field.name, value)
  91. }
  92. }
  93. render() {
  94. return (
  95. <Wrapper>
  96. <ExecutionImage />
  97. <Form>
  98. {this.state.fields.map(field => (
  99. <FieldInputStyled
  100. key={field.name}
  101. name={field.name}
  102. type="boolean"
  103. layout="page"
  104. value={this.getFieldValue(field)}
  105. label={LabelDictionary.get(field.name)}
  106. onChange={value => this.handleValueChange(field, value)}
  107. data-test-id={`reOptions-option-${field.name}`}
  108. />
  109. ))}
  110. </Form>
  111. <Buttons>
  112. <Button secondary onClick={this.props.onCancelClick} data-test-id="reOptions-cancelButton">Cancel</Button>
  113. <Button onClick={() => { this.props.onExecuteClick(this.state.fields) }} data-test-id="reOptions-execButton">{this.props.executionLabel}</Button>
  114. </Buttons>
  115. </Wrapper>
  116. )
  117. }
  118. }
  119. export default ReplicaExecutionOptions