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

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

import Constants from 'utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;

import * as AsyncClient from 'utils/async_client.jsx';
import Client from 'client/web_client.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';

import {browserHistory} from 'react-router/es6';

export function checkIfTeamExists(teamName, onSuccess, onError) {
    Client.findTeamByName(teamName, onSuccess, onError);
}

export function createTeam(team, onSuccess, onError) {
    Client.createTeam(team,
        (rteam) => {
            AppDispatcher.handleServerAction({
                type: ActionTypes.CREATED_TEAM,
                team: rteam,
                member: {team_id: rteam.id, user_id: UserStore.getCurrentId(), roles: 'team_admin team_user'}
            });

            browserHistory.push('/' + rteam.name + '/channels/town-square');

            if (onSuccess) {
                onSuccess(rteam);
            }
        },
        onError
    );
}

export function updateTeam(team, onSuccess, onError) {
    Client.updateTeam(team,
        (rteam) => {
            AppDispatcher.handleServerAction({
                type: ActionTypes.UPDATE_TEAM,
                team: rteam
            });

            browserHistory.push('/' + rteam.name + '/channels/town-square');

            if (onSuccess) {
                onSuccess(rteam);
            }
        },
        onError
    );
}

export function removeUserFromTeam(teamId, userId, success, error) {
    Client.removeUserFromTeam(
        teamId,
        userId,
        () => {
            TeamStore.removeMemberInTeam(teamId, userId);
            UserStore.removeProfileFromTeam(teamId, userId);
            UserStore.emitInTeamChange();
            AsyncClient.getUser(userId);
            AsyncClient.getTeamStats(teamId);

            if (success) {
                success();
            }
        },
        (err) => {
            AsyncClient.dispatchError(err, 'removeUserFromTeam');

            if (error) {
                error(err);
            }
        }
    );
}

export function updateTeamMemberRoles(teamId, userId, newRoles, success, error) {
    Client.updateTeamMemberRoles(teamId, userId, newRoles,
        () => {
            AsyncClient.getTeamMember(teamId, userId);

            if (success) {
                success();
            }
        },
        (err) => {
            if (error) {
                error(err);
            }
        }
    );
}

export function addUserToTeamFromInvite(data, hash, inviteId, success, error) {
    Client.addUserToTeamFromInvite(
        data,
        hash,
        inviteId,
        (team) => {
            if (success) {
                success(team);
            }
        },
        (err) => {
            if (error) {
                error(err);
            }
        }
    );
}

export function getInviteInfo(inviteId, success, error) {
    Client.getInviteInfo(
        inviteId,
        (inviteData) => {
            if (success) {
                success(inviteData);
            }
        },
        (err) => {
            if (error) {
                error(err);
            }
        }
    );
}

export function inviteMembers(data, success, error) {
    Client.inviteMembers(
        data,
        () => {
            if (success) {
                success();
            }
        },
        (err) => {
            if (err) {
                error(err);
            }
        }
    );
}