2022-01-17 13:05:27 +03:00
|
|
|
import AdminRoute from 'ghost-admin/routes/admin';
|
2022-09-09 17:20:07 +03:00
|
|
|
import ConfirmUnsavedChangesModal from '../components/modals/confirm-unsaved-changes';
|
2021-10-06 17:29:15 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2021-10-04 14:01:12 +03:00
|
|
|
|
2022-01-17 13:05:27 +03:00
|
|
|
export default class OffersRoute extends AdminRoute {
|
2022-09-09 17:20:07 +03:00
|
|
|
@service modals;
|
2021-10-06 17:29:15 +03:00
|
|
|
@service router;
|
|
|
|
|
|
|
|
_requiresBackgroundRefresh = true;
|
|
|
|
|
|
|
|
model(params) {
|
|
|
|
this._requiresBackgroundRefresh = false;
|
|
|
|
|
|
|
|
if (params.offer_id) {
|
|
|
|
return this.store.queryRecord('offer', {id: params.offer_id});
|
|
|
|
} else {
|
|
|
|
return this.store.createRecord('offer');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupController(controller, offer) {
|
|
|
|
super.setupController(...arguments);
|
2022-09-09 17:20:07 +03:00
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
if (this._requiresBackgroundRefresh) {
|
2022-10-07 19:03:45 +03:00
|
|
|
controller.fetchOfferTask.perform(offer.id);
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deactivate() {
|
|
|
|
// clean up newly created records and revert unsaved changes to existing
|
|
|
|
this.controller.offer.rollbackAttributes();
|
|
|
|
this._requiresBackgroundRefresh = true;
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:20:07 +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.controller.model.rollbackAttributes();
|
|
|
|
this.hasConfirmed = true;
|
|
|
|
return transition.retry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async confirmUnsavedChanges() {
|
|
|
|
if (this.controller.model?.hasDirtyAttributes) {
|
|
|
|
this.confirmModal = this.modals
|
|
|
|
.open(ConfirmUnsavedChangesModal)
|
|
|
|
.finally(() => {
|
|
|
|
this.confirmModal = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.confirmModal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
this.controller.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
titleToken() {
|
|
|
|
return this.controller.offer.name;
|
|
|
|
}
|
|
|
|
}
|