2021-04-10 10:16:16 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-04-10 10:16:16 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
const DEFAULT_ROLE_NAME = 'Contributor';
|
|
|
|
|
|
|
|
export default class GhRoleSelectionComponent extends Component {
|
|
|
|
@service limit;
|
|
|
|
@service notifications;
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
@tracked roles = [];
|
|
|
|
@tracked limitErrorMessage = null;
|
|
|
|
|
|
|
|
@action
|
|
|
|
async setRole(roleName) {
|
|
|
|
const role = this.roles.findBy('name', roleName);
|
|
|
|
this.args.setRole(role);
|
2021-04-12 13:15:09 +03:00
|
|
|
return this.validateRole(role);
|
2021-04-10 10:16:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchRolesTask() {
|
|
|
|
const roles = yield this.store.query('role', {permissions: 'assign'});
|
|
|
|
const defaultRole = roles.findBy('name', DEFAULT_ROLE_NAME);
|
|
|
|
|
|
|
|
this.roles = roles;
|
|
|
|
|
2021-04-12 13:27:37 +03:00
|
|
|
if (!this.args.selected && defaultRole) {
|
2021-04-10 10:16:16 +03:00
|
|
|
this.args.setRole(defaultRole);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async validateRole(role) {
|
2021-04-20 15:39:03 +03:00
|
|
|
if (role.name === 'Contributor') {
|
|
|
|
this.args.onValidationSuccess?.();
|
|
|
|
}
|
|
|
|
|
2021-04-10 10:16:16 +03:00
|
|
|
if (role.name !== 'Contributor'
|
2021-04-20 15:39:03 +03:00
|
|
|
&& this.limit.limiter
|
|
|
|
&& this.limit.limiter.isLimited('staff')
|
|
|
|
) {
|
2021-04-10 10:16:16 +03:00
|
|
|
try {
|
|
|
|
await this.limit.limiter.errorIfWouldGoOverLimit('staff');
|
|
|
|
|
|
|
|
this.limitErrorMessage = null;
|
2021-04-20 15:39:03 +03:00
|
|
|
this.args.onValidationSuccess?.();
|
2021-04-10 10:16:16 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error.errorType === 'HostLimitError') {
|
|
|
|
this.limitErrorMessage = error.message;
|
2021-04-20 15:39:03 +03:00
|
|
|
this.args.onValidationFailure?.(this.limitErrorMessage);
|
2021-04-10 10:16:16 +03:00
|
|
|
} else {
|
|
|
|
this.notifications.showAPIError(error, {key: 'staff.limit'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.limitErrorMessage = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|