Make Account Center only usable when logged in
[blerg.git] / www / jssrc / blerg / EmailForm.js
1 enyo.kind({
2     name: 'blerg.EmailForm',
3     handlers: {
4         onLogin: "loginStatusChange"
5     },
6     components: [
7         {name: 'emailRegisterForm', components: [
8             {content: "Register an email address to get daily digest updates of your feed. We can also use this address to send you a password recovery link if you forget your password.", tag: 'p'},
9             {tag: 'form', onsubmit: 'registerEmail', components: [
10                 {kind: 'onyx.InputDecorator', classes: 'recovery', components: [
11                     {kind: 'onyx.Input', name: 'email'}
12                 ]},
13                 {kind: 'onyx.Button', content: 'Register', onclick: 'registerEmail'}
14             ]}
15         ]},
16         {name: 'emailStatus', showing: false, components: [
17             {tag: 'p', components: [
18                 {content: 'Registered email: ', tag: null},
19                 {name: 'emailOutput', tag: 'span', classes: 'email-address'},
20             ]},
21             {kind: 'onyx.Button', content: 'Deregister', onclick: 'deregisterEmail'}
22         ]},
23         {name: 'emailSent', showing: false, components: [
24             {content: 'An email has been sent to ', tag: null},
25             {name: 'emailSentAddress', tag: 'span', classes: 'email-address'},
26             {content: '.', tag: null}
27         ]}
28
29     ],
30     create: function() {
31         this.inherited(arguments);
32
33         this.fetchEmailStatus();
34     },
35     registerEmail: function(inSender, inEvent) {
36         var email = this.$.email.getValue();
37         this.$.email.setValue('');
38         this.$.email.node.blur();
39
40         var req = new enyo.Ajax({
41             url: '/aux/email/register',
42             method: 'POST',
43             postBody: {
44                 email: email
45             }
46         });
47
48         req.response(this, function(inSender, inResponse) {
49             this.$.emailSentAddress.setContent(email);
50             this.$.emailRegisterForm.hide();
51             this.$.emailSent.show();
52         });
53
54         req.error(this, function(inSender, inResponse) {
55             alert('Failed to register email.');
56         });
57
58         req.go();
59     },
60     fetchEmailStatus: function() {
61         var req = new enyo.Ajax({
62             url: '/aux/email/status'
63         });
64
65         req.response(this, function(inSender, inResponse) {
66             if (inResponse.email) {
67                 this.$.emailRegisterForm.hide();
68                 this.$.emailStatus.show();
69                 this.$.emailOutput.setContent(inResponse.email);
70             } else {
71                 this.$.emailRegisterForm.show();
72             }
73         });
74
75         req.go();
76     },
77     deregisterEmail: function(inSender, inEvent) {
78         var req = new enyo.Ajax({
79             url: '/aux/email/cancel'
80         });
81
82         req.response(this, function(inSender, inResponse) {
83             if (inResponse.status == 'success') {
84                 this.$.emailStatus.hide();
85                 this.$.emailRegisterForm.show();
86                 alert('Email notifications deregistered');
87             } else {
88                 alert('Failed to deregister');
89             }
90         });
91
92         req.error(this, function(inSender, inResponse) {
93             alert('Failed to deregister');
94         });
95
96         req.go();
97     },
98     loginStatusChange: function(inSender, inEvent) {
99         this.fetchEmailStatus();
100     }
101 });