ExpandableResource.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { Component, useContext, useEffect } from "react";
  2. import styled from "styled-components";
  3. import { Context } from "shared/Context";
  4. import ResourceTab from "./ResourceTab";
  5. import SaveButton from "./SaveButton";
  6. import { baseApi } from "shared/baseApi";
  7. type Props = {
  8. resource: any;
  9. button: any;
  10. handleClick?: () => void;
  11. selected?: boolean;
  12. isLast?: boolean;
  13. roundAllCorners?: boolean;
  14. };
  15. const ExpandableResource: React.FC<Props> = (props) => {
  16. const { resource, button } = props;
  17. const { currentCluster, currentProject } = useContext(Context);
  18. const onSave = () => {
  19. let projID = currentProject.id;
  20. let clusterID = currentCluster.id;
  21. let config = button.actions[0].delete.context.config;
  22. // TODO: construct the endpoint scope, right now we're just using release scope
  23. let uri = `/api/projects/${projID}/clusters/${clusterID}/namespaces/${resource.metadata.namespace}${button.actions[0].delete.relative_uri}`;
  24. // compute the endpoint using button and target context
  25. baseApi<
  26. {
  27. name: string;
  28. namespace: string;
  29. group: string;
  30. version: string;
  31. resource: string;
  32. },
  33. {}
  34. >("DELETE", uri)(
  35. "<token>",
  36. {
  37. name: resource.metadata.name,
  38. namespace: resource.metadata.namespace,
  39. group: config.group,
  40. version: config.version,
  41. resource: config.resource,
  42. },
  43. {}
  44. )
  45. .then((res) => {})
  46. .catch((err) => console.log(err));
  47. };
  48. const readableDate = (s: string) => {
  49. const ts = new Date(s);
  50. const date = ts.toLocaleDateString();
  51. const time = ts.toLocaleTimeString([], {
  52. hour: "numeric",
  53. minute: "2-digit",
  54. });
  55. return `${time} on ${date}`;
  56. };
  57. return (
  58. <ResourceTab
  59. label={resource.label}
  60. name={resource.name}
  61. status={{ label: resource.status }}
  62. >
  63. <ExpandedWrapper>
  64. <StatusSection>
  65. <StatusHeader>
  66. <Status>
  67. <Key>Status:</Key> {resource.status}
  68. </Status>
  69. <Timestamp>Updated {readableDate(resource.timestamp)}</Timestamp>
  70. </StatusHeader>
  71. {resource.message}
  72. </StatusSection>
  73. {Object.keys(resource.data).map((key: string, i: number) => {
  74. return (
  75. <Pair key={i}>
  76. <Key>{key}:</Key>
  77. {resource.data[key]}
  78. </Pair>
  79. );
  80. })}
  81. <StyledSaveButton
  82. onClick={onSave}
  83. clearPosition={true}
  84. text={button.name}
  85. helper={button.description}
  86. statusPosition={"right"}
  87. className="expanded-save-button"
  88. />
  89. </ExpandedWrapper>
  90. </ResourceTab>
  91. );
  92. };
  93. export default ExpandableResource;
  94. const Timestamp = styled.div`
  95. font-size: 12px;
  96. color: #ffffff44;
  97. `;
  98. const StatusHeader = styled.div`
  99. display: flex;
  100. align-items: center;
  101. justify-content: space-between;
  102. margin-bottom: 20px;
  103. `;
  104. const Status = styled.div`
  105. display: flex;
  106. align-items: center;
  107. color: #aaaabb;
  108. `;
  109. const StatusSection = styled.div`
  110. border-radius: 8px;
  111. background: #ffffff11;
  112. font-size: 13px;
  113. padding: 20px 20px 25px;
  114. `;
  115. const ExpandedWrapper = styled.div`
  116. padding: 20px 20px 25px;
  117. `;
  118. const Pair = styled.div`
  119. margin-top: 20px;
  120. font-size: 13px;
  121. padding: 0 5px;
  122. color: #aaaabb;
  123. display: flex;
  124. align-items: center;
  125. `;
  126. const Key = styled.div`
  127. font-weight: bold;
  128. color: #ffffff;
  129. margin-right: 8px;
  130. `;
  131. const StyledSaveButton = styled(SaveButton)`
  132. margin-top: 20px;
  133. `;