diff --git a/package.json b/package.json index 10711232..31acb0bf 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "jsonwebtoken": "^5.7.0", "koa": "^1.1.2", "koa-better-static": "^1.0.5", + "koa-bodyparser": "^2.2.0", "koa-favicon": "^1.2.0", "koa-onerror": "^1.3.1", "koa-proxy": "github:foxcomm/koa-proxy", @@ -93,6 +94,7 @@ "lodash": "^4.3.0", "lodash.flatmap": "^4.2.0", "moment": "^2.15.2", + "mandrill-api": "^1.0.45", "marked": "^0.3.6", "react": "^15.2.0", "react-addons-css-transition-group": "^15.3.2", diff --git a/server/app.js b/server/app.js index 58428cf5..28e7b6d1 100644 --- a/server/app.js +++ b/server/app.js @@ -11,6 +11,8 @@ import verifyJwt from './verify-jwt'; import onerror from 'koa-onerror'; import moment from 'moment'; import chalk from 'chalk'; +import bodyParser from 'koa-bodyparser'; +import mandrillRouter from './routes/mandrill'; function timestamp() { return moment().format('D MMM H:mm:ss'); @@ -22,12 +24,18 @@ export default class App extends KoaApp { super(...args); onerror(this); + if (process.env.MAILCHIMP_API_KEY === void 0) { + throw new Error(`Can't load MAILCHIMP_API_KEY from environment.`); + } + this.use(serve('public')) .use(favicon('public/images/home/top-drawer-favicon.png')) .use(makeApiProxy()) .use(makeElasticProxy()) + .use(bodyParser()) .use(zipcodes.routes()) .use(zipcodes.allowedMethods()) + .use(mandrillRouter(process.env.MAILCHIMP_API_KEY)) .use(verifyJwt) .use(loadI18n) .use(renderReact); diff --git a/server/routes/mandrill.js b/server/routes/mandrill.js new file mode 100644 index 00000000..9ed62f16 --- /dev/null +++ b/server/routes/mandrill.js @@ -0,0 +1,41 @@ +import makeRouter from 'koa-router'; +import { Mandrill } from 'mandrill-api/mandrill'; + +function *sendMessage(mandrillClient, params) { + return new Promise((resolve, reject) => { + mandrillClient.messages.send(params, result => { + resolve(result); + console.log("RESOLVED : " + result); + }, error => { + const err = new Error(error.message || error); + reject(err); + console.log("REJECTED :" + error.message); + }); + }); +} + +export default function mandrillRouter(apiKey) { + const mandrillClient = new Mandrill(apiKey); + + const router = makeRouter() + .post('/api/node/mandrill', function*() { + const { message } = this.request.body; + + let async = false; + let ip_pool = "Main Pool"; + let send_at = "example send_at"; + + console.log("WE ARE SENDING!"); + yield sendMessage(mandrillClient, { + 'message': message, + 'async': async + }); + + this.body = {}; + }); + + return router.routes(); +} + + + diff --git a/src/pages/custom/custom.css b/src/pages/custom/custom.css index dee6190f..7acf914b 100644 --- a/src/pages/custom/custom.css +++ b/src/pages/custom/custom.css @@ -80,7 +80,6 @@ & .text-field { width: 100%; - display: none; padding-bottom: 10px; & input[type=text] { diff --git a/src/pages/custom/custom.jsx b/src/pages/custom/custom.jsx index ddf04ec4..47f237f4 100644 --- a/src/pages/custom/custom.jsx +++ b/src/pages/custom/custom.jsx @@ -2,14 +2,41 @@ import React, { Component } from 'react'; import type { HTMLElement } from 'types'; +import { autobind } from 'core-decorators'; import styles from './custom.css'; import { Form, FormField } from 'ui/forms'; import { TextInput } from 'ui/inputs'; import Button from 'ui/buttons'; +import { api } from 'lib/api'; +type State = { + name: string, + email: string, + message: string, +} class Custom extends Component { + state: State = { + name: '', + email: '', + message: '', + }; + + @autobind + sendCustomEmail() { + const message = { + 'html': this.state.message, + 'from_email': this.state.email, + 'to': [{ + email: 'adil@adilwali.com', + name: 'Adil Wali' + }] + }; + + api.post('/node/mandrill', { message }); + } + get topBanner(): HTMLElement { return (
@@ -39,6 +66,14 @@ class Custom extends Component { ); } + @autobind + handleFormChange(event) { + const { target } = event; + this.setState({ + [target.name]: target.value, + }); + } + get reachOut(): HTMLElement { return (
@@ -53,24 +88,24 @@ class Custom extends Component {

-
- - + + - - + - -