CloudConnectionDetail.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, { Component, PropTypes } from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import s from './CloudConnectionDetail.scss';
  17. import Moment from 'react-moment';
  18. import LoadingIcon from '../LoadingIcon';
  19. import Helper from '../Helper';
  20. const title = 'connection details';
  21. class CloudConnectionDetail extends Component {
  22. static propTypes = {
  23. connection: PropTypes.object
  24. }
  25. static contextTypes = {
  26. onSetTitle: PropTypes.func.isRequired,
  27. };
  28. constructor(props) {
  29. super(props)
  30. this.state = {
  31. fields: this.processProps(props),
  32. showPassword: false
  33. }
  34. }
  35. componentWillMount() {
  36. this.context.onSetTitle(title);
  37. }
  38. componentWillReceiveProps(newProps) {
  39. this.setState({ fields: this.processProps(newProps) })
  40. }
  41. processProps(props) {
  42. let fields = []
  43. let addField = (name, value) => {
  44. name = Helper.convertCloudFieldLabel(name)
  45. if (!fields.find(f => f.fieldName === name)) {
  46. fields.push({
  47. fieldName: name,
  48. fieldValue: value
  49. })
  50. }
  51. }
  52. if (props.connection.credentials) {
  53. for (let fieldName in props.connection.credentials) {
  54. let value = props.connection.credentials[fieldName]
  55. if (value.value) { // if dropdown
  56. value = value.value
  57. }
  58. if (value === true) value = "Yes"
  59. if (value === false) value = "No"
  60. if (typeof value === 'object') {
  61. for (let extraField in value) {
  62. addField(extraField, value[extraField])
  63. }
  64. } else {
  65. addField(fieldName, value)
  66. }
  67. }
  68. }
  69. // Sort username and password to the front
  70. let sortExceptions = { Username: 1, Password: 2 };
  71. fields.sort((a, b) => {
  72. if (sortExceptions[a.fieldName] && sortExceptions[b.fieldName]) {
  73. return sortExceptions[a.fieldName] - sortExceptions[b.fieldName];
  74. } else if (sortExceptions[a.fieldName]) {
  75. return -1;
  76. } else if (sortExceptions[b.fieldName]) {
  77. return 1;
  78. } else {
  79. return 0;
  80. }
  81. })
  82. return fields
  83. }
  84. handleShowPassword() {
  85. this.setState({ showPassword: true })
  86. }
  87. renderAuthFields() {
  88. let renderPasswordField = field => {
  89. if (field.fieldName !== 'Password' || this.state.showPassword) {
  90. return (
  91. <div className={s.value}>
  92. {field.fieldValue || '-'}
  93. </div>
  94. )
  95. }
  96. return (
  97. <div className={s.value + ' ' + s.passwordValue} onClick={this.handleShowPassword.bind(this)}>
  98. •••••••••<span className={s.eyeIcon}></span>
  99. </div>
  100. )
  101. }
  102. if (this.state.fields.length) {
  103. return this.state.fields.map((field, index) => {
  104. if (field.fieldName !== 'login_type') {
  105. return (
  106. <div className={s.formGroup} key={"formGroup_" + index}>
  107. <div className={s.title}>
  108. {field.fieldName}
  109. </div>
  110. {renderPasswordField(field)}
  111. </div>
  112. )
  113. } else {
  114. return null
  115. }
  116. })
  117. } else {
  118. return <LoadingIcon />
  119. }
  120. }
  121. render() {
  122. let item = this.props.connection
  123. let createdAt = Helper.getTimeObject(item.created_at)
  124. if (item) {
  125. return (
  126. <div className={s.root}>
  127. <div className={s.container}>
  128. <div className={s.formGroup}>
  129. <div className={s.title}>
  130. Name
  131. </div>
  132. <div className={s.value}>
  133. {item.name}
  134. </div>
  135. </div>
  136. <div className={s.formGroup}>
  137. <div className={s.title}>
  138. Type
  139. </div>
  140. <div className={s.value}>
  141. {Helper.convertCloudLabel(item.type)}
  142. </div>
  143. </div>
  144. <div className={s.formGroup}>
  145. <div className={s.title}>
  146. Description
  147. </div>
  148. <div className={s.value}>
  149. {item.description == "" ? "-" : item.description}
  150. </div>
  151. </div>
  152. <div className={s.formGroup}>
  153. <div className={s.title}>
  154. Created
  155. </div>
  156. <div className={s.value}>
  157. <Moment format="MM/DD/YYYY HH:mm" date={createdAt} />
  158. </div>
  159. </div>
  160. {this.renderAuthFields()}
  161. </div>
  162. </div>
  163. )
  164. } else {
  165. return (<div className={s.root}>
  166. <div className={s.container}>
  167. <LoadingIcon />
  168. </div>
  169. </div>)
  170. }
  171. }
  172. }
  173. export default withStyles(CloudConnectionDetail, s);