Removed rogue unsaved change model for new offers

refs https://github.com/TryGhost/Team/issues/1126

- adds default properties of offer to a separate object to avoid unnecessary unsaved change modal on new offer and back
This commit is contained in:
Rishabh 2021-10-11 13:33:30 +05:30
parent bfcd0b9ebd
commit 0459a1ca23

View File

@ -5,6 +5,7 @@ import {action} from '@ember/object';
import {getSymbol} from 'ghost-admin/utils/currency'; import {getSymbol} from 'ghost-admin/utils/currency';
import {ghPriceAmount} from '../helpers/gh-price-amount'; import {ghPriceAmount} from '../helpers/gh-price-amount';
import {inject as service} from '@ember/service'; import {inject as service} from '@ember/service';
import {slugify} from '@tryghost/string';
import {task} from 'ember-concurrency-decorators'; import {task} from 'ember-concurrency-decorators';
import {timeout} from 'ember-concurrency'; import {timeout} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking'; import {tracked} from '@glimmer/tracking';
@ -46,6 +47,8 @@ export default class OffersController extends Controller {
} }
]; ];
@tracked defaultProps = null;
leaveScreenTransition = null; leaveScreenTransition = null;
constructor() { constructor() {
@ -72,6 +75,8 @@ export default class OffersController extends Controller {
get cadence() { get cadence() {
if (this.offer.tier && this.offer.cadence) { if (this.offer.tier && this.offer.cadence) {
return `${this.offer.tier.id}-${this.offer.cadence}-${this.offer.currency}`; return `${this.offer.tier.id}-${this.offer.cadence}-${this.offer.currency}`;
} else if (this.defaultProps) {
return `${this.defaultProps.tier.id}-${this.defaultProps.cadence}-${this.defaultProps.currency}`;
} }
return ''; return '';
} }
@ -107,7 +112,8 @@ export default class OffersController extends Controller {
}); });
this.cadences = cadences; this.cadences = cadences;
if (this.offer && !this.offer.tier) { if (this.offer && !this.offer.tier) {
this.updateCadence(this.cadences[0]?.name); this.defaultProps = {};
this.updateCadence(this.cadences[0]?.name, this.defaultProps);
} }
} }
@ -122,10 +128,13 @@ export default class OffersController extends Controller {
*saveTask() { *saveTask() {
let {offer} = this; let {offer} = this;
// if Cmd+S is pressed before the field loses focus make sure we're if (!offer.tier && this.defaultProps) {
// saving the intended property values this.offer.tier = {
// let scratchProps = scratchOffer.getProperties(SCRATCH_PROPS); id: this.defaultProps?.tier.id
// offer.setProperties(scratchProps); };
this.offer.cadence = this.defaultProps.cadence;
this.offer.currency = this.defaultProps.currency;
}
try { try {
yield offer.save(); yield offer.save();
@ -249,7 +258,7 @@ export default class OffersController extends Controller {
const code = this.offer?.code || ''; const code = this.offer?.code || '';
if (code) { if (code) {
const siteUrl = this.config.get('blogUrl'); const siteUrl = this.config.get('blogUrl');
return `${siteUrl}/${code}`; return `${siteUrl}/${slugify(code)}`;
} }
return ''; return '';
} }
@ -269,15 +278,15 @@ export default class OffersController extends Controller {
} }
@action @action
updateCadence(cadence) { updateCadence(cadence, offerObj) {
offerObj = offerObj || this.offer;
if (cadence) { if (cadence) {
const [tierId, tierCadence, currency] = cadence.split('-'); const [tierId, tierCadence, currency] = cadence.split('-');
this.offer.tier = { offerObj.tier = {
id: tierId id: tierId
}; };
this.offer.cadence = tierCadence; offerObj.cadence = tierCadence;
this.offer.currency = currency; offerObj.currency = currency;
const offerType = this.offer.type;
this.offertypes = [ this.offertypes = [
{ {
label: '%', label: '%',
@ -288,7 +297,6 @@ export default class OffersController extends Controller {
offertype: 'fixed' offertype: 'fixed'
} }
]; ];
this.offer.type = offerType;
} }
} }
@ -302,10 +310,6 @@ export default class OffersController extends Controller {
_saveOfferProperty(propKey, newValue) { _saveOfferProperty(propKey, newValue) {
let currentValue = this.offer[propKey]; let currentValue = this.offer[propKey];
// if (newValue && typeof newValue === 'string') {
// newValue = newValue.trim();
// }
// avoid modifying empty values and triggering inadvertant unsaved changes modals // avoid modifying empty values and triggering inadvertant unsaved changes modals
if (newValue !== false && !newValue && !currentValue) { if (newValue !== false && !newValue && !currentValue) {
return; return;