summaryrefslogtreecommitdiffstats
path: root/webapp/components/backstage/backstage_controller.jsx
blob: 0ec6a00916349fe8b6b96f285cf411ebff769c8e (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
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';

import BackstageSidebar from './components/backstage_sidebar.jsx';
import BackstageNavbar from './components/backstage_navbar.jsx';
import ErrorBar from 'components/error_bar.jsx';

export default class BackstageController extends React.Component {
    static get propTypes() {
        return {
            user: React.PropTypes.object,
            children: React.PropTypes.node.isRequired
        };
    }

    constructor(props) {
        super(props);

        this.onTeamChange = this.onTeamChange.bind(this);

        const team = TeamStore.getCurrent();

        this.state = {
            team,
            isAdmin: UserStore.isSystemAdminForCurrentUser(this.props.user) ||
                TeamStore.isTeamAdminForCurrentTeam(team)
        };
    }

    componentDidMount() {
        TeamStore.addChangeListener(this.onTeamChange);
    }

    componentWillUnmount() {
        TeamStore.removeChangeListener(this.onTeamChange);
    }

    onTeamChange() {
        const team = TeamStore.getCurrent();

        this.state = {
            team,
            isAdmin: UserStore.isSystemAdminForCurrentUser(this.props.user) ||
                TeamStore.isTeamAdminForCurrentTeam(team)
        };
    }

    render() {
        return (
            <div className='backstage'>
                <ErrorBar/>
                <BackstageNavbar team={this.state.team}/>
                <div className='backstage-body'>
                    <BackstageSidebar
                        team={this.state.team}
                        user={this.props.user}
                    />
                    {
                        React.Children.map(this.props.children, (child) => {
                            if (!child) {
                                return child;
                            }

                            return React.cloneElement(child, {
                                team: this.state.team,
                                user: this.props.user,
                                isAdmin: this.state.isAdmin
                            });
                        })
                    }
                </div>
            </div>
        );
    }
}