ExpandedChartWrapper.tsx 4.2 KB

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