import React from 'react'; import { Container, Card, NgIf, Input, Button, Textarea, Loader, Notification, Prompt } from '../../components/'; import { invalidate, encrypt, decrypt, gid } from '../../helpers/'; import { Session } from '../../model/'; import config from '../../../config_client'; import './form.scss'; import img_drive from '../../assets/img/google-drive.png'; import img_dropbox from '../../assets/img/dropbox.png'; export class Form extends React.Component { constructor(props){ super(props); const protocols = Object.keys(config.connections); this.state = { refs: {}, type: protocols.length > 2 ? protocols[1] : protocols[0] || null, advanced_ftp: false, advanced_sftp: false, advanced_webdav: false, advanced_s3: false, advanced_git: false, _dummy: true }; this.rerender = this.rerender.bind(this); } componentDidMount(){ this.publishState(config.connections); this.publishState(this.props.credentials); window.addEventListener('resize', this.rerender); } componentWillReceiveProps(props){ if(JSON.stringify(props.credentials) !== JSON.stringify(this.props.credentials)){ this.publishState(props.credentials); } } componentWillUnmount(){ window.removeEventListener('resize', this.rerender); } publishState(_credentials){ const pushDOM = (credentials) => { for(let key in credentials){ let names = credentials[key]; for(let name in names){ const ref_name = [key,name].join("_"); if(this.state.refs[ref_name] && typeof credentials[key][name] !== 'boolean'){ this.state.refs[ref_name].ref.value = credentials[key][name]; } } } }; const setAdvancedCheckbox = (credentials) => { if(credentials['ftp'] && (credentials['ftp']['path'] || credentials['ftp']['port']) ){ this.setState({advanced_ftp: true}); } if(credentials['sftp'] && ( credentials['sftp']['path'] || credentials['sftp']['port'] || credentials['sftp']['private_key']) ){ this.setState({advanced_sftp: true}); } if(credentials['webdav'] && credentials['webdav']['path']){ this.setState({advanced_webdav: true}); } if(credentials['s3'] && credentials['s3']['path']){ this.setState({advanced_s3: true}); } if(credentials['git'] && ( credentials['git']['username'] || credentials['git']['commit'] || credentials['git']['branch'] || credentials['git']['passphrase'] || credentials['git']['author_name'] || credentials['git']['author_email'] || credentials['git']['committer_name'] || credentials['git']['committer_email']) ){ this.setState({advanced_git: true}); } }; setAdvancedCheckbox(_credentials); window.setTimeout(() => pushDOM(_credentials)); // we made this async as DOM needs to be all set before we can insert the new state } onSubmit(e){ e.preventDefault(); // update the credentials object with data coming from the dom (aka "ref" in react language) let credentials = Object.assign({}, this.props.credentials); for(let key in this.state.refs){ if(this.state.refs[key]){ let [type, ...name] = key.split('_'); name = name.join("_"); if(!credentials[type]) credentials[type] = {}; credentials[type][name] = this.state.refs[key].ref.value; } } // create the object we need to authenticate a user against a backend const auth_data = Object.assign({type: this.state.type}, credentials[this.state.type]); this.props.onSubmit(auth_data, credentials); } redirect_dropbox(e){ this.props.onThirdPartyLogin('dropbox'); } redirect_google(e){ this.props.onThirdPartyLogin('google'); } onTypeChange(type){ this.setState({type: type}, () => { this.publishState(config.connections); this.publishState(this.props.credentials); }); } rerender(){ this.setState({_dummy: !this.state._dummy}); } _marginTop(){ let size = 300; const $screen = document.querySelector('.login-form'); if($screen) size = $screen.offsetHeight; size = Math.round((document.body.offsetHeight - size) / 2); if(size < 0) return 0; if(size > 150) return 150; return size; } should_appear(type, key){ if(!config.connections[type]) return false; let value = config.connections[type][key]; if(typeof value === 'string') return true; if(value === false) return false; return true; } input_type(type, key){ if(!config.connections[type]) return 'hidden'; let value = config.connections[type][key]; if(typeof value === 'string') return 'hidden'; else if(typeof value === 'number') return 'hidden'; else if(value === false) return 'hidden'; else if(key === 'password') return 'password'; else if(key === 'secret_access_key') return 'password'; else{ return 'text'; } } render() { let className = (window.innerWidth < 600) ? 'scroll-x' : ''; return ( 1 }>
{ Object.keys(config.connections).map((type) => { return ( ); }) }
{ this.state.refs.webdav_url = input; }} autoComplete="new-password" /> { this.state.refs.webdav_username = input; }} autoComplete="new-password" /> { this.state.refs.webdav_password = input; }} autoComplete="new-password" /> {this.state.refs.webdav_path = input; }} autoComplete="new-password" /> {this.state.refs.ftp_hostname = input; }} autoComplete="new-password" /> {this.state.refs.ftp_username = input; }} autoComplete="new-password" /> {this.state.refs.ftp_password = input; }} autoComplete="new-password" /> {this.state.refs.ftp_path = input; }} autoComplete="new-password" /> {this.state.refs.ftp_port = input; }} autoComplete="new-password" /> {this.state.refs.sftp_host = input; }} autoComplete="new-password" /> {this.state.refs.sftp_username = input; }} autoComplete="new-password" /> {this.state.refs.sftp_password = input; }} autoComplete="new-password" /> {this.state.refs.sftp_path = input; }} autoComplete="new-password" /> {this.state.refs.sftp_port = input; }} autoComplete="new-password" />