CloudConnectionsView.js 5.4 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 './CloudConnectionsView.scss';
  17. import Header from '../Header';
  18. import ConnectionsActions from '../../actions/ConnectionsActions';
  19. import Location from '../../core/Location';
  20. import Link from '../Link';
  21. import Dropdown from '../NewDropdown';
  22. import LoadingIcon from '../LoadingIcon';
  23. import Modal from 'react-modal';
  24. import AddCloudConnection from '../AddCloudConnection';
  25. import ConfirmationDialog from '../ConfirmationDialog'
  26. class CloudConnectionsView extends Component {
  27. title = ""
  28. static propTypes = {
  29. connection: PropTypes.object,
  30. connectionId: PropTypes.string
  31. }
  32. static defaultProps = {
  33. connection: null
  34. }
  35. static contextTypes = {
  36. onSetTitle: PropTypes.func.isRequired,
  37. };
  38. constructor(props) {
  39. super(props)
  40. this.state = {
  41. connection: {
  42. name: null,
  43. type: null,
  44. id: null
  45. },
  46. confirmationDialog: {
  47. visible: false,
  48. message: "Are you sure?",
  49. onConfirm: null,
  50. onCancel: null
  51. },
  52. showModal: false
  53. }
  54. }
  55. componentWillReceiveProps(newProps, oldProps) {
  56. if (newProps.connections) {
  57. let connection = newProps.connections.filter((connection => connection.id == this.props.connectionId))[0]
  58. this.setState({ connection: connection })
  59. }
  60. }
  61. componentDidMount() {
  62. this.context.onSetTitle(this.title);
  63. }
  64. onConnectionsActionsChange(option) {
  65. switch (option.value) {
  66. case "delete":
  67. ConnectionsActions.deleteConnection(this.state.connection)
  68. Location.push('/cloud-endpoints')
  69. break
  70. default:
  71. break
  72. }
  73. }
  74. showEditConnectionModal() {
  75. this.setState({ showModal: true })
  76. }
  77. deleteConnection() {
  78. this.setState({
  79. confirmationDialog: {
  80. visible: true,
  81. onConfirm: () => {
  82. this.setState({ confirmationDialog: { visible: false }})
  83. ConnectionsActions.deleteConnection(this.state.connection)
  84. Location.push('/cloud-endpoints')
  85. },
  86. onCancel: () => {
  87. this.setState({ confirmationDialog: { visible: false }})
  88. }
  89. }
  90. })
  91. }
  92. closeModal() {
  93. this.setState({ showModal: false })
  94. }
  95. goBack() {
  96. Location.push("/cloud-endpoints")
  97. }
  98. render() {
  99. let item = this.state.connection
  100. let title = "Edit Connection"
  101. let modalStyle = {
  102. content: {
  103. padding: "0px",
  104. borderRadius: "4px",
  105. bottom: "auto",
  106. width: "576px",
  107. height: "auto",
  108. left: "50%",
  109. top: "70px",
  110. marginLeft: "-288px"
  111. }
  112. }
  113. if (item) {
  114. return (
  115. <div className={s.root}>
  116. <Header title={title} linkUrl="/cloud-endpoints"/>
  117. <div className={s.connectionHead}>
  118. <div className={s.container}>
  119. <div className="backBtn" onClick={(e) => this.goBack(e)}></div>
  120. <div className={s.connectionTypeImg + " icon endpoint-white "}></div>
  121. <div className={s.connectionInfo}>
  122. <h2>{item.name}</h2>
  123. </div>
  124. </div>
  125. </div>
  126. <div className={s.container}>
  127. <div className={s.sidebar}>
  128. </div>
  129. <div className={s.content}>
  130. <div className={s.connectionTypeImg + " icon large-cloud " + (item && item.type)}></div>
  131. <br />
  132. {React.cloneElement(this.props.children, { connection: item })}
  133. <button onClick={(e) => this.showEditConnectionModal(e)} className="gray">Edit Connection</button>
  134. <br /><br />
  135. <button onClick={(e) => this.deleteConnection(e)} className="wire">Delete</button>
  136. </div>
  137. </div>
  138. <Modal
  139. isOpen={this.state.showModal}
  140. contentLabel="Add new cloud connection"
  141. style={modalStyle}
  142. >
  143. <AddCloudConnection
  144. closeHandle={(e) => this.closeModal(e)}
  145. addHandle={(e) => this.closeModal(e)}
  146. connection={item}
  147. />
  148. </Modal>
  149. <ConfirmationDialog
  150. visible={this.state.confirmationDialog.visible}
  151. message={this.state.confirmationDialog.message}
  152. onConfirm={(e) => this.state.confirmationDialog.onConfirm(e)}
  153. onCancel={(e) => this.state.confirmationDialog.onCancel(e)}
  154. />
  155. </div>
  156. );
  157. } else {
  158. return (
  159. <div className={s.root}>
  160. <Header title={title} linkUrl="/cloud-endpoints"/>
  161. <div className={s.container}>
  162. <LoadingIcon />
  163. </div>
  164. </div>)
  165. }
  166. }
  167. }
  168. export default withStyles(CloudConnectionsView, s);