summaryrefslogtreecommitdiffhomepage
path: root/app/components/Settings.js
blob: ad90e034c4b5023d2c22268221b334c9396c3df4 (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
// @flow
import moment from 'moment';
import * as React from 'react';
import { Component, Text, View } from 'reactxp';
import { Button, CellButton, RedButton, Label, SubText } from './styled';
import { Layout, Container } from './Layout';
import CustomScrollbars from './CustomScrollbars';
import styles from './SettingsStyles';
import Img from './Img';

import type { AccountReduxState } from '../redux/account/reducers';
import type { SettingsReduxState } from '../redux/settings/reducers';

export type SettingsProps = {
  account: AccountReduxState,
  settings: SettingsReduxState,
  version: string,
  onQuit: () => void,
  onClose: () => void,
  onViewAccount: () => void,
  onViewSupport: () => void,
  onViewPreferences: () => void,
  onViewAdvancedSettings: () => void,
  onExternalLink: (type: string) => void,
};

export default class Settings extends Component<SettingsProps> {
  render() {
    return (
      <Layout>
        <Container>
          <View style={styles.settings}>
            <Button
              style={styles.settings__close}
              onPress={this.props.onClose}
              testName="settings__close">
              <Img height={24} width={24} style={styles.settings__close_icon} source="icon-close" />
            </Button>

            <View style={styles.settings__container}>
              <View style={styles.settings__header}>
                <Text style={styles.settings__title}>Settings</Text>
              </View>

              <CustomScrollbars style={styles.settings__scrollview} autoHide={true}>
                <View style={styles.settings__content}>
                  <View>
                    {this._renderTopButtons()}
                    {this._renderMiddleButtons()}
                    {this._renderBottomButtons()}
                  </View>
                  {this._renderQuitButton()}
                </View>
              </CustomScrollbars>
            </View>
          </View>
        </Container>
      </Layout>
    );
  }

  _renderTopButtons() {
    const isLoggedIn = this.props.account.status === 'ok';
    if (!isLoggedIn) {
      return null;
    }

    let isOutOfTime = false,
      formattedExpiry = '';
    const expiryIso = this.props.account.expiry;

    if (isLoggedIn && expiryIso) {
      const expiry = moment(this.props.account.expiry);
      isOutOfTime = expiry.isSameOrBefore(moment());
      formattedExpiry = (expiry.fromNow(true) + ' left').toUpperCase();
    }

    return (
      <View>
        <View style={styles.settings_account} testName="settings__account">
          {isOutOfTime ? (
            <CellButton
              onPress={this.props.onViewAccount}
              testName="settings__account_paid_until_button">
              <Label>Account</Label>
              <SubText
                testName="settings__account_paid_until_subtext"
                style={styles.settings__account_paid_until_Label__error}>
                OUT OF TIME
              </SubText>
              <Img height={12} width={7} source="icon-chevron" />
            </CellButton>
          ) : (
            <CellButton
              onPress={this.props.onViewAccount}
              testName="settings__account_paid_until_button">
              <Label>Account</Label>
              <SubText testName="settings__account_paid_until_subtext">{formattedExpiry}</SubText>
              <Img height={12} width={7} source="icon-chevron" />
            </CellButton>
          )}
        </View>

        <CellButton onPress={this.props.onViewPreferences} testName="settings__preferences">
          <Label>Preferences</Label>
          <Img height={12} width={7} source="icon-chevron" />
        </CellButton>

        <CellButton onPress={this.props.onViewAdvancedSettings} testName="settings__advanced">
          <Label>Advanced</Label>
          <Img height={12} width={7} source="icon-chevron" />
        </CellButton>
        <View style={styles.settings__cell_spacer} />
      </View>
    );
  }

  _renderMiddleButtons() {
    return (
      <View>
        <CellButton
          onPress={this.props.onExternalLink.bind(this, 'download')}
          testName="settings__version">
          <Label>App version</Label>
          <SubText>{this._formattedVersion()}</SubText>
          <Img height={16} width={16} source="icon-extLink" />
        </CellButton>
        <View style={styles.settings__cell_spacer} />
      </View>
    );
  }

  _formattedVersion() {
    // the version in package.json has to be semver, but we use a YEAR.release-channel
    // version scheme. in package.json we thus have to write YEAR.release.X-channel and
    // this function is responsible for removing .X part.
    return this.props.version
      .replace('.0-', '-') // remove the .0 in 2018.1.0-beta9
      .replace(/\.0$/, ''); // remove the .0 in 2018.1.0
  }

  _renderBottomButtons() {
    return (
      <View>
        <CellButton
          onPress={this.props.onExternalLink.bind(this, 'faq')}
          testName="settings__external_link">
          <Label>FAQs</Label>
          <Img height={16} width={16} source="icon-extLink" />
        </CellButton>

        <CellButton
          onPress={this.props.onExternalLink.bind(this, 'guides')}
          testName="settings__external_link">
          <Label>Guides</Label>
          <Img height={16} width={16} source="icon-extLink" />
        </CellButton>

        <CellButton onPress={this.props.onViewSupport} testName="settings__view_support">
          <Label>Report a problem</Label>
          <Img height={12} width={7} source="icon-chevron" />
        </CellButton>
      </View>
    );
  }

  _renderQuitButton() {
    return (
      <View style={styles.settings__footer}>
        <RedButton onPress={this.props.onQuit} testName="settings__quit">
          <Label>Quit app</Label>
        </RedButton>
      </View>
    );
  }
}