summaryrefslogtreecommitdiffstats
path: root/web/react/components/user_settings_appearance.jsx
blob: 878ec42fc03871f181f1d7ea806e8c199e2d5ad5 (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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var UserStore = require('../stores/user_store.jsx');
var SettingItemMin = require('./setting_item_min.jsx');
var SettingItemMax = require('./setting_item_max.jsx');
var Client = require('../utils/client.jsx');
var Utils = require('../utils/utils.jsx');

export default class UserSettingsAppearance extends React.Component {
    constructor(props) {
        super(props);

        this.submitTheme = this.submitTheme.bind(this);
        this.updateTheme = this.updateTheme.bind(this);
        this.handleClose = this.handleClose.bind(this);

        this.state = this.getStateFromStores();
    }
    getStateFromStores() {
        var user = UserStore.getCurrentUser();
        var theme = '#2389d7';
        if (config.ThemeColors != null) {
            theme = config.ThemeColors[0];
        }
        if (user.props && user.props.theme) {
            theme = user.props.theme;
        }

        return {theme: theme.toLowerCase()};
    }
    submitTheme(e) {
        e.preventDefault();
        var user = UserStore.getCurrentUser();
        if (!user.props) {
            user.props = {};
        }
        user.props.theme = this.state.theme;

        Client.updateUser(user,
            function success() {
                this.props.updateSection('');
                window.location.reload();
            }.bind(this),
            function fail(err) {
                var state = this.getStateFromStores();
                state.serverError = err;
                this.setState(state);
            }.bind(this)
        );
    }
    updateTheme(e) {
        var hex = Utils.rgb2hex(e.target.style.backgroundColor);
        this.setState({theme: hex.toLowerCase()});
    }
    handleClose() {
        this.setState({serverError: null});
        this.props.updateTab('general');
    }
    componentDidMount() {
        if (this.props.activeSection === 'theme') {
            $(React.findDOMNode(this.refs[this.state.theme])).addClass('active-border');
        }
        $('#user_settings').on('hidden.bs.modal', this.handleClose);
    }
    componentDidUpdate() {
        if (this.props.activeSection === 'theme') {
            $('.color-btn').removeClass('active-border');
            $(React.findDOMNode(this.refs[this.state.theme])).addClass('active-border');
        }
    }
    componentWillUnmount() {
        $('#user_settings').off('hidden.bs.modal', this.handleClose);
        this.props.updateSection('');
    }
    render() {
        var serverError;
        if (this.state.serverError) {
            serverError = this.state.serverError;
        }

        var themeSection;
        var self = this;

        if (config.ThemeColors != null) {
            if (this.props.activeSection === 'theme') {
                var themeButtons = [];

                for (var i = 0; i < config.ThemeColors.length; i++) {
                    themeButtons.push(
                        <button
                            ref={config.ThemeColors[i]}
                            type='button'
                            className='btn btn-lg color-btn'
                            style={{backgroundColor: config.ThemeColors[i]}}
                            onClick={this.updateTheme}
                        />
                    );
                }

                var inputs = [];

                inputs.push(
                    <li className='setting-list-item'>
                        <div
                            className='btn-group'
                            data-toggle='buttons-radio'
                        >
                            {themeButtons}
                        </div>
                    </li>
                );

                themeSection = (
                    <SettingItemMax
                        title='Theme Color'
                        inputs={inputs}
                        submit={this.submitTheme}
                        serverError={serverError}
                        updateSection={function updateSection(e) {
                            self.props.updateSection('');
                            e.preventDefault();
                        }}
                    />
                );
            } else {
                themeSection = (
                    <SettingItemMin
                        title='Theme Color'
                        describe={this.state.theme}
                        updateSection={function updateSection() {
                            self.props.updateSection('theme');
                        }}
                    />
                );
            }
        }

        return (
            <div>
                <div className='modal-header'>
                    <button
                        type='button'
                        className='close'
                        data-dismiss='modal'
                        aria-label='Close'
                    >
                        <span aria-hidden='true'>&times;</span>
                    </button>
                    <h4
                        className='modal-title'
                        ref='title'
                    >
                        <i className='modal-back'></i>Appearance Settings
                    </h4>
                </div>
                <div className='user-settings'>
                    <h3 className='tab-header'>Appearance Settings</h3>
                    <div className='divider-dark first'/>
                    {themeSection}
                    <div className='divider-dark'/>
                </div>
            </div>
        );
    }
}

UserSettingsAppearance.defaultProps = {
    activeSection: ''
};
UserSettingsAppearance.propTypes = {
    activeSection: React.PropTypes.string,
    updateSection: React.PropTypes.func,
    updateTab: React.PropTypes.func
};