ExpandedTemplate.tsx 3.3 KB

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