ValuesForm.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import React, { Component } from 'react';
  2. import styled from 'styled-components';
  3. import { Section, FormElement } from '../../shared/types';
  4. import { Context } from '../../shared/Context';
  5. import api from '../../shared/api';
  6. import SaveButton from '../SaveButton';
  7. import CheckboxRow from './CheckboxRow';
  8. import InputRow from './InputRow';
  9. import SelectRow from './SelectRow';
  10. type PropsType = {
  11. sections?: Section[]
  12. };
  13. type StateType = any;
  14. export default class ValuesForm extends Component<PropsType, StateType> {
  15. updateFormState() {
  16. let formState: any = {};
  17. this.props.sections.forEach((section: Section, i: number) => {
  18. section.Contents.forEach((item: FormElement, i: number) => {
  19. // If no name is assigned use values.yaml variable as identifier
  20. let key = item.Name || item.Variable;
  21. let def = item.Settings.Default;
  22. switch (item.Type) {
  23. case 'checkbox':
  24. formState[key] = def ? def : false;
  25. break;
  26. case 'string-input':
  27. formState[key] = def ? def : '';
  28. break;
  29. case 'number-input':
  30. formState[key] = def.toString() ? def.toString() : '';
  31. break;
  32. case 'select':
  33. formState[key] = def ? def : item.Settings.Options[0].Value;
  34. default:
  35. }
  36. });
  37. });
  38. this.setState(formState);
  39. }
  40. // Initialize corresponding state fields for form blocks
  41. componentDidMount() {
  42. this.updateFormState();
  43. }
  44. componentDidUpdate(prevProps: PropsType) {
  45. if (this.props.sections !== prevProps.sections) {
  46. this.updateFormState();
  47. }
  48. }
  49. handleDeploy = () => {
  50. console.log(this.state);
  51. let { currentProject } = this.context;
  52. api.deployTemplate('<token>', {}, {
  53. id: currentProject.id,
  54. }, (err: any, res: any) => {
  55. if (err) {
  56. // console.log(err)
  57. } else {
  58. // console.log(res.data)
  59. }
  60. });
  61. }
  62. renderSection = (section: Section) => {
  63. return section.Contents.map((item: FormElement, i: number) => {
  64. // If no name is assigned use values.yaml variable as identifier
  65. let key = item.Name || item.Variable;
  66. switch (item.Type) {
  67. case 'heading':
  68. return <Heading key={i}>{item.Label}</Heading>
  69. case 'subtitle':
  70. return <Helper key={i}>{item.Label}</Helper>
  71. case 'checkbox':
  72. return (
  73. <CheckboxRow
  74. key={i}
  75. checked={this.state[key]}
  76. toggle={() => this.setState({ [key]: !this.state[key] })}
  77. label={item.Label}
  78. />
  79. );
  80. case 'string-input':
  81. return (
  82. <InputRow
  83. key={i}
  84. type='text'
  85. value={this.state[key]}
  86. setValue={(x: string) => this.setState({ [key]: x })}
  87. label={item.Label}
  88. unit={item.Settings ? item.Settings.Unit : null}
  89. />
  90. );
  91. case 'number-input':
  92. return (
  93. <InputRow
  94. key={i}
  95. type='number'
  96. value={this.state[key]}
  97. setValue={(x: string) => this.setState({ [key]: x })}
  98. label={item.Label}
  99. unit={item.Settings ? item.Settings.Unit : null}
  100. />
  101. );
  102. case 'select':
  103. return (
  104. <SelectRow
  105. key={i}
  106. value={this.state[key]}
  107. setActiveValue={(val) => this.setState({ [key]: val })}
  108. options={item.Settings.Options}
  109. dropdownLabel=''
  110. label={item.Label}
  111. />
  112. );
  113. default:
  114. }
  115. });
  116. }
  117. renderFormContents = () => {
  118. if (this.state) {
  119. return this.props.sections.map((section: Section, i: number) => {
  120. // Hide collapsible section if deciding field is false
  121. if (section.ShowIf) {
  122. if (!this.state[section.ShowIf]) {
  123. return null;
  124. }
  125. }
  126. return (
  127. <div key={i}>
  128. {this.renderSection(section)}
  129. </div>
  130. );
  131. });
  132. }
  133. }
  134. render() {
  135. return (
  136. <Wrapper>
  137. <StyledValuesForm>
  138. <DarkMatter />
  139. {this.renderFormContents()}
  140. </StyledValuesForm>
  141. <SaveButton
  142. text='Deploy'
  143. onClick={this.handleDeploy}
  144. status={null}
  145. makeFlush={true}
  146. />
  147. </Wrapper>
  148. );
  149. }
  150. }
  151. ValuesForm.contextType = Context;
  152. const DarkMatter = styled.div`
  153. margin-top: 0px;
  154. `;
  155. const Wrapper = styled.div`
  156. width: 100%;
  157. height: 100%;
  158. `;
  159. const Helper = styled.div`
  160. color: #aaaabb;
  161. line-height: 1.6em;
  162. font-size: 13px;
  163. margin-bottom: 15px;
  164. margin-top: 20px;
  165. `;
  166. const Heading = styled.div`
  167. color: white;
  168. font-weight: 500;
  169. font-size: 16px;
  170. margin-top: 30px;
  171. margin-bottom: 5px;
  172. `;
  173. const StyledValuesForm = styled.div`
  174. width: 100%;
  175. height: 100%;
  176. background: #ffffff11;
  177. color: #ffffff;
  178. padding: 0px 35px 25px;
  179. position: relative;
  180. border-radius: 5px;
  181. font-size: 13px;
  182. overflow: auto;
  183. `;