소스 검색

And we're set!

Brendan Abolivier 8 년 전
부모
커밋
e5bdc0f929
로그인 계정: Brendan Abolivier <contact@brendanabolivier.com> GPG 키 ID: 8EF1500759F70623
2개의 변경된 파일52개의 추가작업 그리고 7개의 파일을 삭제
  1. 37
    5
      front/form.js
  2. 15
    2
      server.js

+ 37
- 5
front/form.js 파일 보기

@@ -7,11 +7,6 @@ var items = {
7 7
 
8 8
 var server  = getServer();
9 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 12
 // Returns the server's base URI based on the user's script tag
@@ -42,6 +37,11 @@ function generateForm(id) {
42 37
     // Set the form's behaviour
43 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 45
     var input = {
46 46
         name: getField(items.name, 'Your name', false, 'input'), // TODO: configurable prefix
47 47
         addr: getField(items.addr, 'Your e-mail address', true, 'input'),
@@ -59,6 +59,23 @@ function generateForm(id) {
59 59
     // Adding submit button
60 60
     
61 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,6 +164,11 @@ function getSubmitButton(id, text) {
147 164
 // Send form data through the XHR object
148 165
 // return: nothing
149 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 172
     xhrSend.open('POST', server + '/send');
151 173
     xhrSend.setRequestHeader('Content-Type', 'application/json');
152 174
     xhrSend.send(JSON.stringify(getFormData()));
@@ -162,4 +184,14 @@ function getFormData() {
162 184
         subj: document.getElementById(items.subj + '_input').value,
163 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 파일 보기

@@ -14,9 +14,11 @@ var log = printit({
14 14
     date: true
15 15
 });
16 16
 
17
+
17 18
 // nodemailer initial configuration
18 19
 var transporter = nodemailer.createTransport(settings.mailserver);
19 20
 
21
+
20 22
 // Serve static (JS + HTML) files
21 23
 app.use(express.static('front'));
22 24
 // Body parsing
@@ -26,6 +28,12 @@ app.use(bodyParser.json());
26 28
 
27 29
 // A request on /send with user input = mail to be sent
28 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 37
     // params will be used as:
30 38
     // - values for html generation from the pug template
31 39
     // - parameters for sending the mail(s)
@@ -50,7 +58,11 @@ app.post('/send', function(req, res, next) {
50 58
         logStatus(infos);
51 59
     }, function() {
52 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,7 +71,7 @@ app.post('/send', function(req, res, next) {
59 71
 var port = process.env.PORT || 1970;
60 72
 // Start the server
61 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,6 +114,7 @@ function logStatus(infos) {
102 114
         log.info('Message sent to ' + infos.accepted[0]);
103 115
     }
104 116
     if(infos.rejected.length !== 0) {
117
+        status.failed++;
105 118
         log.info('Message failed to send to ' + infos.rejected[0]);
106 119
     }
107 120
 }