import * as React from 'react'; import { connect } from 'react-redux'; import { Switch, Route, Redirect, withRouter } from 'react-router'; import Paper from 'material-ui/Paper'; import RaisedButton from 'material-ui/RaisedButton'; import TextField from 'material-ui/TextField'; import Toggle from 'material-ui/Toggle'; import { JournalList } from './JournalList'; import { JournalView } from './JournalView'; import * as EteSync from './api/EteSync'; import { routeResolver, getPalette } from './App'; import * as store from './store'; import * as C from './Constants'; export interface EteSyncContextType { serviceApiUrl: string; credentials: EteSync.Credentials; encryptionKey: string; } interface FormErrors { errorEmail?: string; errorPassword?: string; errorEncryptionPassword?: string; errorServer?: string; } function fetchCredentials(username: string, password: string, encryptionPassword: string, server: string) { const authenticator = new EteSync.Authenticator(server); return (dispatch: any) => { dispatch(store.credentialsRequest()); authenticator.getAuthToken(username, password).then( (authToken) => { const credentials = new EteSync.Credentials(username, authToken); const derived = EteSync.deriveKey(username, encryptionPassword); const context = { serviceApiUrl: server, credentials, encryptionKey: derived, }; dispatch(store.credentialsSuccess(context)); }, (error) => { dispatch(store.credentialsFailure(error)); } ); }; } export class EteSyncContextInner extends React.Component { state: { showAdvanced?: boolean; errors: FormErrors; server: string; username: string; password: string; encryptionPassword: string; }; props: { credentials: store.CredentialsType; }; constructor(props: any) { super(props); this.state = { errors: {}, server: '', username: '', password: '', encryptionPassword: '', }; this.generateEncryption = this.generateEncryption.bind(this); this.toggleAdvancedSettings = this.toggleAdvancedSettings.bind(this); this.handleInputChange = this.handleInputChange.bind(this); } handleInputChange(event: any) { const name = event.target.name; const value = event.target.value; this.setState({ [name]: value }); } generateEncryption(e: any) { e.preventDefault(); const server = this.state.showAdvanced ? this.state.server : C.serviceApiBase; const username = this.state.username; const password = this.state.password; const encryptionPassword = this.state.encryptionPassword; let errors: FormErrors = {}; const fieldRequired = 'This field is required!'; if (!username) { errors.errorEmail = fieldRequired; } if (!password) { errors.errorPassword = fieldRequired; } if (!encryptionPassword) { errors.errorEncryptionPassword = fieldRequired; } if (Object.keys(errors).length) { this.setState({errors: errors}); return; } store.store.dispatch(fetchCredentials(username, password, encryptionPassword, server)); } toggleAdvancedSettings() { this.setState({showAdvanced: !this.state.showAdvanced}); } render() { if (((this.props.credentials.status === store.FetchStatus.Initial) && (this.props.credentials.credentials === undefined)) || (this.props.credentials.status === store.FetchStatus.Failure)) { let advancedSettings = null; if (this.state.showAdvanced) { advancedSettings = (