EndpointValidation.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled, { css } from 'styled-components'
  17. import Button from '@src/components/ui/Button'
  18. import CopyButton from '@src/components/ui/CopyButton'
  19. import StatusImage from '@src/components/ui/StatusComponents/StatusImage'
  20. import { ThemePalette } from '@src/components/Theme'
  21. import type { Validation as ValidationType } from '@src/@types/Endpoint'
  22. import notificationStore from '@src/stores/NotificationStore'
  23. import DomUtils from '@src/utils/DomUtils'
  24. const Wrapper = styled.div<any>`
  25. padding: 48px 32px 32px 32px;
  26. `
  27. const contentStyle = css`
  28. display: flex;
  29. flex-direction: column;
  30. align-items: center;
  31. `
  32. const Loading = styled.div<any>`
  33. ${contentStyle}
  34. `
  35. const Validation = styled.div<any>`
  36. ${contentStyle}
  37. `
  38. const Message = styled.div<any>`
  39. max-width: 100%;
  40. overflow: auto;
  41. margin-top: 48px;
  42. text-align: center;
  43. `
  44. const Title = styled.div<any>`
  45. font-size: 18px;
  46. margin-bottom: 8px;
  47. `
  48. const Subtitle = styled.div<any>`
  49. color: ${ThemePalette.grayscale[4]};
  50. `
  51. const Buttons = styled.div<any>`
  52. margin-top: 48px;
  53. display: flex;
  54. justify-content: ${props => (props.center ? 'center' : 'space-between')};
  55. `
  56. const Error = styled.div<any>`
  57. text-align: left;
  58. cursor: pointer;
  59. &:hover > span {
  60. opacity: 1;
  61. }
  62. > span {
  63. background-position-y: 4px;
  64. margin-left: 4px;
  65. }
  66. `
  67. type Props = {
  68. loading: boolean,
  69. validation?: ValidationType | null,
  70. onCancelClick: () => void,
  71. onRetryClick: () => void,
  72. }
  73. @observer
  74. class EndpointValidation extends React.Component<Props> {
  75. handleCopyClick(message: string) {
  76. const succesful = DomUtils.copyTextToClipboard(message)
  77. if (succesful) {
  78. notificationStore.alert('The value has been copied to clipboard.')
  79. } else {
  80. notificationStore.alert('The value couldn\'t be copied', 'error')
  81. }
  82. }
  83. renderLoading() {
  84. if (!this.props.loading) {
  85. return null
  86. }
  87. return (
  88. <Loading>
  89. <StatusImage loading />
  90. <Message>
  91. <Title>Validating Endpoint</Title>
  92. <Subtitle>Please wait ...</Subtitle>
  93. </Message>
  94. </Loading>
  95. )
  96. }
  97. renderSuccessValidationMessage() {
  98. if (!this.props.validation || !this.props.validation.valid || this.props.loading) {
  99. return null
  100. }
  101. return (
  102. <Validation>
  103. <StatusImage status="COMPLETED" />
  104. <Message>
  105. <Title>Endpoint is Valid</Title>
  106. <Subtitle>All tests passed succesfully.</Subtitle>
  107. </Message>
  108. </Validation>
  109. )
  110. }
  111. renderFailedValidationMessage() {
  112. if (!this.props.validation || this.props.validation.valid || this.props.loading) {
  113. return null
  114. }
  115. const message = this.props.validation.message || 'An unexpected error occurred.'
  116. return (
  117. <Validation>
  118. <StatusImage status="ERROR" />
  119. <Message>
  120. <Title>Validation Failed</Title>
  121. <Error onClick={() => { this.handleCopyClick(message) }}>
  122. {message}<CopyButton />
  123. </Error>
  124. </Message>
  125. </Validation>
  126. )
  127. }
  128. renderButtons() {
  129. if (!this.props.loading && this.props.validation && this.props.validation.valid) {
  130. return (
  131. <Buttons center>
  132. <Button secondary onClick={this.props.onCancelClick}>Dismiss</Button>
  133. </Buttons>
  134. )
  135. }
  136. return (
  137. <Buttons>
  138. <Button secondary onClick={this.props.onCancelClick}>Cancel</Button>
  139. <Button disabled={this.props.loading} onClick={this.props.onRetryClick}>Retry</Button>
  140. </Buttons>
  141. )
  142. }
  143. render() {
  144. return (
  145. <Wrapper>
  146. {this.renderLoading()}
  147. {this.renderFailedValidationMessage()}
  148. {this.renderSuccessValidationMessage()}
  149. {this.renderButtons()}
  150. </Wrapper>
  151. )
  152. }
  153. }
  154. export default EndpointValidation