import React, {Component, PropTypes} from 'react'; import {Tab, Tabs, TabList, TabPanel} from 'react-tabs'; import DebouncedInput from '../base/DebouncedInput'; import FileInputButton from '../base/FileInputButton'; import Modal from '../base/Modal'; import ModalBody from '../base/ModalBody'; import ModalHeader from '../base/ModalHeader'; import PromptButton from '../base/PromptButton'; import * as models from '../../../models/index'; import * as fs from 'fs'; import {trackEvent} from '../../../analytics/index'; class WorkspaceSettingsModal extends Component { state = { showAddCertificateForm: false, crtPath: '', keyPath: '', pfxPath: '', host: '', passphrase: '', }; _workspaceUpdate = patch => models.workspace.update(this.props.workspace, patch); _handleSetModalRef = n => this.modal = n; _handleRemoveWorkspace = () => { this.props.handleRemoveWorkspace(); this.hide(); }; _handleToggleCertificateForm = () => { this.setState({showAddCertificateForm: !this.state.showAddCertificateForm}) }; _handleRename = name => this._workspaceUpdate({name}); _handleDescriptionChange = description => this._workspaceUpdate({description}); _handleCreateHostChange = e => this.setState({host: e.target.value}); _handleCreatePfxChange = pfxPath => this.setState({pfxPath}); _handleCreateCrtChange = crtPath => this.setState({crtPath}); _handleCreateKeyChange = keyPath => this.setState({keyPath}); _handleCreatePassphraseChange = e => this.setState({passphrase: e.target.value}); _handleSubmitCertificate = async e => { e.preventDefault(); const {workspace} = this.props; const {pfxPath, crtPath, keyPath, host, passphrase} = this.state; const cert = crtPath ? fs.readFileSync(crtPath, 'base64') : null; const key = keyPath ? fs.readFileSync(keyPath, 'base64') : null; const pfx = pfxPath ? fs.readFileSync(pfxPath, 'base64') : null; const certificate = {host, passphrase, cert, key, pfx, disabled: false}; const certificates = [ ...workspace.certificates.filter(c => c.host !== certificate.host), certificate, ]; await models.workspace.update(workspace, {certificates}); this._handleToggleCertificateForm(); trackEvent('Certificates', 'Create'); }; _handleDeleteCertificate = certificate => { const {workspace} = this.props; const certificates = workspace.certificates.filter(c => c.host !== certificate.host); models.workspace.update(workspace, {certificates}); trackEvent('Certificates', 'Delete'); }; _handleToggleCertificate = certificate => { const {workspace} = this.props; const certificates = workspace.certificates.map( c => c === certificate ? Object.assign({}, c, {disabled: !c.disabled}) : c ); models.workspace.update(workspace, {certificates}); trackEvent('Certificates', 'Toggle'); }; toggle (workspace) { this.modal.toggle(); this.setState({ workspace, showAddCertificateForm: false, crtPath: '', keyPath: '', pfxPath: '', host: '', passphrase: '', }); } show () { this.modal.show(); this.setState({ showAddCertificateForm: false, crtPath: '', keyPath: '', pfxPath: '', host: '', passphrase: '', }); } hide () { this.modal.hide(); } renderModalHeader () { const {workspace} = this.props; return ( Workspace Settings ) } renderModalBody () { const {workspace} = this.props; const {pfxPath, crtPath, keyPath, showAddCertificateForm} = this.state; return (
{!showAddCertificateForm ? (
{workspace.certificates.length === 0 ? (

You have not yet added any certificates

) : workspace.certificates.map(certificate => (
PFX: {" "} {certificate.pfx ? : } CRT: {" "} {certificate.cert ? : } Key: {" "} {certificate.key ? : } Passphrase: {" "} {certificate.passphrase ? : } Host: {" "} {certificate.host}
this._handleDeleteCertificate(certificate)}>
))}
) : (


  Or  

{" "}
)}
) } render () { const {workspace} = this.props; return ( {workspace ? this.renderModalHeader() : null} {workspace ? this.renderModalBody() : null} ) } } WorkspaceSettingsModal.propTypes = { handleRemoveWorkspace: PropTypes.func.isRequired, workspace: PropTypes.object.isRequired, }; export default WorkspaceSettingsModal;