Ghost/ghost/admin/app/components/modals/members/bulk-remove-label.js
Kevin Ansfield efb7bc4c3b Refactored modal for bulk removing a label to members
refs https://github.com/TryGhost/Team/issues/559

Members controller was becoming bloated and difficult to follow due to catering for many different concerns.

- converted old modal to newer promise-modal style
- pulled full label-removing logic out of the members controller and into the modal so logic is contained in one place
2022-02-09 21:58:59 +00:00

61 lines
1.6 KiB
JavaScript

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
export default class MembersBulkRemoveLabelModal extends Component {
@service ajax;
@service ghostPaths;
@tracked error;
@tracked response;
@tracked selectedLabel;
get isDisabled() {
return !this.args.data.query || !this.selectedLabel;
}
get hasRun() {
return !!(this.error || this.response);
}
@action
setLabel(label) {
this.selectedLabel = label;
}
@task({drop: true})
*removeLabelTask() {
try {
const query = new URLSearchParams(this.args.data.query);
const removeLabelUrl = `${this.ghostPaths.url.api('members/bulk')}?${query}`;
const response = yield this.ajax.put(removeLabelUrl, {
data: {
bulk: {
action: 'removeLabel',
meta: {
label: {
id: this.selectedLabel
}
}
}
}
});
this.args.data.onComplete?.();
this.response = response?.bulk?.meta;
return true;
} catch (e) {
if (e.payload?.errors) {
this.error = e.payload.errors[0].message;
} else {
this.error = 'An unknown error occurred. Please try again.';
}
throw e;
}
}
}