Skip to content

Instantly share code, notes, and snippets.

@omnidan
Last active October 30, 2015 19:52
Show Gist options
  • Save omnidan/6f7ffbc0837d2abc7fbb to your computer and use it in GitHub Desktop.
Save omnidan/6f7ffbc0837d2abc7fbb to your computer and use it in GitHub Desktop.
webhook to listen to trello tasks in a `Done` list
'use strict'
let createStore = require('redux').createStore
let express = require('express')
let bodyParser = require('body-parser')
let fs = require('fs')
// --
const tasksDone = (state, action) => {
if (!state) state = 0
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
const safeRequire = (path) => {
try {
return require(path)
} catch (e) {
return
}
}
const STATE_FILE = './state.json'
const loadStateJSON = () => safeRequire(STATE_FILE)
const saveStateJSON = state => fs.writeFileSync(STATE_FILE, JSON.stringify(state))
let store = createStore(tasksDone, loadStateJSON())
store.subscribe(() => console.log(store.getState()))
store.subscribe(() => saveStateJSON(store.getState()))
const isDone = list => list.name === 'Done'
const taskDone = () => store.dispatch({ type: 'INCREMENT' })
const taskUndone = () => store.dispatch({ type: 'DECREMENT' })
// --
let app = express()
app.use(bodyParser.json())
app.post('/', req => isDone(req.body.action.data.listAfter) ? taskDone() : taskUndone())
let server = app.listen(7000, () => {
let host = server.address().address
let port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})
@omnidan
Copy link
Author

omnidan commented Oct 30, 2015

npm install redux express body-parser

node index.js (with node 4 or higher - I tested it with node 5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment