/*
Copyright (C) 2017 Cloudbase Solutions SRL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
import React, { Component, PropTypes } from 'react';
import withStyles from 'isomorphic-style-loader/lib/withStyles';
import s from './CloudConnectionDetail.scss';
import Moment from 'react-moment';
import LoadingIcon from '../LoadingIcon';
import Helper from '../Helper';
const title = 'connection details';
class CloudConnectionDetail extends Component {
static propTypes = {
connection: PropTypes.object
}
static contextTypes = {
onSetTitle: PropTypes.func.isRequired,
};
constructor(props) {
super(props)
this.state = {
fields: this.processProps(props),
showPassword: false
}
}
componentWillMount() {
this.context.onSetTitle(title);
}
componentWillReceiveProps(newProps) {
this.setState({ fields: this.processProps(newProps) })
}
processProps(props) {
let fields = []
let addField = (name, value) => {
name = Helper.convertCloudFieldLabel(name)
if (!fields.find(f => f.fieldName === name)) {
fields.push({
fieldName: name,
fieldValue: value
})
}
}
if (props.connection.credentials) {
for (let fieldName in props.connection.credentials) {
let value = props.connection.credentials[fieldName]
if (value.value) { // if dropdown
value = value.value
}
if (value === true) value = "Yes"
if (value === false) value = "No"
if (typeof value === 'object') {
for (let extraField in value) {
addField(extraField, value[extraField])
}
} else {
addField(fieldName, value)
}
}
}
// Sort username and password to the front
let sortExceptions = { Username: 1, Password: 2 };
fields.sort((a, b) => {
if (sortExceptions[a.fieldName] && sortExceptions[b.fieldName]) {
return sortExceptions[a.fieldName] - sortExceptions[b.fieldName];
} else if (sortExceptions[a.fieldName]) {
return -1;
} else if (sortExceptions[b.fieldName]) {
return 1;
} else {
return 0;
}
})
return fields
}
handleShowPassword() {
this.setState({ showPassword: true })
}
renderAuthFields() {
let renderPasswordField = field => {
if (field.fieldName !== 'Password' || this.state.showPassword) {
return (
{field.fieldValue || '-'}
)
}
return (
•••••••••
)
}
if (this.state.fields.length) {
return this.state.fields.map((field, index) => {
if (field.fieldName !== 'login_type') {
return (
{field.fieldName}
{renderPasswordField(field)}
)
} else {
return null
}
})
} else {
return
}
}
render() {
let item = this.props.connection
let createdAt = Helper.getTimeObject(item.created_at)
if (item) {
return (
Type
{Helper.convertCloudLabel(item.type)}
Description
{item.description == "" ? "-" : item.description}
{this.renderAuthFields()}
)
} else {
return ()
}
}
}
export default withStyles(CloudConnectionDetail, s);