From 9b630ae021937c88e5e5b6f113e95157a229b071 Mon Sep 17 00:00:00 2001 From: Badri Sunderarajan Date: Sat, 29 May 2021 00:10:16 +0530 Subject: [PATCH] Set up WebSocket for email sending The actual sending doesn't happen yet, but all the frontend infrastructure is set up all right! --- package.json | 1 + server.js | 37 +++++++- src/App.svelte | 238 ++++++++++++++++++++++++++++++++++--------------- yarn.lock | 19 ++++ 4 files changed, 220 insertions(+), 75 deletions(-) diff --git a/package.json b/package.json index 4dedec7..f8ad5f2 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", + "express-ws": "^4.0.0", "importabular": "^0.2.9", "showdown": "^1.9.1", "sirv-cli": "^1.0.0" diff --git a/server.js b/server.js index b040050..c81883c 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,8 @@ const express = require('express') require('dotenv').config() const app = express() +var expressWs = require('express-ws')(app) + const port = process.env.port || 5000 const cors = require('cors') @@ -18,10 +20,43 @@ app.get('/air', (req, res) => { }) }) +app.ws('/hit-send', (ws, request) => { + console.log('Hitting send on some emails!') + + ws.on('message', (message) => { + try { + message = JSON.parse(message) + } catch(err) { + ws.send(JSON.stringify({ + success: false, + error: "We can't understand what you're saying! Me speak JSON only.", + })) + return + } + + console.log(`\n\n-------------------------- BEGIN EMAIL --------------------------\n`) + console.log(`From: ${message.from}\nTo:${message.to}\n\n${message.text}`) + console.log(`\n--------------------------- END EMAIL ---------------------------`) + + setTimeout(() => { + ws.send(JSON.stringify({ + success: true, + from: message.from, + to: message.to, + })) + }, 2000) + }) + + ws.on('close', () => { + console.log('socket closed') + }) +}) + app.use(express.static('public')) -app.get('*', (req, res) => { +app.get('/', (req, res) => { res.sendFile(path.resolve(__dirname, 'public', 'index.html')) }) + app.listen(port, () => { console.log(`Server is up at port ${port}`) }) diff --git a/src/App.svelte b/src/App.svelte index 8725c29..9c1edf9 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -195,88 +195,178 @@ function prev() { step -= 1 } + + let nowSending + + /** + * Hit Send + * + * collates all the data for sending, and passes it + * on to the backend for actual processing. Note that here we + * cookie-cut the emails on the frontend itself; the backend + * just blindly takes what it gets. We're doing this so that we + * can use the same getPreview() function and be consistent: we + * don't want the final to suddenly end up looking very different + * from the preview! + */ + function hitSend() { + step = 4 + let emails = [] + + // process the 'i'th row of the sheet, one by one + for (let i=0; i { + try { + message = JSON.parse(message.data) + } catch (err) { + console.error(`Received malformed response from server: ${message.data}`) + return + } + + if (message.success) { + console.log(`${message.to}'s email sent successfully!`) + + // send the next one + nowSending = emails.pop() + if (!!nowSending) { + socket.send(JSON.stringify(nowSending)) + console.log(`Sending: ${nowSending.to}`) + } else { + socket.close() + step = 2 + } + } else { + console.error('Something went wrong :(') + step = 3 + } + } + }
- Email Oven -

Chip Choc

-

Cookie-cutter emails made easy.

+ {#if step < 4} + Email Oven +

Chip Choc

+

Cookie-cutter emails made easy.

- {#if step >= 1} -

Step 1

- {/if} - - {#if step == 0} - - {/if} - - {#if step >= 1} -
-

Compose your email below, leaving [TK stuff] to be replaced in the table. Make sure each TK is a single word: no spaces allowed!

-
- -