2
0

ExpandedChartWrapper.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React, { Component } from "react";
  2. import { withRouter, type RouteComponentProps } from "react-router";
  3. import styled from "styled-components";
  4. import Loading from "components/Loading";
  5. import PageNotFound from "components/PageNotFound";
  6. import api from "shared/api";
  7. import { Context } from "shared/Context";
  8. import { pushFiltered } from "shared/routing";
  9. import {
  10. type ChartType,
  11. type ChartTypeWithExtendedConfig,
  12. } from "shared/types";
  13. import ExpandedChart from "./ExpandedChart";
  14. import ExpandedJobChart from "./ExpandedJobChart";
  15. type PropsType = RouteComponentProps<{
  16. baseRoute: string;
  17. namespace: string;
  18. }> & {
  19. setSidebar: (x: boolean) => void;
  20. isMetricsInstalled: boolean;
  21. };
  22. type StateType = {
  23. loading: boolean;
  24. currentChart: ChartType;
  25. };
  26. class ExpandedChartWrapper extends Component<PropsType, StateType> {
  27. state = {
  28. loading: true,
  29. currentChart: null as ChartType,
  30. };
  31. // Retrieve full chart data (includes form and values)
  32. getChartData = () => {
  33. const { match } = this.props;
  34. const { namespace, chartName } = match.params as any;
  35. const { currentProject, currentCluster } = this.context;
  36. if (currentProject && currentCluster) {
  37. api
  38. .getChart<ChartTypeWithExtendedConfig>(
  39. "<token>",
  40. {},
  41. {
  42. id: currentProject.id,
  43. namespace,
  44. cluster_id: currentCluster.id,
  45. name: chartName,
  46. revision: 0,
  47. }
  48. )
  49. .then((res) => {
  50. const chart = res.data;
  51. this.setState({ currentChart: res.data, loading: false });
  52. const isJob = res.data.form?.name?.toLowerCase() === "job";
  53. const route = `${isJob ? "/jobs" : "/applications"}/${
  54. currentCluster.name
  55. }/${chart.namespace}/${chart.name}`;
  56. if (isJob && this.props.match.params?.baseRoute === "applications") {
  57. pushFiltered(this.props, route, [
  58. "project_id",
  59. "closeChartRedirectUrl",
  60. ]);
  61. return;
  62. }
  63. if (!isJob && this.props.match.params?.baseRoute !== "applications") {
  64. pushFiltered(this.props, route, [
  65. "project_id",
  66. "closeChartRedirectUrl",
  67. ]);
  68. }
  69. })
  70. .catch((err) => {
  71. console.log(err);
  72. console.log("err", err?.response?.data);
  73. this.setState({ loading: false });
  74. });
  75. }
  76. };
  77. componentDidMount() {
  78. this.setState({ loading: true });
  79. this.getChartData();
  80. }
  81. render() {
  82. const { setSidebar, location, match } = this.props;
  83. const { baseRoute, namespace } = match.params as any;
  84. const { loading, currentChart } = this.state;
  85. if (loading) {
  86. return (
  87. <LoadingWrapper>
  88. <Loading />
  89. </LoadingWrapper>
  90. );
  91. } else if (currentChart && baseRoute === "jobs") {
  92. return (
  93. <ExpandedJobChart
  94. namespace={namespace}
  95. currentChart={currentChart}
  96. currentCluster={this.context.currentCluster}
  97. closeChart={() => {
  98. const urlParams = new URLSearchParams(window.location.search);
  99. if (urlParams.get("closeChartRedirectUrl")) {
  100. this.props.history.push(urlParams.get("closeChartRedirectUrl"));
  101. return;
  102. }
  103. pushFiltered(this.props, "/jobs", ["project_id"], {
  104. cluster: this.context.currentCluster.name,
  105. namespace,
  106. });
  107. }}
  108. setSidebar={setSidebar}
  109. />
  110. );
  111. } else if (currentChart && baseRoute === "applications") {
  112. return (
  113. <ExpandedChart
  114. namespace={namespace}
  115. isMetricsInstalled={this.props.isMetricsInstalled}
  116. currentChart={currentChart}
  117. currentCluster={this.context.currentCluster}
  118. closeChart={() => {
  119. const urlParams = new URLSearchParams(window.location.search);
  120. if (urlParams.get("closeChartRedirectUrl")) {
  121. this.props.history.push(urlParams.get("closeChartRedirectUrl"));
  122. return;
  123. }
  124. pushFiltered(this.props, "/applications", ["project_id"], {
  125. cluster: this.context.currentCluster.name,
  126. namespace,
  127. });
  128. }}
  129. setSidebar={setSidebar}
  130. />
  131. );
  132. }
  133. return <PageNotFound />;
  134. }
  135. }
  136. ExpandedChartWrapper.contextType = Context;
  137. export default withRouter(ExpandedChartWrapper);
  138. const LoadingWrapper = styled.div`
  139. width: 100%;
  140. height: 100vh;
  141. `;