aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/publishDebugInfo/Component.js
blob: 9e59fa20553869f616dee2d048f01f65adb6ccd8 (plain) (blame)
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { observer, inject } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import { H1 } from '@meetfranz/ui';
import injectSheet from 'react-jss';

import Input from '../../components/ui/Input';
import Button from '../../components/ui/Button';
import Modal from '../../components/ui/Modal';
import { state as ModalState } from '.';
import AppStore from '../../stores/AppStore';
import { DEBUG_API } from '../../config';
import { sendAuthRequest } from '../../api/utils/auth';

const messages = defineMessages({
  title: {
    id: 'feature.publishDebugInfo.title',
    defaultMessage: '!!!Publish debug information',
  },
  info: {
    id: 'feature.publishDebugInfo.info',
    defaultMessage: '!!!Publishing your debug information helps us find issues and errors in Ferdi. By publishing your debug information you accept Ferdi Debugger\'s privacy policy and terms of service',
  },
  privacy: {
    id: 'feature.publishDebugInfo.privacy',
    defaultMessage: '!!!Privacy policy',
  },
  terms: {
    id: 'feature.publishDebugInfo.terms',
    defaultMessage: '!!!Terms of service',
  },
  publish: {
    id: 'feature.publishDebugInfo.publish',
    defaultMessage: '!!!Accept and publish',
  },
  published: {
    id: 'feature.publishDebugInfo.published',
    defaultMessage: '!!!Your debug log was published and is now availible at',
  },
});

const styles = theme => ({
  info: {
    paddingTop: 20,
    paddingBottom: 20,
  },
  link: {
    color: theme.styleTypes.primary.accent + ' !important',
    padding: 10,
    cursor: 'pointer',
  },
  button: {
    width: '100%',
    marginTop: 10,
    cursor: 'pointer',
  },
  url: {
    marginTop: 20,

    '& > div': {
      fontFamily: 'SFMono-Regular,Consolas,Liberation Mono,Menlo,Courier,monospace',
    },
  },
});

export default @injectSheet(styles) @inject('stores') @observer class PublishDebugLogModal extends Component {
  static propTypes = {
    classes: PropTypes.object.isRequired,
  };

  static contextTypes = {
    intl: intlShape,
  };

  state = {
    log: null,
    isSendingLog: false,
  }

  // Close this modal
  close() {
    ModalState.isModalVisible = false;
  }

  async publishDebugInfo() {
    this.setState({
      isSendingLog: true,
    })

    const debugInfo = JSON.stringify(this.props.stores.app.debugInfo);
    
    const request = await sendAuthRequest(`${DEBUG_API}/create`, {
      method: 'POST',
      body: JSON.stringify({
        log: debugInfo
      }),
    }, false);
    const response = await request.json();
    console.log(response);
    if (response.id) {
      this.setState({
        log: response.id,
      })
    } else {
      // TODO: Show error message
      this.close();
    }
  }

  render() {
    const { isModalVisible } = ModalState;

    const {
      classes,
    } = this.props;

    const {
      log
    } = this.state;

    const { intl } = this.context;

    return (
      <Modal
        isOpen={isModalVisible}
        shouldCloseOnOverlayClick
        close={this.close.bind(this)}
      >
        <H1>
          {intl.formatMessage(messages.title)}
        </H1>
        { log ? (
          <>
            <p className={classes.info}>{intl.formatMessage(messages.published)}</p>
              <Input
                className={classes.url}
                showLabel={false}
                field={{
                  type: 'url',
                  value: DEBUG_API + '/' + log,
                  disabled: true,
                }}
                readonly
              />
          </>
        ) : (
          <>
            <p className={classes.info}>{intl.formatMessage(messages.info)}</p>

            <a href={ DEBUG_API + '/privacy.html' } target="_blank" className={classes.link}>
              {intl.formatMessage(messages.privacy)}
            </a>
            <a href={ DEBUG_API + '/terms.html' } target="_blank" className={classes.link}>
              {intl.formatMessage(messages.terms)}
            </a>

            <Button
              type="button"
              label={intl.formatMessage(messages.publish)}
              className={classes.button}
              onClick={this.publishDebugInfo.bind(this)}
              disabled={this.state.isSendingLog}
            />
          </>
        ) }
      </Modal>
    );
  }
}

PublishDebugLogModal.wrappedComponent.propTypes = {
  stores: PropTypes.shape({
    app: PropTypes.instanceOf(AppStore).isRequired,
  }).isRequired,
  actions: PropTypes.shape({
    service: PropTypes.shape({
      setActive: PropTypes.func.isRequired,
    }).isRequired,
  }).isRequired,
};