generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
actions.ts
126 lines (103 loc) · 3.56 KB
/
actions.ts
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
import {AnyAction, Dispatch} from 'redux';
import {getCurrentChannel} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {GetStateFunc} from 'mattermost-redux/types/actions';
import {Client4} from 'mattermost-redux/client';
import {IntegrationTypes} from 'mattermost-redux/action_types';
import ActionTypes from 'action_types/';
import {BadgeID} from 'types/badges';
import {RHSState} from 'types/general';
/**
* Stores`showRHSPlugin` action returned by
* registerRightHandSidebarComponent in plugin initialization.
*/
export function setShowRHSAction(showRHSPluginAction: () => void) {
return {
type: ActionTypes.RECEIVED_SHOW_RHS_ACTION,
showRHSPluginAction,
};
}
export function setRHSUser(userID: string | null) {
return {
type: ActionTypes.RECEIVED_RHS_USER,
data: userID,
};
}
export function setRHSBadge(badgeID: BadgeID | null) {
return {
type: ActionTypes.RECEIVED_RHS_BADGE,
data: badgeID,
};
}
export function setRHSView(view: RHSState) {
return {
type: ActionTypes.RECEIVED_RHS_VIEW,
data: view,
};
}
export function setTriggerId(triggerId: string) {
return {
type: IntegrationTypes.RECEIVED_DIALOG_TRIGGER_ID,
data: triggerId,
};
}
export function openGrant(user?: string, badge?: string) {
return (dispatch: Dispatch<AnyAction>, getState: GetStateFunc) => {
let command = '/badges grant';
if (user) {
command += ` --user ${user}`;
}
if (badge) {
command += ` --badge ${badge}`;
}
clientExecuteCommand(dispatch, getState, command);
return {data: true};
};
}
export function openCreateType() {
return (dispatch: Dispatch<AnyAction>, getState: GetStateFunc) => {
const command = '/badges create type';
clientExecuteCommand(dispatch, getState, command);
return {data: true};
};
}
export function openCreateBadge() {
return (dispatch: Dispatch<AnyAction>, getState: GetStateFunc) => {
const command = '/badges create badge';
clientExecuteCommand(dispatch, getState, command);
return {data: true};
};
}
export function openAddSubscription() {
return (dispatch: Dispatch<AnyAction>, getState: GetStateFunc) => {
const command = '/badges subscription create';
clientExecuteCommand(dispatch, getState, command);
return {data: true};
};
}
export function openRemoveSubscription() {
return (dispatch: Dispatch<AnyAction>, getState: GetStateFunc) => {
const command = '/badges subscription remove';
clientExecuteCommand(dispatch, getState, command);
return {data: true};
};
}
export async function clientExecuteCommand(dispatch: Dispatch<AnyAction>, getState: GetStateFunc, command: string) {
let currentChannel = getCurrentChannel(getState());
const currentTeamId = getCurrentTeamId(getState());
// Default to town square if there is no current channel (i.e., if Mattermost has not yet loaded)
if (!currentChannel) {
currentChannel = await Client4.getChannelByName(currentTeamId, 'town-square');
}
const args = {
channel_id: currentChannel?.id,
team_id: currentTeamId,
};
try {
//@ts-ignore Typing in mattermost-redux is wrong
const data = await Client4.executeCommand(command, args);
dispatch(setTriggerId(data?.trigger_id));
} catch (error) {
console.error(error); //eslint-disable-line no-console
}
}