ExpandedChartWrapper.tsx 3.5 KB

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