This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
communication_preferences.js
117 lines (100 loc) · 3.49 KB
/
communication_preferences.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* 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/. */
define([
'cocktail',
'lib/xss',
'lib/constants',
'lib/marketing-email-errors',
'lib/metrics',
'views/base',
'views/form',
'views/mixins/back-mixin',
'views/mixins/settings-mixin',
'views/mixins/checkbox-mixin',
'views/mixins/loading-mixin',
'stache!templates/settings/communication_preferences'
],
function (Cocktail, Xss, Constants, MarketingEmailErrors, Metrics, BaseView, FormView,
BackMixin, SettingsMixin, CheckboxMixin, LoadingMixin, Template) {
'use strict';
var NEWSLETTER_ID = Constants.MARKETING_EMAIL_NEWSLETTER_ID;
var t = BaseView.t;
var View = FormView.extend({
template: Template,
className: 'communication-preferences',
getMarketingEmailPrefs: function () {
var self = this;
if (! self._marketingEmailPrefs) {
self._marketingEmailPrefs =
self.getSignedInAccount().getMarketingEmailPrefs();
}
return self._marketingEmailPrefs;
},
beforeRender: function () {
var self = this;
var emailPrefs = self.getMarketingEmailPrefs();
return emailPrefs.fetch()
.fail(function (err) {
if (MarketingEmailErrors.is(err, 'UNKNOWN_EMAIL')) {
// user has not yet opted in to Basket yet. Ignore.
return;
}
if (MarketingEmailErrors.is(err, 'UNKNOWN_ERROR')) {
self._error = self.translateError(MarketingEmailErrors.toError('SERVICE_UNAVAILABLE'));
} else {
self._error = self.translateError(err);
}
err = self._normalizeError(err);
var errorString = Metrics.prototype.errorToId(err);
err.code = err.code || 'unknown';
// Add status code to metrics data, to differentiate between
// 400 and 500
errorString = errorString + '.' + err.code;
self.logEvent(errorString);
});
},
context: function () {
var self = this;
var emailPrefs = this.getMarketingEmailPrefs();
var isOptedIn = emailPrefs.isOptedIn(NEWSLETTER_ID);
self.logScreenEvent('newsletter.optin.' + String(isOptedIn));
return {
isOptedIn: isOptedIn,
// preferencesURL is only available if the user is already
// registered with basket.
preferencesUrl: Xss.href(emailPrefs.get('preferencesUrl')),
error: self._error
};
},
submit: function () {
var self = this;
var emailPrefs = self.getMarketingEmailPrefs();
return self.setOptInStatus(NEWSLETTER_ID, ! emailPrefs.isOptedIn(NEWSLETTER_ID));
},
setOptInStatus: function (newsletterId, isOptedIn) {
var self = this;
var method = isOptedIn ? 'optIn' : 'optOut';
self.logScreenEvent(method);
return self.getMarketingEmailPrefs()[method](newsletterId)
.then(function () {
self.logScreenEvent(method + '.success');
var successMessage = isOptedIn ?
t('Subscribed successfully') :
t('Unsubscribed successfully');
self.ephemeralMessages.set('success', successMessage);
return self.render();
}, function (err) {
self.displayError(err);
});
}
});
Cocktail.mixin(
View,
BackMixin,
SettingsMixin,
CheckboxMixin,
LoadingMixin
);
return View;
});