Ghost/ghost/admin/app/routes/settings/newsletters/edit-newsletter.js

98 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-04-04 21:26:49 +03:00
import AdminRoute from 'ghost-admin/routes/admin';
import ConfirmUnsavedChangesModal from '../../../components/modals/confirm-unsaved-changes';
import EditNewsletterModal from '../../../components/modals/newsletters/edit';
2022-04-04 21:26:49 +03:00
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');
}
2022-04-04 21:26:49 +03:00
deactivate() {
this.isLeaving = true;
this.newsletterModal?.close();
this.isLeaving = false;
this.newsletterModal = null;
this.confirmModal = null;
this.hasConfirmed = false;
2022-04-04 21:26:49 +03:00
}
@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;
2022-04-04 21:26:49 +03:00
}
@action
async beforeModalClose() {
const shouldLeave = await this.confirmUnsavedChanges();
if (shouldLeave && !this.isLeaving) {
this.router.transitionTo('settings.newsletters');
return true;
2022-04-04 21:26:49 +03:00
}
return false;
2022-04-04 21:26:49 +03:00
}
}