ContentPlugin.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 styled from 'styled-components'
  16. import PropTypes from 'prop-types'
  17. import { EndpointField, Button, LoadingButton } from '../../../components'
  18. const Wrapper = styled.div`
  19. display: flex;
  20. flex-direction: column;
  21. min-height: 0;
  22. `
  23. const Fields = styled.div`
  24. display: flex;
  25. margin-top: 32px;
  26. flex-direction: column;
  27. overflow: auto;
  28. `
  29. const FieldStyled = styled(EndpointField)`
  30. min-width: 224px;
  31. max-width: 224px;
  32. margin-bottom: 16px;
  33. `
  34. const Buttons = styled.div`
  35. display: flex;
  36. justify-content: space-between;
  37. width: 100%;
  38. margin-top: 32px;
  39. flex-shrink: 0;
  40. `
  41. const Row = styled.div`
  42. display: flex;
  43. flex-shrink: 0;
  44. justify-content: space-between;
  45. `
  46. class ContentPlugin extends React.Component {
  47. static propTypes = {
  48. connectionInfoSchema: PropTypes.array,
  49. validation: PropTypes.object,
  50. invalidFields: PropTypes.array,
  51. getFieldValue: PropTypes.func,
  52. handleFieldChange: PropTypes.func,
  53. disabled: PropTypes.bool,
  54. cancelButtonText: PropTypes.string,
  55. validating: PropTypes.bool,
  56. handleValidateClick: PropTypes.func,
  57. handleCancelClick: PropTypes.func,
  58. onRef: PropTypes.func,
  59. }
  60. componentDidMount() {
  61. this.props.onRef(this)
  62. }
  63. componentWillUnmount() {
  64. this.props.onRef(undefined)
  65. }
  66. findInvalidFields = () => {
  67. const invalidFields = this.props.connectionInfoSchema.filter(field => {
  68. if (field.required) {
  69. let value = this.props.getFieldValue(field)
  70. return !value
  71. }
  72. return false
  73. }).map(f => f.name)
  74. return invalidFields
  75. }
  76. renderFields() {
  77. const rows = []
  78. let lastField
  79. this.props.connectionInfoSchema.forEach((field, i) => {
  80. const currentField = (
  81. <FieldStyled
  82. {...field}
  83. large
  84. disabled={this.props.disabled}
  85. password={field.name === 'password'}
  86. highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
  87. value={this.props.getFieldValue(field)}
  88. onChange={value => { this.props.handleFieldChange(field, value) }}
  89. />
  90. )
  91. if (i % 2 !== 0) {
  92. rows.push((
  93. <Row key={field.name}>
  94. {lastField}
  95. {currentField}
  96. </Row>
  97. ))
  98. }
  99. lastField = currentField
  100. })
  101. return (
  102. <Fields>
  103. {rows}
  104. </Fields>
  105. )
  106. }
  107. renderButtons() {
  108. let actionButton = <Button large onClick={() => this.props.handleValidateClick()}>Validate and save</Button>
  109. let message = 'Validating Endpoint ...'
  110. if (this.props.validating || (this.props.validation && this.props.validation.valid)) {
  111. if (this.props.validation && this.props.validation.valid) {
  112. message = 'Saving ...'
  113. }
  114. actionButton = <LoadingButton large>{message}</LoadingButton>
  115. }
  116. return (
  117. <Buttons>
  118. <Button large secondary onClick={() => { this.props.handleCancelClick() }}>{this.props.cancelButtonText}</Button>
  119. {actionButton}
  120. </Buttons>
  121. )
  122. }
  123. render() {
  124. return (
  125. <Wrapper>
  126. {this.renderFields()}
  127. {this.renderButtons()}
  128. </Wrapper>
  129. )
  130. }
  131. }
  132. export default ContentPlugin