Ghost/ghost/admin/app/routes/settings/newsletters/edit-newsletter.js
Simon Backx 19249bdae5 Renamed members-email-labs to newsletters and removed unused files
no issue

We still had some old components and templates from the `members-email` settings that were only were used when the (now GA) `multipleNewsletters` flag was disabled. I’ve removed them and renamed all routes and components from `members-email` and `members-email-labs` to just `newsletters` because it was sometimes hard to understand if a given components was still used or not when fixing issues.
2022-05-05 13:37:53 +02:00

98 lines
2.5 KiB
JavaScript

import AdminRoute from 'ghost-admin/routes/admin';
import ConfirmUnsavedChangesModal from '../../../components/modals/confirm-unsaved-changes';
import EditNewsletterModal from '../../../components/modals/newsletters/edit';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
export default class EditNewsletterRoute extends AdminRoute {
@service modals;
@service router;
@service store;
newsletterModal = null;
model(params) {
return this.store.find('newsletter', params.newsletter_id);
}
setupController(controller, model) {
this.newsletterModal?.close();
this.newsletterModal = this.modals.open(EditNewsletterModal, {
newsletter: model,
afterSave: this.afterSave
}, {
beforeClose: this.beforeModalClose
});
}
@action
afterSave() {
this.router.transitionTo('settings.newsletters');
}
deactivate() {
this.isLeaving = true;
this.newsletterModal?.close();
this.isLeaving = false;
this.newsletterModal = null;
this.confirmModal = null;
this.hasConfirmed = false;
}
@action
async willTransition(transition) {
if (this.hasConfirmed) {
return true;
}
transition.abort();
// wait for any existing confirm modal to be closed before allowing transition
if (this.confirmModal) {
return;
}
const shouldLeave = await this.confirmUnsavedChanges();
if (shouldLeave) {
this.hasConfirmed = true;
return transition.retry();
}
}
async confirmUnsavedChanges() {
const newsletter = this.newsletterModal?._data.newsletter;
if (newsletter?.hasDirtyAttributes) {
this.confirmModal = this.modals.open(ConfirmUnsavedChangesModal)
.then((discardChanges) => {
if (discardChanges === true) {
newsletter.rollbackAttributes();
}
return discardChanges;
}).finally(() => {
this.confirmModal = null;
});
return this.confirmModal;
}
return true;
}
@action
async beforeModalClose() {
const shouldLeave = await this.confirmUnsavedChanges();
if (shouldLeave && !this.isLeaving) {
this.router.transitionTo('settings.newsletters');
return true;
}
return false;
}
}