CloudConnectionDetail.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  33. }
  34. componentWillMount() {
  35. this.context.onSetTitle(title);
  36. }
  37. componentWillReceiveProps(newProps) {
  38. this.setState({ fields: this.processProps(newProps) })
  39. }
  40. processProps(props) {
  41. let fields = []
  42. let addField = (name, value) => {
  43. name = Helper.convertCloudFieldLabel(name)
  44. if (!fields.find(f => f.fieldName === name)) {
  45. fields.push({
  46. fieldName: name,
  47. fieldValue: value
  48. })
  49. }
  50. }
  51. if (props.connection.credentials) {
  52. for (let fieldName in props.connection.credentials) {
  53. let value = props.connection.credentials[fieldName]
  54. if (value.value) { // if dropdown
  55. value = value.value
  56. }
  57. if (value === true) value = "Yes"
  58. if (value === false) value = "No"
  59. if (typeof value === 'object') {
  60. for (let extraField in value) {
  61. addField(extraField, value[extraField])
  62. }
  63. } else {
  64. addField(fieldName, value)
  65. }
  66. }
  67. }
  68. // Sort username and password to the front
  69. let sortExceptions = { Username: 1, Password: 2 };
  70. fields.sort((a, b) => {
  71. if (sortExceptions[a.fieldName] && sortExceptions[b.fieldName]) {
  72. return sortExceptions[a.fieldName] - sortExceptions[b.fieldName];
  73. } else if (sortExceptions[a.fieldName]) {
  74. return -1;
  75. } else if (sortExceptions[b.fieldName]) {
  76. return 1;
  77. } else {
  78. return 0;
  79. }
  80. })
  81. return fields
  82. }
  83. renderAuthFields() {
  84. if (this.state.fields.length) {
  85. return this.state.fields.map((field, index) => {
  86. if (field.fieldName !== 'login_type') {
  87. return (
  88. <div className={s.formGroup} key={"formGroup_" + index}>
  89. <div className={s.title}>
  90. {field.fieldName}
  91. </div>
  92. <div className={s.value}>
  93. {field.fieldValue || '-'}
  94. </div>
  95. </div>
  96. )
  97. } else {
  98. return null
  99. }
  100. })
  101. } else {
  102. return <LoadingIcon />
  103. }
  104. }
  105. render() {
  106. let item = this.props.connection
  107. let createdAt = Helper.getTimeObject(item.created_at)
  108. if (item) {
  109. return (
  110. <div className={s.root}>
  111. <div className={s.container}>
  112. <div className={s.formGroup}>
  113. <div className={s.title}>
  114. Name
  115. </div>
  116. <div className={s.value}>
  117. {item.name}
  118. </div>
  119. </div>
  120. <div className={s.formGroup}>
  121. <div className={s.title}>
  122. Type
  123. </div>
  124. <div className={s.value}>
  125. {Helper.convertCloudLabel(item.type)}
  126. </div>
  127. </div>
  128. <div className={s.formGroup}>
  129. <div className={s.title}>
  130. Description
  131. </div>
  132. <div className={s.value}>
  133. {item.description == "" ? "-" : item.description}
  134. </div>
  135. </div>
  136. <div className={s.formGroup}>
  137. <div className={s.title}>
  138. Created
  139. </div>
  140. <div className={s.value}>
  141. <Moment format="MM/DD/YYYY HH:mm" date={createdAt} />
  142. </div>
  143. </div>
  144. </div>
  145. <div className={s.container}>
  146. {this.renderAuthFields()}
  147. </div>
  148. </div>
  149. )
  150. } else {
  151. return (<div className={s.root}>
  152. <div className={s.container}>
  153. <LoadingIcon />
  154. </div>
  155. </div>)
  156. }
  157. }
  158. }
  159. export default withStyles(CloudConnectionDetail, s);