// @flow import * as React from 'react'; import { Component, Text, View, TextInput } from 'reactxp'; import { Button, BlueButton, GreenButton, Label } from './styled'; import { Layout, Container } from './Layout'; import styles from './SupportStyles'; import Img from './Img'; import type { AccountReduxState } from '../redux/account/reducers'; export type SupportReport = { email: string, message: string, savedReport: ?string, }; type SupportState = { email: string, message: string, savedReport: ?string, sendState: 'INITIAL' | 'CONFIRM_NO_EMAIL' | 'LOADING' | 'SUCCESS' | 'FAILED', }; export type SupportProps = { account: AccountReduxState, onClose: () => void, onViewLog: (string) => void, onCollectLog: (Array) => Promise, onSend: (email: string, message: string, savedReport: string) => void, }; export default class Support extends Component { state = { email: '', message: '', savedReport: null, sendState: 'INITIAL', }; validate() { return this.state.message.trim().length > 0; } onChangeEmail = (email: string) => { this.setState({ email: email }); }; onChangeDescription = (description: string) => { this.setState({ message: description }); }; onViewLog = () => { this._getLog().then((path) => { this.props.onViewLog(path); }); }; _getLog(): Promise { const accountsToRedact = this.props.account.accountHistory; const { savedReport } = this.state; return savedReport ? Promise.resolve(savedReport) : this.props.onCollectLog(accountsToRedact).then((path) => { return new Promise((resolve) => this.setState({ savedReport: path }, () => resolve(path)), ); }); } onSend = () => { if (this.state.sendState === 'INITIAL' && this.state.email.length === 0) { this.setState({ sendState: 'CONFIRM_NO_EMAIL', }); } else { this._sendProblemReport(); } }; _sendProblemReport() { this.setState( { sendState: 'LOADING', }, () => { this._getLog() .then((path) => { return this.props.onSend(this.state.email, this.state.message, path); }) .then(() => { this.setState({ sendState: 'SUCCESS', }); }) .catch(() => { this.setState({ sendState: 'FAILED', }); }); }, ); } render() { const { sendState } = this.state; const header = ( Report a problem {(sendState === 'INITIAL' || sendState === 'CONFIRM_NO_EMAIL') && ( { "To help you more effectively, your app's log file will be attached to this message. Your data will remain secure and private, as it is anonymised before being sent over an encrypted channel." } )} ); const content = this._renderContent(); return ( {header} {content} ); } _renderContent() { switch (this.state.sendState) { case 'INITIAL': case 'CONFIRM_NO_EMAIL': return this._renderForm(); case 'LOADING': return this._renderLoading(); case 'SUCCESS': return this._renderSent(); case 'FAILED': return this._renderFailed(); default: return null; } } _renderForm() { return ( {this.state.sendState === 'CONFIRM_NO_EMAIL' ? this._renderNoEmailWarning() : this._renderActionButtons()} ); } _renderNoEmailWarning() { return ( You are about to send the problem report without a way for us to get back to you. If you want an answer to your report you will have to enter an email address. Send anyway ); } _renderActionButtons() { return [ , Send , ]; } _renderLoading() { return ( Secure Connection Sending... ); } _renderSent() { return ( Secure Connection Sent Thanks! We will look into this. {this.state.email.trim().length > 0 ? ( If needed we will contact you on {'\u00A0'} {this.state.email} ) : null} ); } _renderFailed() { return ( Secure Connection Failed to send this.setState({ sendState: 'INITIAL' })}> Edit message Try again ); } }