2022-04-01 17:32:24 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2022-03-31 13:06:21 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-04-01 17:32:24 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2022-03-31 13:06:21 +03:00
|
|
|
|
|
|
|
const US = {flag: '🇺🇸', name: 'US', baseUrl: 'https://api.mailgun.net/v3'};
|
|
|
|
const EU = {flag: '🇪🇺', name: 'EU', baseUrl: 'https://api.eu.mailgun.net/v3'};
|
|
|
|
|
2022-05-05 14:36:16 +03:00
|
|
|
export default class Newsletters extends Component {
|
2022-03-31 13:06:21 +03:00
|
|
|
@service config;
|
|
|
|
@service settings;
|
2022-09-15 16:48:22 +03:00
|
|
|
@service feature;
|
2022-03-31 13:06:21 +03:00
|
|
|
|
2022-04-01 17:32:24 +03:00
|
|
|
// set recipientsSelectValue as a static property because within this
|
|
|
|
// component's lifecycle it's not always derived from the settings values.
|
|
|
|
// e.g. can be set to "segment" when the filter is empty which is not derivable
|
|
|
|
// from settings as it would equate to "none"
|
|
|
|
@tracked recipientsSelectValue = this._getDerivedRecipientsSelectValue();
|
|
|
|
|
|
|
|
mailgunRegions = [US, EU];
|
|
|
|
|
2022-03-31 13:06:21 +03:00
|
|
|
get emailNewsletterEnabled() {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings.editorDefaultEmailRecipients !== 'disabled';
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get mailgunRegion() {
|
2022-10-07 16:23:21 +03:00
|
|
|
if (!this.settings.mailgunBaseUrl) {
|
2022-03-31 13:06:21 +03:00
|
|
|
return US;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [US, EU].find((region) => {
|
2022-10-07 16:23:21 +03:00
|
|
|
return region.baseUrl === this.settings.mailgunBaseUrl;
|
2022-03-31 13:06:21 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get mailgunSettings() {
|
|
|
|
return {
|
2022-10-07 16:23:21 +03:00
|
|
|
apiKey: this.settings.mailgunApiKey || '',
|
|
|
|
domain: this.settings.mailgunDomain || '',
|
|
|
|
baseUrl: this.settings.mailgunBaseUrl || ''
|
2022-03-31 13:06:21 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setMailgunDomain(event) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.mailgunDomain = event.target.value;
|
|
|
|
if (!this.settings.mailgunBaseUrl) {
|
|
|
|
this.settings.mailgunBaseUrl = this.mailgunRegion.baseUrl;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setMailgunApiKey(event) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.mailgunApiKey = event.target.value;
|
|
|
|
if (!this.settings.mailgunBaseUrl) {
|
|
|
|
this.settings.mailgunBaseUrl = this.mailgunRegion.baseUrl;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setMailgunRegion(region) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.mailgunBaseUrl = region.baseUrl;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
toggleEmailTrackOpens(event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.emailTrackOpens = !this.settings.emailTrackOpens;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-15 16:48:22 +03:00
|
|
|
@action
|
|
|
|
toggleEmailTrackClicks(event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.emailTrackClicks = !this.settings.emailTrackClicks;
|
2022-09-15 16:48:22 +03:00
|
|
|
}
|
|
|
|
|
2022-03-31 13:06:21 +03:00
|
|
|
@action
|
|
|
|
toggleEmailNewsletterEnabled(event) {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
|
|
|
|
const newsletterEnabled = !this.emailNewsletterEnabled;
|
|
|
|
|
|
|
|
if (newsletterEnabled) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipients = 'visibility';
|
2022-03-31 13:06:21 +03:00
|
|
|
} else {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipients = 'disabled';
|
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = null;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:32:24 +03:00
|
|
|
this.recipientsSelectValue = this._getDerivedRecipientsSelectValue();
|
2022-05-18 18:24:38 +03:00
|
|
|
|
|
|
|
// Force a save when the toggle is changed
|
|
|
|
// this is required
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
this.settings.save().catch(console.error);
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDefaultEmailRecipients(value) {
|
|
|
|
// Update the underlying setting properties to match the selected recipients option
|
|
|
|
|
|
|
|
if (['visibility', 'disabled'].includes(value)) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipients = value;
|
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = null;
|
2022-03-31 13:06:21 +03:00
|
|
|
} else {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipients = 'filter';
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value === 'all-members') {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = 'status:free,status:-free';
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value === 'paid-only') {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = 'status:-free';
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value === 'none') {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = null;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the value used to display the selected recipients option explicitly
|
|
|
|
// because it's local non-derived state
|
2022-04-01 17:32:24 +03:00
|
|
|
this.recipientsSelectValue = value;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setDefaultEmailRecipientsFilter(filter) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.editorDefaultEmailRecipientsFilter = filter;
|
2022-03-31 13:06:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_getDerivedRecipientsSelectValue() {
|
2022-10-07 16:23:21 +03:00
|
|
|
const defaultEmailRecipients = this.settings.editorDefaultEmailRecipients;
|
|
|
|
const defaultEmailRecipientsFilter = this.settings.editorDefaultEmailRecipientsFilter;
|
2022-03-31 13:06:21 +03:00
|
|
|
|
|
|
|
if (defaultEmailRecipients === 'filter') {
|
|
|
|
if (defaultEmailRecipientsFilter === 'status:free,status:-free') {
|
|
|
|
return 'all-members';
|
|
|
|
} else if (defaultEmailRecipientsFilter === 'status:-free') {
|
|
|
|
return 'paid-only';
|
|
|
|
} else if (defaultEmailRecipientsFilter === null) {
|
|
|
|
return 'none';
|
|
|
|
} else {
|
|
|
|
return 'segment';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultEmailRecipients;
|
|
|
|
}
|
|
|
|
}
|