Browse Source

And we're set!

Brendan Abolivier 8 years ago
parent
commit
e5bdc0f929
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
2 changed files with 52 additions and 7 deletions
  1. 37
    5
      front/form.js
  2. 15
    2
      server.js

+ 37
- 5
front/form.js View File

7
 
7
 
8
 var server  = getServer();
8
 var server  = getServer();
9
 var xhrSend = new XMLHttpRequest();
9
 var xhrSend = new XMLHttpRequest();
10
-xhrSend.onreadystatechange = function() {
11
-    if (xhrSend.readyState == XMLHttpRequest.DONE) {
12
-        console.log(xhrSend.response);
13
-    }
14
-};
15
 
10
 
16
 
11
 
17
 // Returns the server's base URI based on the user's script tag
12
 // Returns the server's base URI based on the user's script tag
42
     // Set the form's behaviour
37
     // Set the form's behaviour
43
     el.setAttribute('onsubmit', 'sendForm(); return false;');
38
     el.setAttribute('onsubmit', 'sendForm(); return false;');
44
     
39
     
40
+    // Add an empty paragraph for status
41
+    var status = document.createElement('p');
42
+    status.setAttribute('id', 'form_status');
43
+    el.appendChild(status);
44
+    
45
     var input = {
45
     var input = {
46
         name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
46
         name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
47
         addr: getField(items.addr, 'Your e-mail address', true, 'input'),
47
         addr: getField(items.addr, 'Your e-mail address', true, 'input'),
59
     // Adding submit button
59
     // Adding submit button
60
     
60
     
61
     el.appendChild(getSubmitButton('form_subm', 'Send the mail'));
61
     el.appendChild(getSubmitButton('form_subm', 'Send the mail'));
62
+    
63
+    // Setting the XHR callback
64
+    
65
+    xhrSend.onreadystatechange = function() {
66
+        if(xhrSend.readyState == XMLHttpRequest.DONE) {
67
+            let status = document.getElementById('form_status');
68
+            status.setAttribute('class', '');
69
+            if(xhrSend.status === 200) {
70
+                cleanForm();
71
+                status.setAttribute('class', 'success');
72
+                status.innerHTML = 'Your message has been sent.';
73
+            } else {
74
+                status.setAttribute('class', 'failure');
75
+                status.innerHTML = 'An error happened while sending your message, please retry later.';
76
+            }
77
+        }
78
+    };
62
 }
79
 }
63
 
80
 
64
 
81
 
147
 // Send form data through the XHR object
164
 // Send form data through the XHR object
148
 // return: nothing
165
 // return: nothing
149
 function sendForm() {
166
 function sendForm() {
167
+    // Clear status
168
+    let status = document.getElementById('form_status');
169
+    status.setAttribute('class', 'sending');
170
+    status.innerHTML = 'Sending the e-mail';
171
+    
150
     xhrSend.open('POST', server + '/send');
172
     xhrSend.open('POST', server + '/send');
151
     xhrSend.setRequestHeader('Content-Type', 'application/json');
173
     xhrSend.setRequestHeader('Content-Type', 'application/json');
152
     xhrSend.send(JSON.stringify(getFormData()));
174
     xhrSend.send(JSON.stringify(getFormData()));
162
         subj: document.getElementById(items.subj + '_input').value,
184
         subj: document.getElementById(items.subj + '_input').value,
163
         text: document.getElementById(items.text + '_textarea').value
185
         text: document.getElementById(items.text + '_textarea').value
164
     }
186
     }
187
+}
188
+
189
+
190
+// Empties the form fields
191
+// return: nothing
192
+function cleanForm() {
193
+    document.getElementById(items.name + '_input').value = '';
194
+    document.getElementById(items.addr + '_input').value = '';
195
+    document.getElementById(items.subj + '_input').value = '';
196
+    document.getElementById(items.text + '_textarea').value = '';
165
 }
197
 }

+ 15
- 2
server.js View File

14
     date: true
14
     date: true
15
 });
15
 });
16
 
16
 
17
+
17
 // nodemailer initial configuration
18
 // nodemailer initial configuration
18
 var transporter = nodemailer.createTransport(settings.mailserver);
19
 var transporter = nodemailer.createTransport(settings.mailserver);
19
 
20
 
21
+
20
 // Serve static (JS + HTML) files
22
 // Serve static (JS + HTML) files
21
 app.use(express.static('front'));
23
 app.use(express.static('front'));
22
 // Body parsing
24
 // Body parsing
26
 
28
 
27
 // A request on /send with user input = mail to be sent
29
 // A request on /send with user input = mail to be sent
28
 app.post('/send', function(req, res, next) {
30
 app.post('/send', function(req, res, next) {
31
+    // Count the failures
32
+    let status = {
33
+        failed: 0,
34
+        total: settings.recipients.length
35
+    };
36
+    
29
     // params will be used as:
37
     // params will be used as:
30
     // - values for html generation from the pug template
38
     // - values for html generation from the pug template
31
     // - parameters for sending the mail(s)
39
     // - parameters for sending the mail(s)
50
         logStatus(infos);
58
         logStatus(infos);
51
     }, function() {
59
     }, function() {
52
         res.header('Access-Control-Allow-Origin', '*');
60
         res.header('Access-Control-Allow-Origin', '*');
53
-        res.status(200).send();
61
+        if(status.failed === status.total) {
62
+            res.status(500).send()
63
+        } else {
64
+            res.status(200).send();
65
+        }
54
     })
66
     })
55
 });
67
 });
56
 
68
 
59
 var port = process.env.PORT || 1970;
71
 var port = process.env.PORT || 1970;
60
 // Start the server
72
 // Start the server
61
 app.listen(port, function() {
73
 app.listen(port, function() {
62
-    log.info("Server started on port " + port);
74
+    log.info('Server started on port ' + port);
63
 });
75
 });
64
 
76
 
65
 
77
 
102
         log.info('Message sent to ' + infos.accepted[0]);
114
         log.info('Message sent to ' + infos.accepted[0]);
103
     }
115
     }
104
     if(infos.rejected.length !== 0) {
116
     if(infos.rejected.length !== 0) {
117
+        status.failed++;
105
         log.info('Message failed to send to ' + infos.rejected[0]);
118
         log.info('Message failed to send to ' + infos.rejected[0]);
106
     }
119
     }
107
 }
120
 }