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

import AdminSidebar from './admin_sidebar.jsx';
import AdminStore from '../../stores/admin_store.jsx';
import TeamStore from '../../stores/team_store.jsx';
import * as AsyncClient from '../../utils/async_client.jsx';
import LoadingScreen from '../loading_screen.jsx';
import * as Utils from '../../utils/utils.jsx';

import EmailSettingsTab from './email_settings.jsx';
import LogSettingsTab from './log_settings.jsx';
import LogsTab from './logs.jsx';
import FileSettingsTab from './image_settings.jsx';
import PrivacySettingsTab from './privacy_settings.jsx';
import RateSettingsTab from './rate_settings.jsx';
import GitLabSettingsTab from './gitlab_settings.jsx';
import SqlSettingsTab from './sql_settings.jsx';
import TeamSettingsTab from './team_settings.jsx';
import ServiceSettingsTab from './service_settings.jsx';
import LegalAndSupportSettingsTab from './legal_and_support_settings.jsx';
import TeamUsersTab from './team_users.jsx';
import TeamAnalyticsTab from './team_analytics.jsx';

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

        this.selectTab = this.selectTab.bind(this);
        this.removeSelectedTeam = this.removeSelectedTeam.bind(this);
        this.addSelectedTeam = this.addSelectedTeam.bind(this);
        this.onConfigListenerChange = this.onConfigListenerChange.bind(this);
        this.onAllTeamsListenerChange = this.onAllTeamsListenerChange.bind(this);

        var selectedTeams = AdminStore.getSelectedTeams();
        if (selectedTeams == null) {
            selectedTeams = {};
            selectedTeams[TeamStore.getCurrentId()] = 'true';
            AdminStore.saveSelectedTeams(selectedTeams);
        }

        this.state = {
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams,
            selected: props.tab || 'service_settings',
            selectedTeam: props.teamId || null
        };

        if (!props.tab) {
            var tokenIndex = Utils.getUrlParameter('session_token_index');
            history.replaceState(null, null, `/admin_console/${this.state.selected}?session_token_index=${tokenIndex}`);
        }
    }

    componentDidMount() {
        AdminStore.addConfigChangeListener(this.onConfigListenerChange);
        AsyncClient.getConfig();

        AdminStore.addAllTeamsChangeListener(this.onAllTeamsListenerChange);
        AsyncClient.getAllTeams();
    }

    componentWillUnmount() {
        AdminStore.removeConfigChangeListener(this.onConfigListenerChange);
        AdminStore.removeAllTeamsChangeListener(this.onAllTeamsListenerChange);
    }

    onConfigListenerChange() {
        this.setState({
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams: AdminStore.getSelectedTeams(),
            selected: this.state.selected,
            selectedTeam: this.state.selectedTeam
        });
    }

    onAllTeamsListenerChange() {
        this.setState({
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams: AdminStore.getSelectedTeams(),
            selected: this.state.selected,
            selectedTeam: this.state.selectedTeam

        });
    }

    selectTab(tab, teamId) {
        this.setState({
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams: AdminStore.getSelectedTeams(),
            selected: tab,
            selectedTeam: teamId
        });
    }

    removeSelectedTeam(teamId) {
        var selectedTeams = AdminStore.getSelectedTeams();
        Reflect.deleteProperty(selectedTeams, teamId);
        AdminStore.saveSelectedTeams(selectedTeams);

        this.setState({
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams: AdminStore.getSelectedTeams(),
            selected: this.state.selected,
            selectedTeam: this.state.selectedTeam
        });
    }

    addSelectedTeam(teamId) {
        var selectedTeams = AdminStore.getSelectedTeams();
        selectedTeams[teamId] = 'true';
        AdminStore.saveSelectedTeams(selectedTeams);

        this.setState({
            config: AdminStore.getConfig(),
            teams: AdminStore.getAllTeams(),
            selectedTeams: AdminStore.getSelectedTeams(),
            selected: this.state.selected,
            selectedTeam: this.state.selectedTeam
        });
    }

    render() {
        var tab = <LoadingScreen />;

        if (this.state.config != null) {
            if (this.state.selected === 'email_settings') {
                tab = <EmailSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'log_settings') {
                tab = <LogSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'logs') {
                tab = <LogsTab />;
            } else if (this.state.selected === 'image_settings') {
                tab = <FileSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'privacy_settings') {
                tab = <PrivacySettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'rate_settings') {
                tab = <RateSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'gitlab_settings') {
                tab = <GitLabSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'sql_settings') {
                tab = <SqlSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'team_settings') {
                tab = <TeamSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'service_settings') {
                tab = <ServiceSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'legal_and_support_settings') {
                tab = <LegalAndSupportSettingsTab config={this.state.config} />;
            } else if (this.state.selected === 'team_users') {
                if (this.state.teams) {
                    tab = <TeamUsersTab team={this.state.teams[this.state.selectedTeam]} />;
                }
            } else if (this.state.selected === 'team_analytics') {
                if (this.state.teams) {
                    tab = <TeamAnalyticsTab team={this.state.teams[this.state.selectedTeam]} />;
                }
            }
        }

        return (
            <div>
                <div
                    className='sidebar--menu'
                    id='sidebar-menu'
                />
                <AdminSidebar
                    selected={this.state.selected}
                    selectedTeam={this.state.selectedTeam}
                    selectTab={this.selectTab}
                    teams={this.state.teams}
                    selectedTeams={this.state.selectedTeams}
                    removeSelectedTeam={this.removeSelectedTeam}
                    addSelectedTeam={this.addSelectedTeam}
                />
                <div className='inner__wrap channel__wrap'>
                    <div className='row header'>
                    </div>
                    <div className='row main'>
                        <div
                            id='app-content'
                            className='app__content admin'
                        >
                        {tab}
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}

AdminController.defaultProps = {
};

AdminController.propTypes = {
    tab: React.PropTypes.string,
    teamId: React.PropTypes.string
};