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