-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
group.jsx
150 lines (116 loc) · 3.42 KB
/
group.jsx
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
import * as Sentry from '@sentry/react';
import {Client} from 'app/api';
import {buildUserId, buildTeamId} from 'app/utils';
import {uniqueId} from 'app/utils/guid';
import GroupActions from 'app/actions/groupActions';
import GroupStore from 'app/stores/groupStore';
export function assignToUser(params) {
const api = new Client();
const endpoint = `/issues/${params.id}/`;
const id = uniqueId();
GroupActions.assignTo(id, params.id, {
email: (params.member && params.member.email) || '',
});
const request = api.requestPromise(endpoint, {
method: 'PUT',
// Sending an empty value to assignedTo is the same as "clear",
// so if no member exists, that implies that we want to clear the
// current assignee.
data: {
assignedTo: params.user ? buildUserId(params.user.id) : '',
},
});
request
.then(data => {
GroupActions.assignToSuccess(id, params.id, data);
})
.catch(data => {
GroupActions.assignToError(id, params.id, data);
});
return request;
}
export function clearAssignment(groupId) {
const api = new Client();
const endpoint = `/issues/${groupId}/`;
const id = uniqueId();
GroupActions.assignTo(id, groupId, {
email: '',
});
const request = api.requestPromise(endpoint, {
method: 'PUT',
// Sending an empty value to assignedTo is the same as "clear"
data: {
assignedTo: '',
},
});
request
.then(data => {
GroupActions.assignToSuccess(id, groupId, data);
})
.catch(data => {
GroupActions.assignToError(id, groupId, data);
});
return request;
}
export function assignToActor({id, actor}) {
const api = new Client();
const endpoint = `/issues/${id}/`;
const guid = uniqueId();
let actorId;
GroupActions.assignTo(guid, id, {email: ''});
switch (actor.type) {
case 'user':
actorId = buildUserId(actor.id);
break;
case 'team':
actorId = buildTeamId(actor.id);
break;
default:
Sentry.withScope(scope => {
scope.setExtra('actor', actor);
Sentry.captureException('Unknown assignee type');
});
}
return api
.requestPromise(endpoint, {
method: 'PUT',
data: {assignedTo: actorId},
})
.then(data => {
GroupActions.assignToSuccess(guid, id, data);
})
.catch(data => {
GroupActions.assignToError(guid, id, data);
});
}
export function deleteNote(api, group, id, oldText) {
const index = GroupStore.removeActivity(group.id, id);
if (index === -1) {
// I dunno, the id wasn't found in the GroupStore
return Promise.reject(new Error('Group was not found in store'));
}
const promise = api.requestPromise(`/issues/${group.id}/comments/${id}/`, {
method: 'DELETE',
});
promise.catch(() =>
GroupStore.addActivity(group.id, {id, data: {text: oldText}}, index)
);
return promise;
}
export function createNote(api, group, note) {
const promise = api.requestPromise(`/issues/${group.id}/comments/`, {
method: 'POST',
data: note,
});
promise.then(data => GroupStore.addActivity(group.id, data));
return promise;
}
export function updateNote(api, group, note, id, oldText) {
GroupStore.updateActivity(group.id, id, {text: note.text});
const promise = api.requestPromise(`/issues/${group.id}/comments/${id}/`, {
method: 'PUT',
data: note,
});
promise.catch(() => GroupStore.updateActivity(group.id, id, {text: oldText}));
return promise;
}