-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathsettings.js
65 lines (55 loc) · 1.96 KB
/
settings.js
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
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { task } from 'ember-concurrency';
export default class CrateSettingsController extends Controller {
@service notifications;
crate = null;
username = '';
addOwnerTask = task(async () => {
const username = this.username;
try {
await this.crate.inviteOwner(username);
if (username.includes(':')) {
this.notifications.success(`Team ${username} was added as a crate owner`);
this.crate.owner_team.reload();
} else {
this.notifications.success(`An invite has been sent to ${username}`);
}
} catch (error) {
let detail = error.errors?.[0]?.detail;
if (detail && !detail.startsWith('{')) {
this.notifications.error(`Error sending invite: ${detail}`);
} else {
this.notifications.error('Error sending invite');
}
}
});
removeOwnerTask = task(async owner => {
try {
await this.crate.removeOwner(owner.get('username'));
if (owner.kind === 'team') {
this.notifications.success(`Team ${owner.get('display_name')} removed as crate owner`);
let owner_team = await this.crate.owner_team;
removeOwner(owner_team, owner);
} else {
this.notifications.success(`User ${owner.get('username')} removed as crate owner`);
let owner_user = await this.crate.owner_user;
removeOwner(owner_user, owner);
}
} catch (error) {
let subject = owner.kind === 'team' ? `team ${owner.get('display_name')}` : `user ${owner.get('username')}`;
let message = `Failed to remove the ${subject} as crate owner`;
let detail = error.errors?.[0]?.detail;
if (detail && !detail.startsWith('{')) {
message += `: ${detail}`;
}
this.notifications.error(message);
}
});
}
function removeOwner(owners, target) {
let idx = owners.indexOf(target);
if (idx !== -1) {
owners.splice(idx, 1);
}
}