ReplicaExecutionOptions.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // @flow
  15. import React from 'react'
  16. import { observer } from 'mobx-react'
  17. import styled from 'styled-components'
  18. import Button from '../../atoms/Button'
  19. import WizardOptionsField from '../../molecules/WizardOptionsField'
  20. import LabelDictionary from '../../../utils/LabelDictionary'
  21. import KeyboardManager from '../../../utils/KeyboardManager'
  22. import { executionOptions } from '../../../constants'
  23. import type { Field } from '../../../types/Field'
  24. import executionImage from './images/execution.svg'
  25. const Wrapper = styled.div`
  26. display: flex;
  27. flex-direction: column;
  28. align-items: center;
  29. padding: 0 32px 32px 32px;
  30. `
  31. const ExecutionImage = styled.div`
  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`
  39. height: 120px;
  40. `
  41. const Buttons = styled.div`
  42. display: flex;
  43. justify-content: space-between;
  44. width: 100%;
  45. `
  46. const WizardOptionsFieldStyled = styled(WizardOptionsField)`
  47. width: 319px;
  48. justify-content: space-between;
  49. `
  50. type Props = {
  51. options: ?{ [string]: mixed },
  52. onChange: (fieldName: string, fieldValue: string) => void,
  53. executionLabel: string,
  54. onCancelClick: () => void,
  55. onExecuteClick: (options: Field[]) => void,
  56. }
  57. type State = {
  58. fields: Field[],
  59. }
  60. @observer
  61. class ReplicaExecutionOptions extends React.Component<Props, State> {
  62. static defaultProps: $Shape<Props> = {
  63. executionLabel: 'Execute',
  64. }
  65. state = {
  66. fields: [...executionOptions],
  67. }
  68. componentDidMount() {
  69. KeyboardManager.onEnter('execution-options', () => { this.props.onExecuteClick(this.state.fields) }, 2)
  70. }
  71. componentWillUnmount() {
  72. KeyboardManager.removeKeyDown('execution-options')
  73. }
  74. getFieldValue(field: Field) {
  75. if (!this.props.options || this.props.options[field.name] == null) {
  76. return field.value
  77. }
  78. return this.props.options[field.name]
  79. }
  80. handleValueChange(field: Field, value: string) {
  81. let fields = this.state.fields.map(f => {
  82. if (f.name === field.name) {
  83. return { ...f, value }
  84. }
  85. return { ...f }
  86. })
  87. this.setState({ fields })
  88. if (this.props.onChange) {
  89. this.props.onChange(field.name, value)
  90. }
  91. }
  92. render() {
  93. return (
  94. <Wrapper>
  95. <ExecutionImage />
  96. <Form>
  97. {this.state.fields.map(field => {
  98. return (
  99. <WizardOptionsFieldStyled
  100. key={field.name}
  101. name={field.name}
  102. type="strict-boolean"
  103. value={this.getFieldValue(field)}
  104. label={LabelDictionary.get(field.name)}
  105. onChange={value => this.handleValueChange(field, value)}
  106. data-test-id={`reOptions-option-${field.name}`}
  107. />
  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