FIX: Populate the Email Digest preview with the current username

This commit is contained in:
Robin Ward 2017-06-28 15:36:34 -04:00
parent 6200d7d6ab
commit f6222ff225
7 changed files with 48 additions and 47 deletions

View File

@ -2,11 +2,13 @@ import EmailPreview from 'admin/models/email-preview';
import { popupAjaxError } from 'discourse/lib/ajax-error'; import { popupAjaxError } from 'discourse/lib/ajax-error';
export default Ember.Controller.extend({ export default Ember.Controller.extend({
username: null,
lastSeen: null,
emailEmpty: Em.computed.empty('email'), emailEmpty: Ember.computed.empty('email'),
sendEmailDisabled: Em.computed.or('emailEmpty', 'sendingEmail'), sendEmailDisabled: Ember.computed.or('emailEmpty', 'sendingEmail'),
showSendEmailForm: Em.computed.notEmpty('model.html_content'), showSendEmailForm: Ember.computed.notEmpty('model.html_content'),
htmlEmpty: Em.computed.empty('model.html_content'), htmlEmpty: Ember.computed.empty('model.html_content'),
actions: { actions: {
refresh() { refresh() {
@ -14,7 +16,14 @@ export default Ember.Controller.extend({
this.set('loading', true); this.set('loading', true);
this.set('sentEmail', false); this.set('sentEmail', false);
EmailPreview.findDigest(this.get('lastSeen'), this.get('username')).then(email => {
let username = this.get('username');
if (!username) {
username = this.currentUser.get('username');
this.set('username', username);
}
EmailPreview.findDigest(username, this.get('lastSeen')).then(email => {
model.setProperties(email.getProperties('html_content', 'text_content')); model.setProperties(email.getProperties('html_content', 'text_content'));
this.set('loading', false); this.set('loading', false);
}); });
@ -28,16 +37,14 @@ export default Ember.Controller.extend({
this.set('sendingEmail', true); this.set('sendingEmail', true);
this.set('sentEmail', false); this.set('sentEmail', false);
const self = this; EmailPreview.sendDigest(this.get('username'), this.get('lastSeen'), this.get('email')).then(result => {
EmailPreview.sendDigest(this.get('lastSeen'), this.get('username'), this.get('email')).then(result => {
if (result.errors) { if (result.errors) {
bootbox.alert(result.errors); bootbox.alert(result.errors);
} else { } else {
self.set('sentEmail', true); this.set('sentEmail', true);
} }
}).catch(popupAjaxError).finally(function() { }).catch(popupAjaxError).finally(() => {
self.set('sendingEmail', false); this.set('sendingEmail', false);
}); });
} }
} }

View File

@ -1,42 +1,24 @@
import { ajax } from 'discourse/lib/ajax'; import { ajax } from 'discourse/lib/ajax';
const EmailPreview = Discourse.Model.extend({}); const EmailPreview = Discourse.Model.extend({});
export function oneWeekAgo() {
return moment().locale('en').subtract(7, 'days').format('YYYY-MM-DD');
}
EmailPreview.reopenClass({ EmailPreview.reopenClass({
findDigest: function(lastSeenAt, username) {
if (Em.isEmpty(lastSeenAt)) {
lastSeenAt = this.oneWeekAgo();
}
if (Em.isEmpty(username)) {
username = Discourse.User.current().username;
}
findDigest(username, lastSeenAt) {
return ajax("/admin/email/preview-digest.json", { return ajax("/admin/email/preview-digest.json", {
data: { last_seen_at: lastSeenAt, username: username } data: { last_seen_at: lastSeenAt || oneWeekAgo(), username }
}).then(function (result) { }).then(result => EmailPreview.create(result));
return EmailPreview.create(result);
});
}, },
sendDigest: function(lastSeenAt, username, email) { sendDigest(username, lastSeenAt, email) {
if (Em.isEmpty(lastSeenAt)) {
lastSeenAt = this.oneWeekAgo();
}
if (Em.isEmpty(username)) {
username = Discourse.User.current().username;
}
return ajax("/admin/email/send-digest.json", { return ajax("/admin/email/send-digest.json", {
data: { last_seen_at: lastSeenAt, username: username, email: email } data: { last_seen_at: lastSeenAt || oneWeekAgo(), username, email }
}); });
}, },
oneWeekAgo() {
const en = moment().locale('en');
return en.subtract(7, 'days').format('YYYY-MM-DD');
}
}); });
export default EmailPreview; export default EmailPreview;

View File

@ -1,16 +1,17 @@
import EmailPreview from 'admin/models/email-preview'; import { default as EmailPreview, oneWeekAgo } from 'admin/models/email-preview';
export default Discourse.Route.extend({ export default Discourse.Route.extend({
model() { model() {
return EmailPreview.findDigest(); return EmailPreview.findDigest(this.currentUser.get('username'));
}, },
afterModel(model) { afterModel(model) {
const controller = this.controllerFor('adminEmailPreviewDigest'); const controller = this.controllerFor('adminEmailPreviewDigest');
controller.setProperties({ controller.setProperties({
model: model, model,
lastSeen: moment().subtract(7, 'days').format('YYYY-MM-DD'), username: this.currentUser.get('username'),
lastSeen: oneWeekAgo(),
showHtml: true showHtml: true
}); });
} }

View File

@ -1,11 +1,11 @@
<p>{{i18n 'admin.email.preview_digest_desc'}}</p> <p>{{i18n 'admin.email.preview_digest_desc'}}</p>
<div class='admin-controls'> <div class='admin-controls email-preview'>
<div class='span7 controls'> <div class='span7 controls'>
<label for='last-seen'>{{i18n 'admin.email.last_seen_user'}}</label> <label for='last-seen'>{{i18n 'admin.email.last_seen_user'}}</label>
{{date-picker-past value=lastSeen id="last-seen"}} {{date-picker-past value=lastSeen id="last-seen"}}
<label>{{i18n 'admin.email.user'}}:</label> <label>{{i18n 'admin.email.user'}}:</label>
{{user-selector single="true" usernames=username}} {{user-selector single="true" usernames=username canReceiveUpdates="true"}}
<button class='btn' {{action "refresh"}}>{{i18n 'admin.email.refresh'}}</button> <button class='btn' {{action "refresh"}}>{{i18n 'admin.email.refresh'}}</button>
<div class="toggle"> <div class="toggle">
<label>{{i18n 'admin.email.format'}}</label> <label>{{i18n 'admin.email.format'}}</label>

View File

@ -2,7 +2,7 @@
import loadScript from "discourse/lib/load-script"; import loadScript from "discourse/lib/load-script";
import { default as computed, on } from "ember-addons/ember-computed-decorators"; import { default as computed, on } from "ember-addons/ember-computed-decorators";
export default Em.Component.extend({ export default Ember.Component.extend({
classNames: ["date-picker-wrapper"], classNames: ["date-picker-wrapper"],
_picker: null, _picker: null,

View File

@ -1 +1 @@
{{input type="text" class="date-picker" placeholder=placeholder}} {{input type="text" class="date-picker" placeholder=placeholder value=value}}

View File

@ -316,6 +316,17 @@ td.flaggers td {
} }
} }
.email-preview {
.ac-wrap {
.item {
margin: 0.2em 0 0 0.4em;
}
// input[type=text] {
// margin-left: 0.4em;
// }
}
}
.paste-users { .paste-users {
width: 400px; width: 400px;
height: 150px; height: 150px;