summaryrefslogtreecommitdiffhomepage
path: root/app/components/Support.js
blob: fa4e6cf62d07a5947f1131e9c849d5e0fa942bdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// @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<string>) => Promise<string>,
  onSend: (email: string, message: string, savedReport: string) => void,
};

export default class Support extends Component<SupportProps, SupportState> {
  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<string> {
    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 = (
      <View style={styles.support__header}>
        <Text style={styles.support__title}>Report a problem</Text>
        {(sendState === 'INITIAL' || sendState === 'CONFIRM_NO_EMAIL') && (
          <Text style={styles.support__subtitle}>
            {
              "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."
            }
          </Text>
        )}
      </View>
    );

    const content = this._renderContent();

    return (
      <Layout>
        <Container>
          <View style={styles.support}>
            <Button
              style={styles.support__close}
              onPress={this.props.onClose}
              testName="support__close">
              <Img height={24} width={24} style={styles.support__close_icon} source="icon-back" />
              <Text style={styles.support__close_title}>Settings</Text>
            </Button>
            <View style={styles.support__container}>
              {header}

              {content}
            </View>
          </View>
        </Container>
      </Layout>
    );
  }

  _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 (
      <View style={styles.support__content}>
        <View style={styles.support__form}>
          <View style={styles.support__form_row}>
            <TextInput
              style={styles.support__form_email}
              placeholder="Your email"
              defaultValue={this.state.email}
              onChangeText={this.onChangeEmail}
              keyboardType="email-address"
            />
          </View>
          <View style={styles.support__form_row_message}>
            <View style={styles.support__form_message_scroll_wrap}>
              <TextInput
                style={styles.support__form_message}
                placeholder="Describe your problem"
                defaultValue={this.state.message}
                multiline={true}
                onChangeText={this.onChangeDescription}
                testName="support__form_message"
              />
            </View>
          </View>
          <View style={styles.support__footer}>
            {this.state.sendState === 'CONFIRM_NO_EMAIL'
              ? this._renderNoEmailWarning()
              : this._renderActionButtons()}
          </View>
        </View>
      </View>
    );
  }

  _renderNoEmailWarning() {
    return (
      <View>
        <Text style={styles.support__no_email_warning}>
          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.
        </Text>
        <GreenButton
          disabled={!this.validate()}
          onPress={this.onSend}
          testName="support__send_logs">
          Send anyway
        </GreenButton>
      </View>
    );
  }

  _renderActionButtons() {
    return [
      <BlueButton key={1} onPress={this.onViewLog} testName="support__view_logs">
        <Label>View app logs</Label>
        <Img source="icon-extLink" height={16} width={16} />
      </BlueButton>,
      <GreenButton
        key={2}
        disabled={!this.validate()}
        onPress={this.onSend}
        testName="support__send_logs">
        Send
      </GreenButton>,
    ];
  }

  _renderLoading() {
    return (
      <View style={styles.support__content}>
        <View style={styles.support__form}>
          <View style={styles.support__form_row}>
            <View style={styles.support__status_icon}>
              <Img source="icon-spinner" height={60} width={60} alt="" />
            </View>
            <View style={styles.support__status_security__secure}>Secure Connection</View>
            <Text style={styles.support__send_status}>Sending...</Text>
          </View>
        </View>
      </View>
    );
  }

  _renderSent() {
    return (
      <View style={styles.support__content}>
        <View style={styles.support__form}>
          <View style={styles.support__form_row}>
            <View style={styles.support__status_icon}>
              <Img source="icon-success" height={60} width={60} alt="" />
            </View>
            <Text style={styles.support__status_security__secure}>Secure Connection</Text>
            <Text style={styles.support__send_status}>Sent</Text>

            <Text style={styles.support__subtitle}>Thanks! We will look into this.</Text>
            {this.state.email.trim().length > 0 ? (
              <Text style={styles.support__subtitle}>
                If needed we will contact you on {'\u00A0'}
                <Text style={styles.support__sent_email}>{this.state.email}</Text>
              </Text>
            ) : null}
          </View>
        </View>
      </View>
    );
  }

  _renderFailed() {
    return (
      <View style={styles.support__content}>
        <View style={styles.support__form}>
          <View style={styles.support__form_row}>
            <View style={styles.support__status_icon}>
              <Img source="icon-fail" height={60} width={60} alt="" />
            </View>
            <Text style={styles.support__status_security__secure}>Secure Connection</Text>
            <Text style={styles.support__send_status}>Failed to send</Text>
          </View>
        </View>
        <View style={styles.support__footer}>
          <BlueButton onPress={() => this.setState({ sendState: 'INITIAL' })}>
            Edit message
          </BlueButton>
          <GreenButton onPress={this.onSend} testName="support__send_logs">
            Try again
          </GreenButton>
        </View>
      </View>
    );
  }
}