MinionPoolModalContent.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Copyright (C) 2020 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 * as React from 'react'
  15. import styled from 'styled-components'
  16. import LabelDictionary from '@src/utils/LabelDictionary'
  17. import FieldInput from '@src/components/ui/FieldInput'
  18. import type { Field } from '@src/@types/Field'
  19. import { ThemePalette, ThemeProps } from '@src/components/Theme'
  20. import EndpointLogos from '@src/components/modules/EndpointModule/EndpointLogos'
  21. import { Endpoint } from '@src/@types/Endpoint'
  22. import ToggleButtonBar from '@src/components/ui/ToggleButtonBar'
  23. const Wrapper = styled.div<any>`
  24. display: flex;
  25. flex-direction: column;
  26. min-height: 0;
  27. `
  28. const Fields = styled.div<any>`
  29. display: flex;
  30. margin-top: 32px;
  31. padding: 0 32px;
  32. flex-direction: column;
  33. overflow: auto;
  34. `
  35. const ToggleButtonBarStyled = styled(ToggleButtonBar)`
  36. margin-top: 16px;
  37. `
  38. const FieldStyled = styled(FieldInput)`
  39. min-width: ${props => (props.useTextArea ? '100%' : '224px')};
  40. max-width: ${ThemeProps.inputSizes.large.width}px;
  41. margin-bottom: 16px;
  42. `
  43. const Row = styled.div<any>`
  44. display: flex;
  45. flex-shrink: 0;
  46. justify-content: space-between;
  47. `
  48. const EndpointField = styled.div`
  49. min-width: ${ThemeProps.inputSizes.large.width}px;
  50. max-width: ${ThemeProps.inputSizes.large.width}px;
  51. margin-bottom: 16px;
  52. `
  53. const EndpointFieldLabel = styled.div<any>`
  54. font-weight: ${ThemeProps.fontWeights.medium};
  55. flex-grow: 1;
  56. margin-bottom: 2px;
  57. font-size: 10px;
  58. color: ${ThemePalette.grayscale[3]};
  59. text-transform: uppercase;
  60. display: flex;
  61. align-items: center;
  62. `
  63. const EndpointFieldLabelText = styled.span`
  64. margin-right: 24px;
  65. `
  66. const EndpointFieldValue = styled.div`
  67. display: flex;
  68. align-items: center;
  69. height: 29px;
  70. `
  71. const EndpointFieldValueText = styled.div`
  72. overflow: hidden;
  73. text-overflow: ellipsis;
  74. margin-left: 8px;
  75. font-size: 12px;
  76. color: ${ThemePalette.grayscale[4]};
  77. `
  78. const EndpointFieldValueLabel = styled.div`
  79. text-transform: capitalize;
  80. `
  81. const EndpointFieldValueLogo = styled.div``
  82. const Group = styled.div`
  83. display: flex;
  84. flex-direction: column;
  85. flex-shrink: 0;
  86. `
  87. const GroupName = styled.div<any>`
  88. display: flex;
  89. align-items: center;
  90. margin: 32px 0 24px 0;
  91. `
  92. const DisabledMessage = styled.div`
  93. display: flex;
  94. align-items: center;
  95. width: 340px;
  96. margin: 0 auto 32px auto;
  97. text-align: center;
  98. font-size: 13px;
  99. `
  100. const GroupNameText = styled.div<any>`
  101. margin: 0 32px;
  102. font-size: 16px;
  103. `
  104. const GroupNameBar = styled.div<any>`
  105. flex-grow: 1;
  106. background: ${ThemePalette.grayscale[3]};
  107. height: 1px;
  108. `
  109. const GroupFields = styled.div<any>`
  110. display: flex;
  111. justify-content: space-between;
  112. flex-direction: column;
  113. `
  114. type Props = {
  115. envOptionsDisabled: boolean
  116. defaultSchema: Field[],
  117. envSchema: Field[],
  118. invalidFields: string[],
  119. endpoint: Endpoint
  120. platform: 'source' | 'destination'
  121. optionsLoading: boolean
  122. optionsLoadingSkipFields: string[],
  123. getFieldValue: (field: Field | null | undefined) => any,
  124. onFieldChange: (field: Field | null, value: any) => void,
  125. disabled: boolean,
  126. cancelButtonText: string,
  127. onResizeUpdate: (scrollOffset: number) => void,
  128. scrollableRef: (ref: HTMLElement) => void,
  129. onCreateClick: () => void
  130. onCancelClick: () => void
  131. }
  132. type State = {
  133. useAdvancedOptions: boolean
  134. }
  135. class MinionPoolModalContent extends React.Component<Props, State> {
  136. state = {
  137. useAdvancedOptions: false,
  138. }
  139. componentDidUpdate(_: Props, prevState: State) {
  140. if (prevState.useAdvancedOptions !== this.state.useAdvancedOptions) {
  141. this.props.onResizeUpdate(0)
  142. }
  143. }
  144. filterBySimpleAdvanced(fields: Field[]): Field[] {
  145. if (this.state.useAdvancedOptions) {
  146. return fields
  147. }
  148. const exceptions = ['endpoint_id', 'platform', 'os_type']
  149. return fields.filter(f => (f.required && f.default == null) || exceptions.indexOf(f.name) > -1)
  150. }
  151. renderEndpoint() {
  152. return (
  153. <EndpointField>
  154. <EndpointFieldLabel>
  155. <EndpointFieldLabelText>
  156. Endpoint
  157. </EndpointFieldLabelText>
  158. </EndpointFieldLabel>
  159. <EndpointFieldValue>
  160. <EndpointFieldValueLogo>
  161. <EndpointLogos
  162. endpoint={this.props.endpoint.type}
  163. height={32}
  164. />
  165. </EndpointFieldValueLogo>
  166. <EndpointFieldValueText title={this.props.endpoint.name}>
  167. ({this.props.endpoint.name})
  168. </EndpointFieldValueText>
  169. </EndpointFieldValue>
  170. </EndpointField>
  171. )
  172. }
  173. renderReadOnlyField(field: Field) {
  174. return (
  175. <EndpointField>
  176. <EndpointFieldLabel>
  177. <EndpointFieldLabelText>
  178. {field.title}
  179. </EndpointFieldLabelText>
  180. </EndpointFieldLabel>
  181. <EndpointFieldValue>
  182. <EndpointFieldValueLabel>
  183. {this.props.getFieldValue(field)}
  184. </EndpointFieldValueLabel>
  185. </EndpointFieldValue>
  186. </EndpointField>
  187. )
  188. }
  189. renderFieldSet(customFields: Field[], options?: { disabled?: boolean }) {
  190. const rows: JSX.Element[] = []
  191. let lastField: JSX.Element
  192. let i = 0
  193. customFields.forEach((field, schemaIndex) => {
  194. let currentField
  195. if (field.name === 'endpoint_id') {
  196. currentField = this.renderEndpoint()
  197. } else if (field.name === 'platform' || (field.name === 'os_type' && this.props.platform === 'source')) {
  198. currentField = this.renderReadOnlyField(field)
  199. } else {
  200. currentField = (
  201. <FieldStyled
  202. {...field}
  203. label={field.title || LabelDictionary.get(field.name)}
  204. width={ThemeProps.inputSizes.large.width}
  205. disabled={this.props.disabled || options?.disabled}
  206. highlight={this.props.invalidFields.findIndex(fn => fn === field.name) > -1}
  207. value={this.props.getFieldValue(field)}
  208. onChange={value => { this.props.onFieldChange(field, value) }}
  209. nullableBoolean={field.nullableBoolean != null ? field.nullableBoolean : true}
  210. disabledLoading={this.props.optionsLoading && !this.props.optionsLoadingSkipFields.find(fn => fn === field.name)}
  211. />
  212. )
  213. }
  214. const pushRow = (field1: React.ReactNode, field2?: React.ReactNode) => {
  215. rows.push((
  216. <Row key={field.name}>
  217. {field1}
  218. {field2}
  219. </Row>
  220. ))
  221. }
  222. if (field.useTextArea) {
  223. pushRow(currentField)
  224. i -= 1
  225. } else if (i % 2 !== 0) {
  226. pushRow(lastField, currentField)
  227. } else if (schemaIndex === customFields.length - 1) {
  228. pushRow(currentField)
  229. if (field.useTextArea) {
  230. i -= 1
  231. }
  232. } else {
  233. lastField = currentField
  234. }
  235. i += 1
  236. })
  237. return rows
  238. }
  239. renderFields() {
  240. return (
  241. <Fields ref={(ref: HTMLElement) => { this.props.scrollableRef(ref) }}>
  242. <Group>
  243. <GroupFields>
  244. {this.renderFieldSet(this.filterBySimpleAdvanced(this.props.defaultSchema))}
  245. </GroupFields>
  246. </Group>
  247. <Group>
  248. <GroupName>
  249. <GroupNameBar />
  250. <GroupNameText>Environment Options</GroupNameText>
  251. <GroupNameBar />
  252. </GroupName>
  253. {this.props.envOptionsDisabled ? (
  254. <DisabledMessage>
  255. The environment options are disabled while the minion pool is not deallocated.
  256. </DisabledMessage>
  257. ) : null}
  258. <GroupFields>
  259. {this.renderFieldSet(
  260. this.filterBySimpleAdvanced(this.props.envSchema),
  261. { disabled: this.props.envOptionsDisabled },
  262. )}
  263. </GroupFields>
  264. </Group>
  265. </Fields>
  266. )
  267. }
  268. renderSimpleAdvancedToggle() {
  269. return (
  270. <ToggleButtonBarStyled
  271. items={[{ label: 'Simple', value: 'simple' }, { label: 'Advanced', value: 'advanced' }]}
  272. selectedValue={this.state.useAdvancedOptions ? 'advanced' : 'simple'}
  273. onChange={item => { this.setState({ useAdvancedOptions: item.value === 'advanced' }) }}
  274. />
  275. )
  276. }
  277. render() {
  278. return (
  279. <Wrapper>
  280. {this.renderSimpleAdvancedToggle()}
  281. {this.renderFields()}
  282. </Wrapper>
  283. )
  284. }
  285. }
  286. export default MinionPoolModalContent