Ghost/ghost/admin/app/components/gh-role-selection.js
Kevin Ansfield ecf82fbfe8 Fixed invite staff modal button not reflecting validation state (#1924)
no issue

- when the role selection was extracted to an external component the limit validation was also extracted but had no way of feeding back to the consumer
- added `onValidationSuccess` and `onValidationFailure` arguments to the role selection component to allow validation feedback to the consumer
- updated staff invite modal with actions to update state based on validation so the modal's buttons can update accordingly
2021-04-20 13:39:03 +01:00

63 lines
1.8 KiB
JavaScript

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency-decorators';
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);
return this.validateRole(role);
}
@task
*fetchRolesTask() {
const roles = yield this.store.query('role', {permissions: 'assign'});
const defaultRole = roles.findBy('name', DEFAULT_ROLE_NAME);
this.roles = roles;
if (!this.args.selected && defaultRole) {
this.args.setRole(defaultRole);
}
}
async validateRole(role) {
if (role.name === 'Contributor') {
this.args.onValidationSuccess?.();
}
if (role.name !== 'Contributor'
&& this.limit.limiter
&& this.limit.limiter.isLimited('staff')
) {
try {
await this.limit.limiter.errorIfWouldGoOverLimit('staff');
this.limitErrorMessage = null;
this.args.onValidationSuccess?.();
} catch (error) {
if (error.errorType === 'HostLimitError') {
this.limitErrorMessage = error.message;
this.args.onValidationFailure?.(this.limitErrorMessage);
} else {
this.notifications.showAPIError(error, {key: 'staff.limit'});
}
}
} else {
this.limitErrorMessage = null;
}
}
}