2021-10-06 17:29:15 +03:00
|
|
|
import Controller, {inject as controller} from '@ember/controller';
|
2021-10-04 14:01:12 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-10-04 16:00:41 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-10-04 14:01:12 +03:00
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
export default class OffersController extends Controller {
|
|
|
|
@controller offers;
|
2021-10-04 16:00:41 +03:00
|
|
|
@service config;
|
|
|
|
@service settings;
|
|
|
|
@service store;
|
2021-10-06 17:29:15 +03:00
|
|
|
@service notifications;
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@tracked cadences = [];
|
|
|
|
@tracked products = [];
|
2021-10-05 15:34:19 +03:00
|
|
|
@tracked durations = [
|
|
|
|
{
|
|
|
|
label: 'Forever',
|
|
|
|
duration: 'forever'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Once',
|
|
|
|
duration: 'once'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Multiple months',
|
|
|
|
duration: 'multiple-months'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
@tracked selectedDuration = 'forever';
|
2021-10-04 16:00:41 +03:00
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
leaveScreenTransition = null;
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.setup();
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
get offer() {
|
|
|
|
return this.model;
|
|
|
|
}
|
|
|
|
|
|
|
|
set offer(offer) {
|
|
|
|
this.model = offer;
|
|
|
|
}
|
|
|
|
|
|
|
|
get scratchOffer() {
|
|
|
|
return {
|
|
|
|
...this.offer
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
get cadence() {
|
|
|
|
if (this.offer.tier && this.offer.cadence) {
|
|
|
|
return `${this.offer.tier.id}-${this.offer.cadence}`;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tasks -------------------------------------------------------------------
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*fetchProducts() {
|
2021-10-06 17:29:15 +03:00
|
|
|
this.products = yield this.store.query('product', {include: 'monthly_price,yearly_price'});
|
|
|
|
const cadences = [{
|
|
|
|
label: 'Select',
|
|
|
|
name: ''
|
|
|
|
}];
|
2021-10-04 16:00:41 +03:00
|
|
|
this.products.forEach((product) => {
|
|
|
|
cadences.push({
|
|
|
|
label: `${product.name} - Monthly`,
|
2021-10-06 17:29:15 +03:00
|
|
|
name: `${product.id}-month`
|
2021-10-04 16:00:41 +03:00
|
|
|
});
|
|
|
|
cadences.push({
|
|
|
|
label: `${product.name} - Yearly`,
|
2021-10-06 17:29:15 +03:00
|
|
|
name: `${product.id}-year`
|
2021-10-04 16:00:41 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
this.cadences = cadences;
|
|
|
|
}
|
|
|
|
|
2021-10-05 16:03:39 +03:00
|
|
|
@task
|
|
|
|
copyOfferUrl() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*saveTask() {
|
|
|
|
let {offer} = this;
|
|
|
|
|
|
|
|
// if Cmd+S is pressed before the field loses focus make sure we're
|
|
|
|
// saving the intended property values
|
|
|
|
// let scratchProps = scratchOffer.getProperties(SCRATCH_PROPS);
|
|
|
|
// offer.setProperties(scratchProps);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield offer.save();
|
|
|
|
|
|
|
|
// replace 'offer.new' route with 'offer' route
|
|
|
|
this.replaceRoute('offer', offer);
|
|
|
|
|
|
|
|
return offer;
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
this.notifications.showAPIError(error, {key: 'offer.save'});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@task
|
|
|
|
*fetchOfferTask(offerId) {
|
|
|
|
this.isLoading = true;
|
|
|
|
|
|
|
|
this.offer = yield this.store.queryRecord('offer', {
|
|
|
|
id: offerId
|
|
|
|
});
|
|
|
|
|
|
|
|
this.isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
return this.saveTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
leaveScreen() {
|
|
|
|
this.offer.rollbackAttributes();
|
|
|
|
return this.leaveScreenTransition.retry();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleUnsavedChangesModal(transition) {
|
|
|
|
let leaveTransition = this.leaveScreenTransition;
|
|
|
|
|
|
|
|
if (!transition && this.showUnsavedChangesModal) {
|
|
|
|
this.leaveScreenTransition = null;
|
|
|
|
this.showUnsavedChangesModal = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.leaveScreenTransition = transition;
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.showUnsavedChangesModal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 16:00:41 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
|
|
|
this.fetchProducts.perform();
|
2021-10-06 17:29:15 +03:00
|
|
|
// this.fetchOfferTask.perform();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setProperty(propKey, value) {
|
|
|
|
this._saveOfferProperty(propKey, value);
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDiscountType(discountType) {
|
2021-10-06 17:29:15 +03:00
|
|
|
this._saveOfferProperty('type', discountType);
|
|
|
|
// this.offer.discountType = discountType;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDiscountAmount(e) {
|
|
|
|
this._saveOfferProperty('amount', e.target.value);
|
|
|
|
// this.offer.discountAmount = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setOfferName(e) {
|
|
|
|
this._saveOfferProperty('name', e.target.value);
|
|
|
|
// this.offer.name = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setPortalTitle(e) {
|
|
|
|
this._saveOfferProperty('displayTitle', e.target.value);
|
|
|
|
// this.offer.portalTitle = e.target.value;
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
|
|
|
|
2021-10-05 15:34:19 +03:00
|
|
|
@action
|
2021-10-06 17:29:15 +03:00
|
|
|
setPortalDescription(e) {
|
|
|
|
this._saveOfferProperty('displayDescription', e.target.value);
|
|
|
|
// this.offer.portalDescription = e.target.value;
|
2021-10-05 15:34:19 +03:00
|
|
|
}
|
|
|
|
|
2021-10-04 14:01:12 +03:00
|
|
|
@action
|
2021-10-06 17:29:15 +03:00
|
|
|
setOfferCode(e) {
|
|
|
|
this._saveOfferProperty('code', e.target.value);
|
|
|
|
// this.offer.code = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDurationInMonths(e) {
|
|
|
|
this._saveOfferProperty('durationInMonths', e.target.value);
|
|
|
|
// this.offer.durationInMonths = e.target.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateCadence(cadence) {
|
|
|
|
const [tierId, tierCadence] = cadence.split('-');
|
|
|
|
this.offer.tier = {
|
|
|
|
id: tierId
|
|
|
|
};
|
|
|
|
this.offer.cadence = tierCadence;
|
|
|
|
// this._saveOfferProperty('cadence', cadence);
|
|
|
|
// this.offer.cadence = cadence;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
updateDuration(duration) {
|
|
|
|
this._saveOfferProperty('duration', duration);
|
|
|
|
// this.offer.duration = duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private -----------------------------------------------------------------
|
|
|
|
|
|
|
|
_saveOfferProperty(propKey, newValue) {
|
|
|
|
let currentValue = this.offer[propKey];
|
|
|
|
|
|
|
|
// if (newValue && typeof newValue === 'string') {
|
|
|
|
// newValue = newValue.trim();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// avoid modifying empty values and triggering inadvertant unsaved changes modals
|
|
|
|
if (newValue !== false && !newValue && !currentValue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.offer[propKey] = newValue;
|
2021-10-04 16:00:41 +03:00
|
|
|
}
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|