Ghost/ghost/admin/app/components/settings/members-email-labs.js
Kevin Ansfield e398557a75 Added sender email verification flow for newsletters
refs https://github.com/TryGhost/Team/issues/584
refs https://github.com/TryGhost/Team/issues/1498

- updated newsletter save routine in `edit-newsletter` modal to open an email confirmation modal if the API indicates one was sent
  - modal indicates that the previously set or default email will continue to be used until verified
  - response from API when saving looks like `{newsletters: [{...}], meta: {sent_email_verification: ['sender_name]}}`
  - added custom newsletter serializer and updated model so that the `meta` property returned in the response when saving posts is exposed
    - Ember Data only exposes meta on array-response find/query methods
    - https://github.com/emberjs/data/issues/2905
- added `/settings/members-email-labs/?verifyEmail=xyz` query param handling
  - opens email verification modal if param is set and instantly clears the query param to avoid problems with sticky params
  - when the modal opens it makes a `PUT /newsletters/verify-email/` request with the token in the body params, on the API side this works the same as a newsletter update request returning the fully updated newsletter record which is then pushed into the store
- removed unused from/reply address code from `<Settings::MembersEmailLabs>` component and controller
  - setting the values now handled per-newsletter in the edit-newsletter modal
  - verifying email change is handled in the members-email-labs controller
- fixed mirage not outputting pluralized root for "singular" endpoints such as POST/PUT requests to better match our API behaviour
2022-04-13 19:34:58 +01:00

142 lines
4.6 KiB
JavaScript

import Component from '@glimmer/component';
import {action} from '@ember/object';
import {inject as service} from '@ember/service';
import {tracked} from '@glimmer/tracking';
const US = {flag: '🇺🇸', name: 'US', baseUrl: 'https://api.mailgun.net/v3'};
const EU = {flag: '🇪🇺', name: 'EU', baseUrl: 'https://api.eu.mailgun.net/v3'};
export default class MembersEmailLabs extends Component {
@service config;
@service settings;
// 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];
get emailNewsletterEnabled() {
return this.settings.get('editorDefaultEmailRecipients') !== 'disabled';
}
get mailgunRegion() {
if (!this.settings.get('mailgunBaseUrl')) {
return US;
}
return [US, EU].find((region) => {
return region.baseUrl === this.settings.get('mailgunBaseUrl');
});
}
get mailgunSettings() {
return {
apiKey: this.settings.get('mailgunApiKey') || '',
domain: this.settings.get('mailgunDomain') || '',
baseUrl: this.settings.get('mailgunBaseUrl') || ''
};
}
@action
setMailgunDomain(event) {
this.settings.set('mailgunDomain', event.target.value);
if (!this.settings.get('mailgunBaseUrl')) {
this.settings.set('mailgunBaseUrl', this.mailgunRegion.baseUrl);
}
}
@action
setMailgunApiKey(event) {
this.settings.set('mailgunApiKey', event.target.value);
if (!this.settings.get('mailgunBaseUrl')) {
this.settings.set('mailgunBaseUrl', this.mailgunRegion.baseUrl);
}
}
@action
setMailgunRegion(region) {
this.settings.set('mailgunBaseUrl', region.baseUrl);
}
@action
toggleEmailTrackOpens(event) {
if (event) {
event.preventDefault();
}
this.settings.set('emailTrackOpens', !this.settings.get('emailTrackOpens'));
}
@action
toggleEmailNewsletterEnabled(event) {
if (event) {
event.preventDefault();
}
const newsletterEnabled = !this.emailNewsletterEnabled;
if (newsletterEnabled) {
this.settings.set('editorDefaultEmailRecipients', 'visibility');
} else {
this.settings.set('editorDefaultEmailRecipients', 'disabled');
this.settings.set('editorDefaultEmailRecipientsFilter', null);
}
this.recipientsSelectValue = this._getDerivedRecipientsSelectValue();
}
@action
setDefaultEmailRecipients(value) {
// Update the underlying setting properties to match the selected recipients option
if (['visibility', 'disabled'].includes(value)) {
this.settings.set('editorDefaultEmailRecipients', value);
this.settings.set('editorDefaultEmailRecipientsFilter', null);
} else {
this.settings.set('editorDefaultEmailRecipients', 'filter');
}
if (value === 'all-members') {
this.settings.set('editorDefaultEmailRecipientsFilter', 'status:free,status:-free');
}
if (value === 'paid-only') {
this.settings.set('editorDefaultEmailRecipientsFilter', 'status:-free');
}
if (value === 'none') {
this.settings.set('editorDefaultEmailRecipientsFilter', null);
}
// Update the value used to display the selected recipients option explicitly
// because it's local non-derived state
this.recipientsSelectValue = value;
}
@action
setDefaultEmailRecipientsFilter(filter) {
this.settings.set('editorDefaultEmailRecipientsFilter', filter);
}
_getDerivedRecipientsSelectValue() {
const defaultEmailRecipients = this.settings.get('editorDefaultEmailRecipients');
const defaultEmailRecipientsFilter = this.settings.get('editorDefaultEmailRecipientsFilter');
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;
}
}