ExpandedTemplate.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { Component } from "react";
  2. import styled from "styled-components";
  3. import { PorterTemplate } from "shared/types";
  4. import api from "shared/api";
  5. import TemplateInfo from "./TemplateInfo";
  6. import Loading from "components/Loading";
  7. import { Context } from "shared/Context";
  8. type PropsType = {
  9. currentTemplate: PorterTemplate;
  10. currentTab: string;
  11. setCurrentTemplate: (x: PorterTemplate) => void;
  12. skipDescription?: boolean;
  13. showLaunchFlow: () => void;
  14. setForm: (x: any) => void;
  15. helm_repo_id?: number;
  16. repo_url?: string;
  17. };
  18. type StateType = {
  19. showLaunchTemplate: boolean;
  20. form: any | null;
  21. values: any | null;
  22. loading: boolean;
  23. error: boolean;
  24. markdown: string | null;
  25. keywords: string[];
  26. };
  27. export default class ExpandedTemplate extends Component<PropsType, StateType> {
  28. state = {
  29. showLaunchTemplate: false,
  30. form: null as any | null,
  31. values: null as any | null,
  32. loading: true,
  33. error: false,
  34. markdown: null as string | null,
  35. keywords: [] as string[],
  36. };
  37. componentDidMount() {
  38. this.fetchTemplateInfo();
  39. }
  40. fetchTemplateInfo = () => {
  41. this.setState({ loading: true });
  42. if (this.props.helm_repo_id) {
  43. api
  44. .getChartInfoFromHelmRepo(
  45. "<token>",
  46. {},
  47. {
  48. project_id: this.context.currentProject.id,
  49. helm_repo_id: this.props.helm_repo_id,
  50. name: this.props.currentTemplate.name.toLowerCase().trim(),
  51. version: this.props.currentTemplate.currentVersion,
  52. }
  53. )
  54. .then((res) => {
  55. let { form, values, markdown, metadata } = res.data;
  56. let keywords = metadata.keywords;
  57. this.props.setForm(form);
  58. this.setState({
  59. values,
  60. markdown,
  61. keywords,
  62. loading: false,
  63. error: false,
  64. });
  65. })
  66. .catch((err) => this.setState({ loading: false, error: true }));
  67. } else {
  68. let params =
  69. this.props.currentTab == "porter"
  70. ? { repo_url: process.env.APPLICATION_CHART_REPO_URL }
  71. : { repo_url: process.env.ADDON_CHART_REPO_URL };
  72. api
  73. .getTemplateInfo("<token>", params, {
  74. name: this.props.currentTemplate.name.toLowerCase().trim(),
  75. version: this.props.currentTemplate.currentVersion,
  76. })
  77. .then((res) => {
  78. let { form, values, markdown, metadata } = res.data;
  79. let keywords = metadata.keywords;
  80. this.props.setForm(form);
  81. this.setState({
  82. values,
  83. markdown,
  84. keywords,
  85. loading: false,
  86. error: false,
  87. });
  88. })
  89. .catch((err) => this.setState({ loading: false, error: true }));
  90. }
  91. };
  92. componentDidUpdate = (prevProps: PropsType) => {
  93. if (
  94. prevProps.currentTemplate.name !== this.props.currentTemplate.name ||
  95. prevProps.currentTemplate.currentVersion !==
  96. this.props.currentTemplate.currentVersion
  97. ) {
  98. this.fetchTemplateInfo();
  99. }
  100. };
  101. renderContents = () => {
  102. if (this.state.loading) {
  103. return (
  104. <LoadingWrapper>
  105. <Loading />
  106. </LoadingWrapper>
  107. );
  108. }
  109. return (
  110. <FadeWrapper>
  111. <TemplateInfo
  112. currentTab={this.props.currentTab}
  113. currentTemplate={this.props.currentTemplate}
  114. setCurrentTemplate={this.props.setCurrentTemplate}
  115. setCurrentVersion={(version) => {
  116. let template = {
  117. ...this.props.currentTemplate,
  118. currentVersion: version,
  119. };
  120. this.props.setCurrentTemplate(template);
  121. }}
  122. launchTemplate={this.props.showLaunchFlow}
  123. markdown={this.state.markdown}
  124. keywords={this.state.keywords}
  125. />
  126. </FadeWrapper>
  127. );
  128. };
  129. render() {
  130. return (
  131. <StyledExpandedTemplate>{this.renderContents()}</StyledExpandedTemplate>
  132. );
  133. }
  134. }
  135. ExpandedTemplate.contextType = Context;
  136. const FadeWrapper = styled.div`
  137. animation: fadeIn 0.2s;
  138. @keyframes fadeIn {
  139. from {
  140. opacity: 0;
  141. }
  142. to {
  143. opacity: 1;
  144. }
  145. }
  146. `;
  147. const LoadingWrapper = styled.div`
  148. height: calc(100vh - 200px);
  149. width: 100%;
  150. `;
  151. const StyledExpandedTemplate = styled.div`
  152. width: 100%;
  153. min-width: 300px;
  154. padding-top: 30px;
  155. `;