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