Last active
October 30, 2015 19:52
-
-
Save omnidan/6f7ffbc0837d2abc7fbb to your computer and use it in GitHub Desktop.
webhook to listen to trello tasks in a `Done` list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
npm install redux express body-parser
node index.js
(with node 4 or higher - I tested it with node 5)