-
Notifications
You must be signed in to change notification settings - Fork 209
/
resend-mixin.js
99 lines (88 loc) · 2.84 KB
/
resend-mixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Helper functions to allow a view to resend, e.g., an email or SMS.
*
* Binds a click event to the #resend DOM element which causes
* the message to be resent.
*
* Consumers must expose a `resend` function which returns a promise
* and actually resends the message.
*
* When #resend is clicked, a <viewname>.resend event is logged.
*
* @module ResendMixin
*/
import _ from 'underscore';
import Duration from 'duration';
import EmailResend from '../../models/email-resend';
import preventDefaultThen from '../decorators/prevent_default_then';
const t = msg => msg;
const SHOW_RESEND_IN_MS = new Duration('5m').milliseconds();
/**
* Creates the mixin to be used by views.
*
* @param {Object} [options={}] options
* @param {String} successMessage success message to display when complete.
* Defaults to `Email resent`. If falsey, no message is displayed.
* @return {Function} the mixin to be consumed by views.
*/
export default function(options = {}) {
const { successMessage } = _.defaults(options, {
successMessage: t(
'Email resent. Add accounts@firefox.com to your contacts to ensure a smooth delivery.'
),
});
return {
initialize() {
this._emailResend = new EmailResend();
this.listenTo(
this._emailResend,
'maxTriesReached',
this._onMaxTriesReached
);
},
events: {
'click #resend': preventDefaultThen('_resend'),
},
_resend() {
return Promise.resolve().then(() => {
this.logViewEvent('resend');
this._updateSuccessMessage();
// The button is hidden after the fourth click for 5 minutes, then
// start the process again.
this._emailResend.incrementRequestCount();
if (this._emailResend.shouldResend()) {
return this.resend()
.then(() => {
if (successMessage) {
this.displaySuccess(successMessage);
}
})
.catch(err => this.displayError(err));
}
});
},
_updateSuccessMessage() {
// if a success message is already being displayed, shake it.
var successEl = this.$('.success:visible');
if (successEl) {
successEl
.one('animationend', () => {
successEl.removeClass('shake');
})
.addClass('shake');
}
},
_onMaxTriesReached() {
// Hide the button after too many attempts. Redisplay button after a delay.
this.logViewEvent('too_many_attempts');
this.$('#resend').hide();
this.setTimeout(() => {
this._emailResend.reset();
this.$('#resend').show();
}, SHOW_RESEND_IN_MS);
},
};
}