1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/components/modal-members-label-form.js
Kevin Ansfield 650b785d3d Refactored members routes
no issue

- moved labels fetching into the controller to unify members+labels loading approaches
- removed unnecessary `deactivate` hook on members route because the label form component already rolls back the model attributes when it's destroyed
- unified non owner/admin redirect approach across members and member routes
2020-05-20 16:39:31 +01:00

88 lines
2.6 KiB
JavaScript

import ModalComponent from 'ghost-admin/components/modal-base';
import {computed} from '@ember/object';
import {resetQueryParams} from 'ghost-admin/helpers/reset-query-params';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default ModalComponent.extend({
router: service(),
notifications: service(),
model: null,
showDeleteLabelModal: false,
confirm() {},
label: computed.and('model', 'model.label'),
init() {
this._super(...arguments);
},
willDestroyElement() {
this._super(...arguments);
this.label.errors.clear();
this.label.rollbackAttributes();
},
actions: {
toggleDeleteLabelModal() {
this.label.rollbackAttributes();
this.set('showDeleteLabelModal', true);
},
validate(property) {
return this.label.validate({property});
}
},
saveTask: task(function* () {
let label = this.model && this.model.label;
let availableLabels = (this.model && this.model.labels) || [];
if (!label) {
return false;
}
try {
yield label.validate();
let duplicateLabel = availableLabels.find((existingLabel) => {
return existingLabel.name.trim().toLowerCase() === label.name.trim().toLowerCase()
&& existingLabel.slug !== label.slug;
});
if (duplicateLabel) {
label.errors.add('name', 'A label with the same name already exists');
label.hasValidated.pushObject('name');
// label.invalidate();
return false;
}
let savedLabel = yield label.save();
this.notifications.showNotification('Label saved'.htmlSafe());
this.send('closeModal');
return savedLabel;
} catch (error) {
if (error) {
this.notifications.showAPIError(error, {key: 'label.save'});
}
}
}),
deleteLabel: task(function * () {
let label = this.model && this.model.label;
if (!label) {
return false;
}
try {
yield label.destroyRecord();
let routeName = this.router.currentRouteName;
this.notifications.showNotification('Label deleted'.htmlSafe());
this.send('closeModal');
this.router.transitionTo(routeName, {queryParams: resetQueryParams(routeName)});
} catch (error) {
if (error) {
return this.notifications.showAPIError(error, {key: 'label.delete'});
}
}
})
});